Skip to content

Commit

Permalink
Merge pull request #81 from warptools/useful-catalog-ls
Browse files Browse the repository at this point in the history
Enhance catalog ls to traverse down catalogs.
  • Loading branch information
TripleDogDare committed Sep 13, 2023
2 parents 8751fe7 + ea2162e commit 2961a17
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/_docs/warpforge-catalog.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Add an item to the given catalog in the root workspace. Will create a catalog if
Add a module to the root workspace catalog as a new release
### ls
List available catalogs in the root workspace
List available catalogs in the root workspace and their contents
### show
Show the contents of a module in the root workspace catalog
Expand Down
81 changes: 70 additions & 11 deletions app/catalog/catalog_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ var catalogCmdDef = &cli.Command{
),
},
{
Name: "ls",
Usage: "List available catalogs in the root workspace",
Name: "ls",
Usage: "List available catalogs in the root workspace and their contents",
ArgsUsage: "[catalog] [module] [release] [item]",
Action: util.ChainCmdMiddleware(cmdCatalogLs,
util.CmdMiddlewareLogging,
util.CmdMiddlewareTracingConfig,
Expand All @@ -89,7 +90,6 @@ var catalogCmdDef = &cli.Command{
{
Name: "show",
Usage: "Show the contents of a module in the root workspace catalog",

Action: util.ChainCmdMiddleware(cmdCatalogShow,
util.CmdMiddlewareLogging,
util.CmdMiddlewareTracingConfig,
Expand Down Expand Up @@ -355,19 +355,78 @@ func cmdCatalogLs(c *cli.Context) error {
return err
}

// get the list of catalogs in this workspace
catalogs, err := wsSet.Root().ListCatalogs()
if c.NArg() == 0 {
// get the list of catalogs in this workspace
catalogs, err := wsSet.Root().ListCatalogs()
if err != nil {
return fmt.Errorf("failed to list catalogs: %s", err)
}

// print the list
for _, catalog := range catalogs {
if catalog != "" {
fmt.Fprintf(c.App.Writer, "%s\n", catalog)
}
}
return nil
}

currentArg := 0
nextPosArg := func() (string, bool) {
if currentArg > c.NArg() {
return "", false
}
arg := c.Args().Get(currentArg)
currentArg++
return arg, currentArg < c.NArg()
}

catalogName, moreArgs := nextPosArg()
catalog, err := wsSet.Root().OpenCatalog(catalogName)
if err != nil {
return fmt.Errorf("failed to list catalogs: %s", err)
return fmt.Errorf("failed to find catalog: %s", err)
}
if !moreArgs {
// get list of modules in this catalog
for _, module := range catalog.Modules() {
fmt.Fprintf(c.App.Writer, "%s\n", module)
}
return nil
}

// print the list
for _, catalog := range catalogs {
if catalog != "" {
fmt.Fprintf(c.App.Writer, "%s\n", catalog)
moduleName, moreArgs := nextPosArg()
ref := wfapi.CatalogRef{
ModuleName: wfapi.ModuleName(moduleName),
}
module, err := catalog.GetModule(ref)
if err != nil {
return fmt.Errorf("failed to find module: %s", err)
}
if !moreArgs {
// get list of releases in a module
for _, release := range module.Releases.Keys {
fmt.Fprintf(c.App.Writer, "%s\n", release)
}
return nil
}
return nil

releaseName, moreArgs := nextPosArg()
ref.ReleaseName = wfapi.ReleaseName(releaseName)
release, err := catalog.GetRelease(ref)
if err != nil {
return fmt.Errorf("failed to find release: %s", err)
}
if !moreArgs {
// list ware IDs for items
for _, label := range release.Items.Keys {
item := release.Items.Values[label]
fmt.Fprintf(c.App.Writer, "%s\t%s\n", item, label)
}
return nil
}
// after this idk, we could list mirrors or replays or... nothing.
// this seems like a good ending point due to ambiguity of meaning
return fmt.Errorf("too many args")
}

func cmdCatalogBundle(c *cli.Context) error {
Expand Down

0 comments on commit 2961a17

Please sign in to comment.