From ecfd9def472e23fcff4913581e593300313e5d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Doma=C5=84ski?= Date: Thu, 17 Oct 2024 00:30:01 +0200 Subject: [PATCH] Add labctl playground machines [Playground ID] --- cmd/playground/machines.go | 41 ++++++++++++++++++++++++++++++++++++ cmd/playground/playground.go | 1 + 2 files changed, 42 insertions(+) create mode 100644 cmd/playground/machines.go diff --git a/cmd/playground/machines.go b/cmd/playground/machines.go new file mode 100644 index 0000000..1956014 --- /dev/null +++ b/cmd/playground/machines.go @@ -0,0 +1,41 @@ +package playground + +import ( + "context" + "fmt" + "text/tabwriter" + "github.com/spf13/cobra" + "github.com/iximiuz/labctl/internal/labcli" +) + +func newMachinesCommand(cli labcli.CLI) *cobra.Command { + cmd := &cobra.Command{ + Use: "machines [Playground ID]", + Short: `List machines of a specific playground by Playground ID`, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + playgroundID := args[0] + return labcli.WrapStatusError(runListMachines(cmd.Context(), cli, playgroundID)) + }, + } + + return cmd +} + +func runListMachines(ctx context.Context, cli labcli.CLI, playgroundID string) error { + play, err := cli.Client().GetPlay(ctx, playgroundID) + if err != nil { + return fmt.Errorf("couldn't find playground with ID %s: %w", playgroundID, err) + } + + writer := tabwriter.NewWriter(cli.OutputStream(), 0, 4, 2, ' ', 0) + defer writer.Flush() + + fmt.Fprintln(writer, "MACHINE NAME") + + for _, machine := range play.Machines { + fmt.Fprintln(writer, machine.Name) + } + + return nil +} diff --git a/cmd/playground/playground.go b/cmd/playground/playground.go index cf2fb88..df67a5c 100644 --- a/cmd/playground/playground.go +++ b/cmd/playground/playground.go @@ -17,6 +17,7 @@ func NewCommand(cli labcli.CLI) *cobra.Command { newListCommand(cli), newStartCommand(cli), newStopCommand(cli), + newMachinesCommand(cli), ) return cmd