Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add simple type filter to store info #150

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading