Skip to content

Commit

Permalink
Merge pull request #19 from jedrekdomanski/machine-command
Browse files Browse the repository at this point in the history
Add playground machines sub-command to labctl for listing machines in a playground
  • Loading branch information
iximiuz authored Oct 17, 2024
2 parents dc963f5 + ecfd9de commit e828086
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
41 changes: 41 additions & 0 deletions cmd/playground/machines.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions cmd/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewCommand(cli labcli.CLI) *cobra.Command {
newListCommand(cli),
newStartCommand(cli),
newStopCommand(cli),
newMachinesCommand(cli),
)

return cmd
Expand Down

0 comments on commit e828086

Please sign in to comment.