-
Notifications
You must be signed in to change notification settings - Fork 240
/
suspend.go
71 lines (54 loc) · 1.61 KB
/
suspend.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
package cmd
import (
"fmt"
"os"
"time"
"github.com/superfly/flyctl/cmdctx"
"github.com/briandowns/spinner"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/docstrings"
)
//TODO: Move all output to status styled begin/done updates
func newSuspendCommand() *Command {
suspendStrings := docstrings.Get("suspend")
suspendCmd := BuildCommandKS(nil, runSuspend, suspendStrings, os.Stdout, requireSession, requireAppNameAsArg)
suspendCmd.Args = cobra.RangeArgs(0, 1)
return suspendCmd
}
func runSuspend(ctx *cmdctx.CmdContext) error {
// appName := ctx.Args[0]
// fmt.Println(appName, len(ctx.Args))
// if appName == "" {
// appName = ctx.AppName
// }
appName := ctx.AppName
_, err := ctx.Client.API().SuspendApp(appName)
if err != nil {
return err
}
appstatus, err := ctx.Client.API().GetAppStatus(appName, false)
if err != nil {
return err
}
fmt.Printf("%s is now %s\n", appstatus.Name, appstatus.Status)
allocount := len(appstatus.Allocations)
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
s.Writer = os.Stderr
s.Prefix = fmt.Sprintf("Suspending %s with %d instances to stop ", appstatus.Name, allocount)
s.Start()
for allocount > 0 {
plural := ""
if allocount > 1 {
plural = "s"
}
s.Prefix = fmt.Sprintf("Suspending %s with %d instance%s to stop ", appstatus.Name, allocount, plural)
appstatus, err = ctx.Client.API().GetAppStatus(ctx.AppName, false)
if err != nil {
return err
}
allocount = len(appstatus.Allocations)
}
s.FinalMSG = fmt.Sprintf("Suspend complete - %s is now suspended with no running instances\n", appstatus.Name)
s.Stop()
return nil
}