Unverified Commit 5526a928 authored by bryanl's avatar bryanl
Browse files

Add `--installed` flag to pkg list



Adds a new flag to `pkg list` that only lists installed packages.
Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent b03ceb4f
......@@ -30,6 +30,7 @@ ks pkg list [flags]
```
-h, --help help for list
--installed Only list installed packages
```
### Options inherited from parent commands
......
......@@ -61,6 +61,8 @@ const (
OptionGlobal = "global"
// OptionGracePeriod is gracePeriod option.
OptionGracePeriod = "grace-period"
// OptionInstalled is for listing installed packages.
OptionInstalled = "only-installed"
// OptionJPaths is jsonnet paths.
OptionJPaths = "jpaths"
// OptionLibName is libName.
......
......@@ -44,6 +44,8 @@ func RunPkgList(m map[string]interface{}) error {
// PkgList lists available registries
type PkgList struct {
app app.App
onlyInstalled bool
registryListFn func(ksApp app.App) ([]registry.Registry, error)
out io.Writer
}
......@@ -54,6 +56,7 @@ func NewPkgList(m map[string]interface{}) (*PkgList, error) {
rl := &PkgList{
app: ol.LoadApp(),
onlyInstalled: ol.LoadBool(OptionInstalled),
registryListFn: registry.List,
out: os.Stdout,
......@@ -87,13 +90,13 @@ func (pl *PkgList) Run() error {
}
for libName := range spec.Libraries {
row := []string{r.Name(), libName}
_, isInstalled := appLibraries[libName]
if isInstalled {
row = append(row, pkgInstalled)
if pl.onlyInstalled && !isInstalled {
continue
}
rows = append(rows, row)
rows = append(rows, pl.addRow(r.Name(), libName, isInstalled))
}
}
......@@ -109,3 +112,12 @@ func (pl *PkgList) Run() error {
t.AppendBulk(rows)
return t.Render()
}
func (pl *PkgList) addRow(regName, libName string, isInstalled bool) []string {
row := []string{regName, libName}
if isInstalled {
row = append(row, pkgInstalled)
}
return row
}
......@@ -33,16 +33,6 @@ func TestPkgList(t *testing.T) {
appMock.On("Libraries").Return(libaries, nil)
in := map[string]interface{}{
OptionApp: appMock,
}
a, err := NewPkgList(in)
require.NoError(t, err)
var buf bytes.Buffer
a.out = &buf
spec := &registry.Spec{
Libraries: registry.LibraryRefSpecs{
"lib1": &registry.LibraryRef{},
......@@ -53,15 +43,47 @@ func TestPkgList(t *testing.T) {
incubator := mockRegistry("incubator", false)
incubator.On("FetchRegistrySpec").Return(spec, nil)
cases := []struct {
name string
onlyInstalled bool
outputName string
}{
{
name: "list all packages",
outputName: "pkg/list/output.txt",
},
{
name: "installed packages",
onlyInstalled: true,
outputName: "pkg/list/installed.txt",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
in := map[string]interface{}{
OptionApp: appMock,
OptionInstalled: tc.onlyInstalled,
}
a, err := NewPkgList(in)
require.NoError(t, err)
a.registryListFn = func(app.App) ([]registry.Registry, error) {
registries := []registry.Registry{incubator}
return registries, nil
}
var buf bytes.Buffer
a.out = &buf
err = a.Run()
require.NoError(t, err)
assertOutput(t, "pkg/list/output.txt", buf.String())
assertOutput(t, tc.outputName, buf.String())
})
}
})
}
......
REGISTRY NAME INSTALLED
======== ==== =========
incubator lib1 *
......@@ -30,6 +30,7 @@ const (
flagFilename = "filename"
flagGcTag = "gc-tag"
flagGracePeriod = "grace-period"
flagInstalled = "installed"
flagJpath = "jpath"
flagModule = "module"
flagNamespace = "namespace"
......
......@@ -18,10 +18,16 @@ package clicmd
import (
"fmt"
"github.com/spf13/viper"
"github.com/ksonnet/ksonnet/pkg/actions"
"github.com/spf13/cobra"
)
const (
vPkgListInstalled = "pkg-list-installed"
)
var pkgListCmd = &cobra.Command{
Use: "list",
Short: pkgShortDesc["list"],
......@@ -32,6 +38,7 @@ var pkgListCmd = &cobra.Command{
m := map[string]interface{}{
actions.OptionApp: ka,
actions.OptionInstalled: viper.GetBool(vPkgListInstalled),
}
return runAction(actionPkgList, m)
......@@ -57,4 +64,7 @@ the following info:
func init() {
pkgCmd.AddCommand(pkgListCmd)
pkgListCmd.Flags().Bool(flagInstalled, false, "Only list installed packages")
viper.BindPFlag(vPkgListInstalled, pkgListCmd.Flags().Lookup(flagInstalled))
}
......@@ -29,6 +29,7 @@ func Test_pkgListCmd(t *testing.T) {
action: actionPkgList,
expected: map[string]interface{}{
actions.OptionApp: ka,
actions.OptionInstalled: false,
},
},
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment