Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[WIP]add playground catalog command #23

Merged
merged 1 commit into from
Dec 15, 2024
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
90 changes: 90 additions & 0 deletions cmd/playground/catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package playground

import (
"cmp"
"context"
"fmt"
"slices"

"github.com/spf13/cobra"

"github.com/iximiuz/labctl/internal/api"
"github.com/iximiuz/labctl/internal/labcli"
)

type catalogOptions struct {
filter string
quiet bool
}

func newCatalogCommand(cli labcli.CLI) *cobra.Command {
var opts catalogOptions

cmd := &cobra.Command{
Use: "catalog",
Aliases: []string{"cat"},
Short: `List catalog`,
RunE: func(cmd *cobra.Command, args []string) error {
return labcli.WrapStatusError(runListCatalogs(cmd.Context(), cli, &opts))
},
}
flags := cmd.Flags()

flags.BoolVarP(
&opts.quiet,
"quiet",
"q",
false,
`Only print playground IDs`,
)

flags.StringVarP(
&opts.filter,
"filter",
"f",
"",
`Filter to use for catalog list. ("recent" | "popular" | "my-custom") (default "")`,
)
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...)
}

slices.SortFunc(catalog, func(a, b api.Playground) int { return cmp.Compare(a.Name, b.Name) })

fmt.Fprintln(cli.OutputStream(), "Available playgrounds:")
if opts.quiet {
for _, p := range catalog {
fmt.Fprintln(cli.OutputStream(), 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)
}
}

return nil
}
1 change: 1 addition & 0 deletions cmd/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func NewCommand(cli labcli.CLI) *cobra.Command {

cmd.AddCommand(
newListCommand(cli),
newCatalogCommand(cli),
newStartCommand(cli),
newStopCommand(cli),
newMachinesCommand(cli),
Expand Down
2 changes: 1 addition & 1 deletion cmd/playground/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
if err != nil {
cli.PrintErr("Couldn't list known playgrounds: %v\n", err)
return ""
Expand Down
10 changes: 8 additions & 2 deletions internal/api/playgrounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"context"
"net/url"
)

type Playground struct {
Expand All @@ -26,9 +27,14 @@ 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) ([]Playground, error) {
func (c *Client) ListPlaygrounds(ctx context.Context, filter string) ([]Playground, error) {
var plays []Playground
return plays, c.GetInto(ctx, "/playgrounds", nil, nil, &plays)

q := url.Values{}
if filter != "" {
q.Add("filter", filter)
}
return plays, c.GetInto(ctx, "/playgrounds", q, nil, &plays)
}

type MachineUser struct {
Expand Down