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] ...@@ -30,6 +30,7 @@ ks pkg list [flags]
``` ```
-h, --help help for list -h, --help help for list
--installed Only list installed packages
``` ```
### Options inherited from parent commands ### Options inherited from parent commands
......
...@@ -61,6 +61,8 @@ const ( ...@@ -61,6 +61,8 @@ const (
OptionGlobal = "global" OptionGlobal = "global"
// OptionGracePeriod is gracePeriod option. // OptionGracePeriod is gracePeriod option.
OptionGracePeriod = "grace-period" OptionGracePeriod = "grace-period"
// OptionInstalled is for listing installed packages.
OptionInstalled = "only-installed"
// OptionJPaths is jsonnet paths. // OptionJPaths is jsonnet paths.
OptionJPaths = "jpaths" OptionJPaths = "jpaths"
// OptionLibName is libName. // OptionLibName is libName.
......
...@@ -44,6 +44,8 @@ func RunPkgList(m map[string]interface{}) error { ...@@ -44,6 +44,8 @@ func RunPkgList(m map[string]interface{}) error {
// PkgList lists available registries // PkgList lists available registries
type PkgList struct { type PkgList struct {
app app.App app app.App
onlyInstalled bool
registryListFn func(ksApp app.App) ([]registry.Registry, error) registryListFn func(ksApp app.App) ([]registry.Registry, error)
out io.Writer out io.Writer
} }
...@@ -54,6 +56,7 @@ func NewPkgList(m map[string]interface{}) (*PkgList, error) { ...@@ -54,6 +56,7 @@ func NewPkgList(m map[string]interface{}) (*PkgList, error) {
rl := &PkgList{ rl := &PkgList{
app: ol.LoadApp(), app: ol.LoadApp(),
onlyInstalled: ol.LoadBool(OptionInstalled),
registryListFn: registry.List, registryListFn: registry.List,
out: os.Stdout, out: os.Stdout,
...@@ -87,13 +90,13 @@ func (pl *PkgList) Run() error { ...@@ -87,13 +90,13 @@ func (pl *PkgList) Run() error {
} }
for libName := range spec.Libraries { for libName := range spec.Libraries {
row := []string{r.Name(), libName}
_, isInstalled := appLibraries[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 { ...@@ -109,3 +112,12 @@ func (pl *PkgList) Run() error {
t.AppendBulk(rows) t.AppendBulk(rows)
return t.Render() 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) { ...@@ -33,16 +33,6 @@ func TestPkgList(t *testing.T) {
appMock.On("Libraries").Return(libaries, nil) 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{ spec := &registry.Spec{
Libraries: registry.LibraryRefSpecs{ Libraries: registry.LibraryRefSpecs{
"lib1": &registry.LibraryRef{}, "lib1": &registry.LibraryRef{},
...@@ -53,15 +43,47 @@ func TestPkgList(t *testing.T) { ...@@ -53,15 +43,47 @@ func TestPkgList(t *testing.T) {
incubator := mockRegistry("incubator", false) incubator := mockRegistry("incubator", false)
incubator.On("FetchRegistrySpec").Return(spec, nil) 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) { a.registryListFn = func(app.App) ([]registry.Registry, error) {
registries := []registry.Registry{incubator} registries := []registry.Registry{incubator}
return registries, nil return registries, nil
} }
var buf bytes.Buffer
a.out = &buf
err = a.Run() err = a.Run()
require.NoError(t, err) 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 ( ...@@ -30,6 +30,7 @@ const (
flagFilename = "filename" flagFilename = "filename"
flagGcTag = "gc-tag" flagGcTag = "gc-tag"
flagGracePeriod = "grace-period" flagGracePeriod = "grace-period"
flagInstalled = "installed"
flagJpath = "jpath" flagJpath = "jpath"
flagModule = "module" flagModule = "module"
flagNamespace = "namespace" flagNamespace = "namespace"
......
...@@ -18,10 +18,16 @@ package clicmd ...@@ -18,10 +18,16 @@ package clicmd
import ( import (
"fmt" "fmt"
"github.com/spf13/viper"
"github.com/ksonnet/ksonnet/pkg/actions" "github.com/ksonnet/ksonnet/pkg/actions"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const (
vPkgListInstalled = "pkg-list-installed"
)
var pkgListCmd = &cobra.Command{ var pkgListCmd = &cobra.Command{
Use: "list", Use: "list",
Short: pkgShortDesc["list"], Short: pkgShortDesc["list"],
...@@ -32,6 +38,7 @@ var pkgListCmd = &cobra.Command{ ...@@ -32,6 +38,7 @@ var pkgListCmd = &cobra.Command{
m := map[string]interface{}{ m := map[string]interface{}{
actions.OptionApp: ka, actions.OptionApp: ka,
actions.OptionInstalled: viper.GetBool(vPkgListInstalled),
} }
return runAction(actionPkgList, m) return runAction(actionPkgList, m)
...@@ -57,4 +64,7 @@ the following info: ...@@ -57,4 +64,7 @@ the following info:
func init() { func init() {
pkgCmd.AddCommand(pkgListCmd) 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) { ...@@ -29,6 +29,7 @@ func Test_pkgListCmd(t *testing.T) {
action: actionPkgList, action: actionPkgList,
expected: map[string]interface{}{ expected: map[string]interface{}{
actions.OptionApp: ka, 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