|
| 1 | +package playground |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "github.com/iximiuz/labctl/internal/completion" |
| 11 | + "github.com/iximiuz/labctl/internal/labcli" |
| 12 | +) |
| 13 | + |
| 14 | +type lifetimeOptions struct { |
| 15 | + playID string |
| 16 | + lifetime time.Duration |
| 17 | +} |
| 18 | + |
| 19 | +func newLifetimeCommand(cli labcli.CLI) *cobra.Command { |
| 20 | + var opts lifetimeOptions |
| 21 | + |
| 22 | + cmd := &cobra.Command{ |
| 23 | + Use: "lifetime <playground-id> [new-lifetime]", |
| 24 | + Short: `Show or set a playground's lifetime - the total session duration counted from the playground's start (e.g., 90m, 3h, 2h30m)`, |
| 25 | + Args: cobra.RangeArgs(1, 2), |
| 26 | + ValidArgsFunction: completion.ActivePlays(cli), |
| 27 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 28 | + opts.playID = args[0] |
| 29 | + |
| 30 | + if len(args) == 1 { |
| 31 | + return labcli.WrapStatusError(runShowPlaygroundLifetime(cmd.Context(), cli, &opts)) |
| 32 | + } |
| 33 | + |
| 34 | + lifetime, err := time.ParseDuration(args[1]) |
| 35 | + if err != nil { |
| 36 | + return labcli.WrapStatusError(fmt.Errorf("invalid lifetime %q: %w", args[1], err)) |
| 37 | + } |
| 38 | + opts.lifetime = lifetime |
| 39 | + |
| 40 | + return labcli.WrapStatusError(runSetPlaygroundLifetime(cmd.Context(), cli, &opts)) |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + return cmd |
| 45 | +} |
| 46 | + |
| 47 | +func runShowPlaygroundLifetime(ctx context.Context, cli labcli.CLI, opts *lifetimeOptions) error { |
| 48 | + play, err := cli.Client().GetPlay(ctx, opts.playID) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("couldn't get playground: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + if play.MaxPlayTime == "" { |
| 54 | + return labcli.NewStatusError(1, "playground %s has no lifetime information", opts.playID) |
| 55 | + } |
| 56 | + |
| 57 | + lifetime, err := time.ParseDuration(play.MaxPlayTime) |
| 58 | + if err != nil { |
| 59 | + cli.PrintOut("%s\n", play.MaxPlayTime) |
| 60 | + return nil |
| 61 | + } |
| 62 | + |
| 63 | + if play.ExpiresIn > 0 { |
| 64 | + cli.PrintOut("%s (expires in %s)\n", lifetime, (time.Duration(play.ExpiresIn) * time.Millisecond).Round(time.Second)) |
| 65 | + } else { |
| 66 | + cli.PrintOut("%s\n (expired)", lifetime) |
| 67 | + } |
| 68 | + |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +func runSetPlaygroundLifetime(ctx context.Context, cli labcli.CLI, opts *lifetimeOptions) error { |
| 73 | + minutes := int(opts.lifetime.Minutes()) |
| 74 | + if minutes < 1 { |
| 75 | + return fmt.Errorf("lifetime must be at least 1 minute") |
| 76 | + } |
| 77 | + |
| 78 | + cli.PrintAux("Setting lifetime of playground %s to %s...\n", opts.playID, opts.lifetime) |
| 79 | + |
| 80 | + play, err := cli.Client().SetPlayMaxPlayTime(ctx, opts.playID, minutes) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("couldn't set the playground lifetime: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + if play.ExpiresIn > 0 { |
| 86 | + cli.PrintAux("Playground will now expire in %s.\n", (time.Duration(play.ExpiresIn) * time.Millisecond).Round(time.Second)) |
| 87 | + } else { |
| 88 | + cli.PrintAux("Playground lifetime is set.\n") |
| 89 | + } |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
0 commit comments