From 2174e96f0e571b1bb7ce4b4ddb86fd226f7dd2ee Mon Sep 17 00:00:00 2001 From: Adam Martin Date: Tue, 19 Dec 2023 09:59:06 -0500 Subject: [PATCH] add simple type filter to store info Signed-off-by: Adam Martin --- cmd/hauler/cli/store.go | 12 ++++++++++-- cmd/hauler/cli/store/info.go | 14 ++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/cmd/hauler/cli/store.go b/cmd/hauler/cli/store.go index 519922c2..86a64661 100644 --- a/cmd/hauler/cli/store.go +++ b/cmd/hauler/cli/store.go @@ -3,6 +3,7 @@ package cli import ( "github.com/spf13/cobra" "helm.sh/helm/v3/pkg/action" + "fmt" "github.com/rancherfederal/hauler/cmd/hauler/cli/store" ) @@ -155,6 +156,8 @@ func addStoreSave() *cobra.Command { func addStoreInfo() *cobra.Command { o := &store.InfoOpts{RootOpts: rootStoreOpts} + var allowedValues = []string{"image", "chart", "file", "all"} + cmd := &cobra.Command{ Use: "info", Short: "Print out information about the store", @@ -167,8 +170,13 @@ func addStoreInfo() *cobra.Command { if err != nil { return err } - - return store.InfoCmd(ctx, o, s) + + for _, allowed := range allowedValues { + if o.TypeFilter == allowed { + return store.InfoCmd(ctx, o, s) + } + } + return fmt.Errorf("type must be one of %v", allowedValues) }, } o.AddFlags(cmd) diff --git a/cmd/hauler/cli/store/info.go b/cmd/hauler/cli/store/info.go index 6ea255b1..8bebe531 100644 --- a/cmd/hauler/cli/store/info.go +++ b/cmd/hauler/cli/store/info.go @@ -22,6 +22,7 @@ type InfoOpts struct { *RootOpts OutputFormat string + TypeFilter string SizeUnit string } @@ -29,6 +30,7 @@ func (o *InfoOpts) AddFlags(cmd *cobra.Command) { f := cmd.Flags() f.StringVarP(&o.OutputFormat, "output", "o", "table", "Output format (table, json)") + f.StringVarP(&o.TypeFilter, "type", "t", "all", "Filter on type (image, chart, file)") // TODO: Regex/globbing } @@ -64,7 +66,7 @@ func InfoCmd(ctx context.Context, o *InfoOpts, s *store.Layout) error { return err } - i := newItem(s, desc, internalManifest, internalDesc.Platform.Architecture) + i := newItem(s, desc, internalManifest, internalDesc.Platform.Architecture, o) var emptyItem item if i != emptyItem { items = append(items, i) @@ -89,7 +91,7 @@ func InfoCmd(ctx context.Context, o *InfoOpts, s *store.Layout) error { return err } - i := newItem(s, desc, m, internalManifest.Architecture) + i := newItem(s, desc, m, internalManifest.Architecture, o) var emptyItem item if i != emptyItem { items = append(items, i) @@ -101,7 +103,7 @@ func InfoCmd(ctx context.Context, o *InfoOpts, s *store.Layout) error { return err } - i := newItem(s, desc, m, "-") + i := newItem(s, desc, m, "-", o) var emptyItem item if i != emptyItem { items = append(items, i) @@ -177,7 +179,7 @@ func (a byReferenceAndArch) Less(i, j int) bool { return a[i].Reference < a[j].Reference } -func newItem(s *store.Layout, desc ocispec.Descriptor, m ocispec.Manifest, arch string) item { +func newItem(s *store.Layout, desc ocispec.Descriptor, m ocispec.Manifest, arch string, o *InfoOpts) item { // skip listing cosign items if desc.Annotations["kind"] == "dev.cosignproject.cosign/atts" || desc.Annotations["kind"] == "dev.cosignproject.cosign/sigs" || @@ -208,6 +210,10 @@ func newItem(s *store.Layout, desc ocispec.Descriptor, m ocispec.Manifest, arch return item{} } + if o.TypeFilter != "all" && ctype != o.TypeFilter { + return item{} + } + return item{ Reference: ref.Name(), Type: ctype,