-
Notifications
You must be signed in to change notification settings - Fork 234
/
stop.go
50 lines (40 loc) · 1.02 KB
/
stop.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
package vm
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/iostreams"
)
func newStop() *cobra.Command {
const (
short = "Stop a VM"
long = "Request for a VM to be asynchronously stopped"
usage = "stop <vm-id>"
)
cmd := command.New(usage, short, long, runStop,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.ExactArgs(1)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)
return cmd
}
func runStop(ctx context.Context) (err error) {
var (
io = iostreams.FromContext(ctx)
appName = appconfig.NameFromContext(ctx)
client = client.FromContext(ctx).API()
)
if err := client.StopAllocation(ctx, appName, flag.FirstArg(ctx)); err != nil {
return fmt.Errorf("failed to stop allocation: %w", err)
}
fmt.Fprintf(io.Out, "VM %s is being stopped\n", flag.FirstArg(ctx))
return
}