Skip to content

Commit

Permalink
Merge pull request #150 from amartin120/info-type-filter
Browse files Browse the repository at this point in the history
add simple type filter to store info
  • Loading branch information
amartin120 authored Dec 19, 2023
2 parents 8cfe443 + 2174e96 commit f982f51
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
12 changes: 10 additions & 2 deletions cmd/hauler/cli/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions cmd/hauler/cli/store/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ type InfoOpts struct {
*RootOpts

OutputFormat string
TypeFilter string
SizeUnit string
}

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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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" ||
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f982f51

Please sign in to comment.