-
Notifications
You must be signed in to change notification settings - Fork 221
/
list.go
104 lines (89 loc) · 2.39 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
package machine
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/internal/app"
"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.LoadAppNameIfPresent,
)
cmd.Aliases = []string{"ls"}
cmd.Args = cobra.NoArgs
flag.Add(
cmd,
flag.App(),
flag.AppConfig(),
flag.Bool{
Name: "quiet",
Shorthand: "q",
Description: "Only list machine ids",
},
)
return cmd
}
func runMachineList(ctx context.Context) (err error) {
var (
appName = app.NameFromContext(ctx)
client = client.FromContext(ctx).API()
io = iostreams.FromContext(ctx)
silence = flag.GetBool(ctx, "quiet")
cfg = config.FromContext(ctx)
)
if appName == "" {
return fmt.Errorf("app is not found")
}
app, err := client.GetAppCompact(ctx, appName)
if err != nil {
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 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))
fmt.Fprintf(io.Out, "%d machines have been retrieved.\n%s\n\n", len(machines), listOfMachinesLink)
if silence {
for _, machine := range machines {
rows = append(rows, []string{machine.ID})
}
_ = render.Table(io.Out, appName, rows, "ID")
} else {
for _, machine := range machines {
rows = append(rows, []string{
machine.ID,
fmt.Sprintf("%s:%s", machine.ImageRef.Repository, machine.ImageRef.Tag),
machine.CreatedAt,
machine.UpdatedAt,
machine.State,
machine.Region,
machine.Name,
machine.PrivateIP,
})
}
_ = render.Table(io.Out, appName, rows, "ID", "Image", "Created", "Last Updated", "State", "Region", "Name", "IP Address")
}
return nil
}