Skip to content

Commit 27314c0

Browse files
committed
Add support for rm --all
1 parent 29dde0e commit 27314c0

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

pkg/cmd/rm.go

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,65 @@ import (
1212
var rmCmd = cli.Command{
1313
Name: "rm",
1414
Usage: "Remove one or more instances",
15-
ArgsUsage: "<instance> [instance...]",
15+
ArgsUsage: "[instance...]",
1616
Flags: []cli.Flag{
1717
&cli.BoolFlag{
1818
Name: "force",
1919
Aliases: []string{"f"},
2020
Usage: "Force removal of running instances",
2121
},
22+
&cli.BoolFlag{
23+
Name: "all",
24+
Usage: "Remove all instances (stopped only, unless --force)",
25+
},
2226
},
2327
Action: handleRm,
2428
HideHelpCommand: true,
2529
}
2630

2731
func handleRm(ctx context.Context, cmd *cli.Command) error {
2832
args := cmd.Args().Slice()
29-
if len(args) < 1 {
30-
return fmt.Errorf("instance ID required\nUsage: hypeman rm [flags] <instance> [instance...]")
33+
force := cmd.Bool("force")
34+
all := cmd.Bool("all")
35+
36+
if !all && len(args) < 1 {
37+
return fmt.Errorf("instance ID required\nUsage: hypeman rm [flags] <instance> [instance...]\n hypeman rm --all [--force]")
3138
}
3239

33-
force := cmd.Bool("force")
3440
client := hypeman.NewClient(getDefaultRequestOptions(cmd)...)
3541

36-
var lastErr error
37-
for _, identifier := range args {
38-
// Resolve instance by ID, partial ID, or name
39-
instanceID, err := ResolveInstance(ctx, &client, identifier)
42+
// If --all, get all instance IDs
43+
var identifiers []string
44+
if all {
45+
instances, err := client.Instances.List(ctx)
4046
if err != nil {
41-
fmt.Printf("Error: %v\n", err)
42-
lastErr = err
43-
continue
47+
return fmt.Errorf("failed to list instances: %w", err)
48+
}
49+
for _, inst := range *instances {
50+
identifiers = append(identifiers, inst.ID)
51+
}
52+
if len(identifiers) == 0 {
53+
fmt.Println("No instances to remove")
54+
return nil
55+
}
56+
} else {
57+
identifiers = args
58+
}
59+
60+
var lastErr error
61+
for _, identifier := range identifiers {
62+
// Resolve instance by ID, partial ID, or name (skip if --all since we have full IDs)
63+
var instanceID string
64+
var err error
65+
if all {
66+
instanceID = identifier
67+
} else {
68+
instanceID, err = ResolveInstance(ctx, &client, identifier)
69+
if err != nil {
70+
fmt.Printf("Error: %v\n", err)
71+
lastErr = err
72+
continue
73+
}
4474
}
4575

4676
// Check instance state if not forcing
@@ -57,6 +87,10 @@ func handleRm(ctx context.Context, cmd *cli.Command) error {
5787
}
5888

5989
if inst.State == "Running" {
90+
if all {
91+
// Silently skip running instances when using --all without --force
92+
continue
93+
}
6094
fmt.Printf("Error: cannot remove running instance %s. Stop it first or use --force\n", instanceID)
6195
lastErr = fmt.Errorf("instance is running")
6296
continue

0 commit comments

Comments
 (0)