Skip to content

Commit 3a85c7f

Browse files
committed
Add rm
1 parent cbb8eb8 commit 3a85c7f

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

pkg/cmd/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func init() {
7272
&runCmd,
7373
&psCmd,
7474
&logsCmd,
75+
&rmCmd,
7576
{
7677
Name: "health",
7778
Category: "API RESOURCE",

pkg/cmd/rm.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/onkernel/hypeman-go"
8+
"github.com/onkernel/hypeman-go/option"
9+
"github.com/urfave/cli/v3"
10+
)
11+
12+
var rmCmd = cli.Command{
13+
Name: "rm",
14+
Usage: "Remove one or more instances",
15+
ArgsUsage: "<instance> [instance...]",
16+
Flags: []cli.Flag{
17+
&cli.BoolFlag{
18+
Name: "force",
19+
Aliases: []string{"f"},
20+
Usage: "Force removal of running instances",
21+
},
22+
},
23+
Action: handleRm,
24+
HideHelpCommand: true,
25+
}
26+
27+
func handleRm(ctx context.Context, cmd *cli.Command) error {
28+
args := cmd.Args().Slice()
29+
if len(args) < 1 {
30+
return fmt.Errorf("instance ID required\nUsage: hypeman rm [flags] <instance> [instance...]")
31+
}
32+
33+
force := cmd.Bool("force")
34+
client := hypeman.NewClient(getDefaultRequestOptions(cmd)...)
35+
36+
var lastErr error
37+
for _, instanceID := range args {
38+
// Check instance state if not forcing
39+
if !force {
40+
inst, err := client.Instances.Get(
41+
ctx,
42+
instanceID,
43+
option.WithMiddleware(debugMiddleware(cmd.Root().Bool("debug"))),
44+
)
45+
if err != nil {
46+
fmt.Printf("Error: failed to get instance %s: %v\n", instanceID, err)
47+
lastErr = err
48+
continue
49+
}
50+
51+
if inst.State == "Running" {
52+
fmt.Printf("Error: cannot remove running instance %s. Stop it first or use --force\n", instanceID)
53+
lastErr = fmt.Errorf("instance is running")
54+
continue
55+
}
56+
}
57+
58+
// Delete the instance
59+
err := client.Instances.Delete(
60+
ctx,
61+
instanceID,
62+
option.WithMiddleware(debugMiddleware(cmd.Root().Bool("debug"))),
63+
)
64+
if err != nil {
65+
fmt.Printf("Error: failed to remove instance %s: %v\n", instanceID, err)
66+
lastErr = err
67+
continue
68+
}
69+
70+
fmt.Println(instanceID)
71+
}
72+
73+
return lastErr
74+
}
75+

0 commit comments

Comments
 (0)