-
Notifications
You must be signed in to change notification settings - Fork 224
/
show.go
133 lines (112 loc) · 2.98 KB
/
show.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package scale
import (
"context"
"encoding/json"
"fmt"
"strconv"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"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 newScaleShow() *cobra.Command {
const (
short = "Show current resources"
long = `Show current VM size and counts`
)
cmd := command.New("show", short, long, runScaleShow,
command.RequireSession,
command.RequireAppName,
failOnMachinesApp,
)
cmd.Args = cobra.NoArgs
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)
return cmd
}
func runScaleShow(ctx context.Context) error {
apiClient := client.FromContext(ctx).API()
appName := appconfig.NameFromContext(ctx)
size, tgCounts, processGroups, err := apiClient.AppVMResources(ctx, appName)
if err != nil {
return err
}
countMsg := countMessage(tgCounts)
maxPerRegionMsg := maxPerRegionMessage(processGroups)
printVMResources(ctx, size, countMsg, maxPerRegionMsg, processGroups)
return nil
}
func countMessage(counts []api.TaskGroupCount) string {
msg := ""
if len(counts) == 1 {
for _, tg := range counts {
if tg.Name == "app" {
msg = fmt.Sprint(tg.Count)
}
}
}
if msg == "" {
for _, tg := range counts {
msg += fmt.Sprintf("%s=%d ", tg.Name, tg.Count)
}
}
return msg
}
func maxPerRegionMessage(groups []api.ProcessGroup) string {
msg := ""
if len(groups) == 1 {
for _, pg := range groups {
if pg.Name == "app" {
if pg.MaxPerRegion == 0 {
msg = "Not set"
} else {
msg = fmt.Sprint(pg.MaxPerRegion)
}
}
}
}
if msg == "" {
for _, pg := range groups {
msg += fmt.Sprintf("%s=%d ", pg.Name, pg.MaxPerRegion)
}
}
return msg
}
func printVMResources(ctx context.Context, vmSize api.VMSize, count string, maxPerRegion string, processGroups []api.ProcessGroup) {
io := iostreams.FromContext(ctx)
appName := appconfig.NameFromContext(ctx)
if flag.GetBool(ctx, "json") {
out := struct {
api.VMSize
Count string
MaxPerRegion string
}{
VMSize: vmSize,
Count: count,
MaxPerRegion: maxPerRegion,
}
prettyJSON, _ := json.MarshalIndent(out, "", " ")
fmt.Fprintln(io.Out, string(prettyJSON))
return
}
fmt.Fprintf(io.Out, "VM Resources for %s\n", appName)
if len(processGroups) <= 1 {
fmt.Fprintf(io.Out, "%15s: %s\n", "VM Size", vmSize.Name)
fmt.Fprintf(io.Out, "%15s: %s\n", "VM Memory", formatMemory(vmSize))
}
fmt.Fprintf(io.Out, "%15s: %s\n", "Count", count)
fmt.Fprintf(io.Out, "%15s: %s\n", "Max Per Region", maxPerRegion)
if len(processGroups) > 1 {
for _, pg := range processGroups {
fmt.Fprintf(io.Out, "\nProcess group %s\n", pg.Name)
fmt.Fprintf(io.Out, "%15s: %s\n", "VM Size", pg.VMSize.Name)
fmt.Fprintf(io.Out, "%15s: %s\n", "VM Memory", formatMemory(*pg.VMSize))
fmt.Fprintf(io.Out, "%15s: %s\n", "Max Per Region", strconv.Itoa(pg.MaxPerRegion))
}
}
}