Skip to content

Commit

Permalink
added list-repos flag (#298)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Toy <[email protected]>
  • Loading branch information
zackbradys and atoy3731 authored Aug 23, 2024
1 parent d6b3c94 commit bd0cd8f
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 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 {
OutputFormat string
TypeFilter string
SizeUnit string
ListRepos bool
}

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, sigs, atts, sbom)")
f.BoolVar(&o.ListRepos, "list-repos", false, "List all repository names")

// TODO: Regex/globbing
}
Expand Down Expand Up @@ -121,6 +123,11 @@ func InfoCmd(ctx context.Context, o *InfoOpts, s *store.Layout) error {
return err
}

if o.ListRepos {
buildListRepos(items...)
return nil
}

// sort items by ref and arch
sort.Sort(byReferenceAndArch(items))

Expand All @@ -135,6 +142,30 @@ func InfoCmd(ctx context.Context, o *InfoOpts, s *store.Layout) error {
return nil
}

func buildListRepos(items ...item) {
// Create map to track unique repository names
repos := make(map[string]bool)

for _, i := range items {
repoName := ""
for j := 0; j < len(i.Reference); j++ {
if i.Reference[j] == '/' {
repoName = i.Reference[:j]
break
}
}
if repoName == "" {
repoName = i.Reference
}
repos[repoName] = true
}

// Collect and print unique repository names
for repoName := range repos {
fmt.Println(repoName)
}
}

func buildTable(items ...item) {
// Create a table for the results
table := tablewriter.NewWriter(os.Stdout)
Expand Down

0 comments on commit bd0cd8f

Please sign in to comment.