-
Notifications
You must be signed in to change notification settings - Fork 234
/
remove.go
93 lines (76 loc) · 2.06 KB
/
remove.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package machine
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/internal/app"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/iostreams"
)
func newRemove() *cobra.Command {
const (
short = "Remove a Fly machine"
long = short + "\n"
usage = "remove <id>"
)
cmd := command.New(usage, short, long, runMachineRemove,
command.RequireSession,
command.LoadAppNameIfPresent,
)
cmd.Aliases = []string{"rm"}
flag.Add(
cmd,
flag.App(),
flag.AppConfig(),
flag.Bool{
Name: "force",
Shorthand: "f",
Description: "force kill machine if it's running",
},
)
cmd.Args = cobra.MinimumNArgs(1)
return cmd
}
func runMachineRemove(ctx context.Context) (err error) {
var (
appName = app.NameFromContext(ctx)
out = iostreams.FromContext(ctx).Out
machineID = flag.FirstArg(ctx)
input = api.RemoveMachineInput{
AppID: app.NameFromContext(ctx),
ID: machineID,
Kill: flag.GetBool(ctx, "force"),
}
)
app, err := appFromMachineOrName(ctx, machineID, appName)
if err != nil {
return err
}
flapsClient, err := flaps.New(ctx, app)
if err != nil {
return fmt.Errorf("could not make flaps client: %w", err)
}
// check if machine even exists
current, err := flapsClient.Get(ctx, machineID)
if err != nil {
return fmt.Errorf("could not retrieve machine %s", machineID)
}
switch current.State {
case "destroyed":
return fmt.Errorf("machine %s has already been destroyed", machineID)
case "started":
if !input.Kill {
return fmt.Errorf("machine %s currently started, either stop first or use --force flag", machineID)
}
}
fmt.Fprintf(out, "machine %s was found and is currently in %s state, attempting to destroy...\n", machineID, current.State)
err = flapsClient.Destroy(ctx, input)
if err != nil {
return fmt.Errorf("could not destroy machine %s: %w", machineID, err)
}
fmt.Fprintf(out, "%s has been destroyed\n", machineID)
return
}