-
Notifications
You must be signed in to change notification settings - Fork 221
/
vmsizes.go
70 lines (56 loc) · 1.49 KB
/
vmsizes.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
package platform
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/internal/cli/internal/command"
"github.com/superfly/flyctl/internal/cli/internal/config"
"github.com/superfly/flyctl/internal/cli/internal/render"
"github.com/superfly/flyctl/internal/client"
"github.com/superfly/flyctl/pkg/iostreams"
)
func newVMSizes() (cmd *cobra.Command) {
const (
long = `View a list of VM sizes which can be used with the FLYCTL SCALE VM command
`
short = "List VM Sizes"
)
cmd = command.New("vm-sizes", short, long, runVMSizes,
command.RequireSession,
)
cmd.Args = cobra.NoArgs
return
}
func runVMSizes(ctx context.Context) error {
client := client.FromContext(ctx).API()
sizes, err := client.PlatformVMSizes(ctx)
if err != nil {
return fmt.Errorf("failed retrieving sizes: %w", err)
}
out := iostreams.FromContext(ctx).Out
if config.FromContext(ctx).JSONOutput {
return render.JSON(out, sizes)
}
var rows [][]string
for _, size := range sizes {
rows = append(rows, []string{
size.Name,
cores(size),
memory(size),
})
}
return render.Table(out, "", rows, "Name", "CPU Cores", "Memory")
}
func cores(size api.VMSize) string {
if size.CPUCores < 1.0 {
return fmt.Sprintf("%.2f", size.CPUCores)
}
return fmt.Sprintf("%d", int(size.CPUCores))
}
func memory(size api.VMSize) string {
if size.MemoryGB < 1.0 {
return fmt.Sprintf("%d MB", size.MemoryMB)
}
return fmt.Sprintf("%d GB", int(size.MemoryGB))
}