-
Notifications
You must be signed in to change notification settings - Fork 226
/
select.go
261 lines (233 loc) · 7.63 KB
/
select.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package machine
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"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/flag"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/iostreams"
)
// We now prompt for a machine automatically when no machine IDs are
// provided. This flag is retained for backward compatability.
var selectFlag = flag.Bool{
Name: "select",
Description: "Select from a list of machines",
Hidden: true,
}
func selectOneMachine(ctx context.Context, app *api.AppCompact, machineID string, haveMachineID bool) (*api.Machine, context.Context, error) {
if err := checkSelectConditions(ctx, haveMachineID); err != nil {
return nil, nil, err
}
var err error
if app != nil {
ctx, err = buildContextFromApp(ctx, app)
} else {
ctx, err = buildContextFromAppNameOrMachineID(ctx, machineID)
}
if err != nil {
return nil, nil, err
}
var machine *api.Machine
if shouldPrompt(ctx, haveMachineID) {
machine, err = promptForOneMachine(ctx)
if err != nil {
return nil, nil, err
}
} else {
machine, err = flaps.FromContext(ctx).Get(ctx, machineID)
if err != nil {
if err := rewriteMachineNotFoundErrors(ctx, err, machineID); err != nil {
return nil, nil, err
}
return nil, nil, fmt.Errorf("could not get machine %s: %w", machineID, err)
}
}
return machine, ctx, nil
}
func selectManyMachines(ctx context.Context, machineIDs []string) ([]*api.Machine, context.Context, error) {
haveMachineIDs := len(machineIDs) > 0
if err := checkSelectConditions(ctx, haveMachineIDs); err != nil {
return nil, nil, err
}
ctx, err := buildContextFromAppNameOrMachineID(ctx, machineIDs...)
if err != nil {
return nil, nil, err
}
var machines []*api.Machine
if shouldPrompt(ctx, haveMachineIDs) {
machines, err = promptForManyMachines(ctx)
if err != nil {
return nil, nil, err
}
} else {
flapsClient := flaps.FromContext(ctx)
for _, machineID := range machineIDs {
machine, err := flapsClient.Get(ctx, machineID)
if err != nil {
if err := rewriteMachineNotFoundErrors(ctx, err, machineID); err != nil {
return nil, nil, err
}
return nil, nil, fmt.Errorf("could not get machine %s: %w", machineID, err)
}
machines = append(machines, machine)
}
}
return machines, ctx, nil
}
func selectManyMachineIDs(ctx context.Context, machineIDs []string) ([]string, context.Context, error) {
haveMachineIDs := len(machineIDs) > 0
if err := checkSelectConditions(ctx, haveMachineIDs); err != nil {
return nil, nil, err
}
ctx, err := buildContextFromAppNameOrMachineID(ctx, machineIDs...)
if err != nil {
return nil, nil, err
}
if shouldPrompt(ctx, haveMachineIDs) {
// NOTE: machineIDs must be empty in this case.
machines, err := promptForManyMachines(ctx)
if err != nil {
return nil, nil, err
}
for _, machine := range machines {
machineIDs = append(machineIDs, machine.ID)
}
}
return machineIDs, ctx, nil
}
func buildContextFromApp(ctx context.Context, app *api.AppCompact) (context.Context, error) {
flapsClient, err := flaps.New(ctx, app)
if err != nil {
return nil, fmt.Errorf("could not create flaps client: %w", err)
}
ctx = flaps.NewContext(ctx, flapsClient)
return ctx, nil
}
func buildContextFromAppNameOrMachineID(ctx context.Context, machineIDs ...string) (context.Context, error) {
var (
appName = appconfig.NameFromContext(ctx)
flapsClient *flaps.Client
err error
)
if appName == "" {
// NOTE: assuming that we validated the command line arguments
// correctly, we must have at least one machine ID when no app
// is set.
client := client.FromContext(ctx).API()
var gqlMachine *api.GqlMachine
gqlMachine, err = client.GetMachine(ctx, machineIDs[0])
if err != nil {
return nil, fmt.Errorf("could not get machine from GraphQL to determine app name: %w", err)
}
ctx = appconfig.WithName(ctx, gqlMachine.App.Name)
flapsClient, err = flaps.New(ctx, gqlMachine.App)
} else {
flapsClient, err = flaps.NewFromAppName(ctx, appName)
}
if err != nil {
return nil, fmt.Errorf("could not create flaps client: %w", err)
}
ctx = flaps.NewContext(ctx, flapsClient)
return ctx, nil
}
func promptForOneMachine(ctx context.Context) (*api.Machine, error) {
machines, err := flaps.FromContext(ctx).List(ctx, "")
if err != nil {
return nil, fmt.Errorf("could not get a list of machines: %w", err)
} else if len(machines) == 0 {
return nil, fmt.Errorf("the app %s has no machines", appconfig.NameFromContext(ctx))
}
options := sortAndBuildOptions(machines)
var selection int
if err := prompt.Select(ctx, &selection, "Select a machine:", "", options...); err != nil {
return nil, fmt.Errorf("could not prompt for machine: %w", err)
}
return machines[selection], nil
}
func promptForManyMachines(ctx context.Context) ([]*api.Machine, error) {
machines, err := flaps.FromContext(ctx).List(ctx, "")
if err != nil {
return nil, fmt.Errorf("could not get a list of machines: %w", err)
} else if len(machines) == 0 {
return nil, fmt.Errorf("the app %s has no machines", appconfig.NameFromContext(ctx))
}
options := sortAndBuildOptions(machines)
var selections []int
if err := prompt.MultiSelect(ctx, &selections, "Select machines:", nil, options...); err != nil {
return nil, fmt.Errorf("could not prompt for machines: %w", err)
}
var selectedMachines []*api.Machine
for _, selection := range selections {
selectedMachines = append(selectedMachines, machines[selection])
}
if len(selectedMachines) == 0 {
return nil, errors.New("no machines selected")
}
return selectedMachines, nil
}
func sortAndBuildOptions(machines []*api.Machine) []string {
sort.Slice(machines, func(i, j int) bool {
return machines[i].ID < machines[j].ID
})
options := []string{}
for _, machine := range machines {
details := fmt.Sprintf("%s, region %s", machine.State, machine.Region)
if group := machine.ProcessGroup(); group != "" {
details += fmt.Sprintf(", process group '%s'", group)
}
role := getMachineRole(machine)
if role != "" {
details += fmt.Sprintf(", role '%s'", role)
}
options = append(options, fmt.Sprintf("%s %s (%s)", machine.ID, machine.Name, details))
}
return options
}
func getMachineRole(machine *api.Machine) string {
if machine.State != api.MachineStateStarted {
return ""
}
for _, check := range machine.Checks {
if check.Name == "role" {
if check.Status == api.Passing {
return check.Output
} else {
return "error"
}
}
}
return ""
}
func rewriteMachineNotFoundErrors(ctx context.Context, err error, machineID string) error {
if strings.Contains(err.Error(), "machine not found") {
appName := appconfig.NameFromContext(ctx)
return fmt.Errorf("machine %s was not found in app '%s'", machineID, appName)
} else {
return nil
}
}
func checkSelectConditions(ctx context.Context, haveMachineIDs bool) error {
haveSelectFlag := flag.GetBool(ctx, "select")
appName := appconfig.NameFromContext(ctx)
switch {
case haveSelectFlag && haveMachineIDs:
return errors.New("machine IDs can't be used with --select")
case haveSelectFlag && appName == "":
return errors.New("an app name must be specified to use --select")
case !haveMachineIDs && appName == "":
return errors.New("a machine ID or an app name is required")
case shouldPrompt(ctx, haveMachineIDs) && !iostreams.FromContext(ctx).IsInteractive():
return errors.New("a machine ID must be specified when not running interactively")
default:
return nil
}
}
func shouldPrompt(ctx context.Context, haveMachineIDs bool) bool {
return flag.GetBool(ctx, "select") || !haveMachineIDs
}