From e2ad16f373bfd6b9b73f906a903dad75800b2d94 Mon Sep 17 00:00:00 2001 From: Ivan Velichko Date: Sun, 15 Dec 2024 18:45:55 +0000 Subject: [PATCH] polish the labctl playground catalog command a little --- cmd/challenge/catalog.go | 2 +- cmd/playground/catalog.go | 51 +++++++++++++------------------------ cmd/playground/start.go | 2 +- internal/api/playgrounds.go | 11 +++++--- 4 files changed, 27 insertions(+), 39 deletions(-) diff --git a/cmd/challenge/catalog.go b/cmd/challenge/catalog.go index 760901f..2848981 100644 --- a/cmd/challenge/catalog.go +++ b/cmd/challenge/catalog.go @@ -21,7 +21,7 @@ func newCatalogCommand(cli labcli.CLI) *cobra.Command { cmd := &cobra.Command{ Use: "catalog", - Aliases: []string{"catalog"}, + Aliases: []string{"cat", "catalog"}, Short: "List challenges from the catalog, optionally filtered by category and/or status", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/playground/catalog.go b/cmd/playground/catalog.go index ff74ece..9222d05 100644 --- a/cmd/playground/catalog.go +++ b/cmd/playground/catalog.go @@ -23,9 +23,9 @@ func newCatalogCommand(cli labcli.CLI) *cobra.Command { cmd := &cobra.Command{ Use: "catalog", Aliases: []string{"cat"}, - Short: `List catalog`, + Short: "List playgrounds from the catalog, optionally filtering by type", RunE: func(cmd *cobra.Command, args []string) error { - return labcli.WrapStatusError(runListCatalogs(cmd.Context(), cli, &opts)) + return labcli.WrapStatusError(runCatalog(cmd.Context(), cli, &opts)) }, } flags := cmd.Flags() @@ -35,54 +35,37 @@ func newCatalogCommand(cli labcli.CLI) *cobra.Command { "quiet", "q", false, - `Only print playground IDs`, + `Only print playground names`, ) - flags.StringVarP( &opts.filter, "filter", "f", "", - `Filter to use for catalog list. ("recent" | "popular" | "my-custom") (default "")`, + `Filter to use for catalog list. ("recent" | "popular" | "my-custom") (default no filter, meaning all)`, ) return cmd } -func runListCatalogs(ctx context.Context, cli labcli.CLI, opts *catalogOptions) error { - - var catalog []api.Playground - var err error - - // once the backend is fixed there is no need for this if - if opts.filter != "" { - catalog, err = cli.Client().ListPlaygrounds(ctx, opts.filter) - if err != nil { - return fmt.Errorf("couldn't list catalog: %w", err) - } - } else { - catalog, err = cli.Client().ListPlaygrounds(ctx, "") - if err != nil { - return fmt.Errorf("couldn't list catalog: %w", err) - } - customs, err := cli.Client().ListPlaygrounds(ctx, "my-custom") - if err != nil { - return fmt.Errorf("couldn't list catalog: %w", err) - } - catalog = append(catalog, customs...) +func runCatalog(ctx context.Context, cli labcli.CLI, opts *catalogOptions) error { + playgrounds, err := cli.Client().ListPlaygrounds(ctx, &api.ListPlaygroundsOptions{ + Filter: opts.filter, + }) + if err != nil { + return fmt.Errorf("couldn't list playgrounds: %w", err) } - slices.SortFunc(catalog, func(a, b api.Playground) int { return cmp.Compare(a.Name, b.Name) }) + slices.SortFunc(playgrounds, func(a, b api.Playground) int { return cmp.Compare(a.Name, b.Name) }) + + cli.PrintAux("Available playgrounds:\n") - fmt.Fprintln(cli.OutputStream(), "Available playgrounds:") if opts.quiet { - for _, p := range catalog { - fmt.Fprintln(cli.OutputStream(), p.Name) + for _, p := range playgrounds { + cli.PrintOut("%s\n", p.Name) } - } else { - for _, p := range catalog { - //fmt.Fprintf(cli.OutputStream(), "%-30v %-30v\n", p.Name, p.Title) - fmt.Fprintf(cli.OutputStream(), " - %s - %s\n", p.Name, p.Description) + for _, p := range playgrounds { + cli.PrintAux(" - %s - %s\n", p.Name, p.Description) } } diff --git a/cmd/playground/start.go b/cmd/playground/start.go index ccddf96..221ca01 100644 --- a/cmd/playground/start.go +++ b/cmd/playground/start.go @@ -208,7 +208,7 @@ func runStartPlayground(ctx context.Context, cli labcli.CLI, opts *startOptions) } func listKnownPlaygrounds(ctx context.Context, cli labcli.CLI) string { - playgrounds, err := cli.Client().ListPlaygrounds(ctx, "") + playgrounds, err := cli.Client().ListPlaygrounds(ctx, nil) if err != nil { cli.PrintErr("Couldn't list known playgrounds: %v\n", err) return "" diff --git a/internal/api/playgrounds.go b/internal/api/playgrounds.go index 6b113aa..594f042 100644 --- a/internal/api/playgrounds.go +++ b/internal/api/playgrounds.go @@ -28,13 +28,18 @@ func (c *Client) GetPlayground(ctx context.Context, name string) (*Playground, e return &p, c.GetInto(ctx, "/playgrounds/"+name, nil, nil, &p) } -func (c *Client) ListPlaygrounds(ctx context.Context, filter string) ([]Playground, error) { +type ListPlaygroundsOptions struct { + Filter string +} + +func (c *Client) ListPlaygrounds(ctx context.Context, opts *ListPlaygroundsOptions) ([]Playground, error) { var plays []Playground q := url.Values{} - if filter != "" { - q.Add("filter", filter) + if opts != nil && opts.Filter != "" { + q.Add("filter", opts.Filter) } + return plays, c.GetInto(ctx, "/playgrounds", q, nil, &plays) }