-
Notifications
You must be signed in to change notification settings - Fork 224
/
list.go
149 lines (119 loc) · 3.27 KB
/
list.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package machine
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
)
func newList() *cobra.Command {
const (
short = "List Fly machines"
long = short + "\n"
usage = "list"
)
cmd := command.New(usage, short, long, runMachineList,
command.RequireSession,
command.RequireAppName,
)
cmd.Aliases = []string{"ls"}
cmd.Args = cobra.NoArgs
flag.Add(
cmd,
flag.App(),
flag.AppConfig(),
flag.JSONOutput(),
flag.Bool{
Name: "quiet",
Shorthand: "q",
Description: "Only list machine ids",
},
)
return cmd
}
func runMachineList(ctx context.Context) (err error) {
var (
appName = appconfig.NameFromContext(ctx)
client = client.FromContext(ctx).API()
io = iostreams.FromContext(ctx)
silence = flag.GetBool(ctx, "quiet")
cfg = config.FromContext(ctx)
)
app, err := client.GetAppCompact(ctx, appName)
if err != nil {
help := newList().Help()
if help != nil {
fmt.Println(help)
}
fmt.Println()
return err
}
flapsClient, err := flaps.New(ctx, app)
if err != nil {
return fmt.Errorf("list of machines could not be retrieved: %w", err)
}
machines, err := flapsClient.List(ctx, "")
if err != nil {
return fmt.Errorf("machines could not be retrieved")
}
if len(machines) == 0 {
if !silence {
fmt.Fprintf(io.Out, "No machines are available on this app %s\n", appName)
}
return nil
}
if cfg.JSONOutput {
return render.JSON(io.Out, machines)
}
rows := [][]string{}
listOfMachinesLink := io.CreateLink("View them in the UI here", fmt.Sprintf("https://fly.io/apps/%s/machines/", appName))
if !silence {
fmt.Fprintf(io.Out, "%d machines have been retrieved from app %s.\n%s\n\n", len(machines), appName, listOfMachinesLink)
}
if silence {
for _, machine := range machines {
rows = append(rows, []string{machine.ID})
}
_ = render.Table(io.Out, "", rows)
} else {
for _, machine := range machines {
var volName string
if machine.Config != nil && len(machine.Config.Mounts) > 0 {
volName = machine.Config.Mounts[0].Volume
}
appPlatform := ""
machineProcessGroup := ""
if machine.Config != nil {
if platformVersion, ok := machine.Config.Metadata[api.MachineConfigMetadataKeyFlyPlatformVersion]; ok {
appPlatform = platformVersion
}
if processGroup := machine.ProcessGroup(); processGroup != "" {
machineProcessGroup = processGroup
}
}
rows = append(rows, []string{
machine.ID,
machine.Name,
machine.State,
machine.Region,
machine.ImageRefWithVersion(),
machine.PrivateIP,
volName,
machine.CreatedAt,
machine.UpdatedAt,
appPlatform,
machineProcessGroup,
fmt.Sprintf("%s:%dMB", machine.Config.Guest.ToSize(), machine.Config.Guest.MemoryMB),
})
}
_ = render.Table(io.Out, appName, rows, "ID", "Name", "State", "Region", "Image", "IP Address", "Volume", "Created", "Last Updated", "App Platform", "Process Group", "Size")
}
return nil
}