@@ -13,15 +13,19 @@ import (
13
13
"github.com/superfly/flyctl/internal/appconfig"
14
14
"github.com/superfly/flyctl/internal/flag"
15
15
"github.com/superfly/flyctl/internal/prompt"
16
+ "github.com/superfly/flyctl/iostreams"
16
17
)
17
18
19
+ // We now prompt for a machine automatically when no machine IDs are
20
+ // provided. This flag is retained for backward compatability.
18
21
var selectFlag = flag.Bool {
19
22
Name : "select" ,
20
23
Description : "Select from a list of machines" ,
24
+ Hidden : true ,
21
25
}
22
26
23
27
func selectOneMachine (ctx context.Context , app * api.AppCompact , machineID string , haveMachineID bool ) (* api.Machine , context.Context , error ) {
24
- if err := checkSelectCmdline (ctx , haveMachineID ); err != nil {
28
+ if err := checkSelectConditions (ctx , haveMachineID ); err != nil {
25
29
return nil , nil , err
26
30
}
27
31
@@ -36,7 +40,7 @@ func selectOneMachine(ctx context.Context, app *api.AppCompact, machineID string
36
40
}
37
41
38
42
var machine * api.Machine
39
- if flag . GetBool (ctx , "select" ) {
43
+ if shouldPrompt (ctx , haveMachineID ) {
40
44
machine , err = promptForOneMachine (ctx )
41
45
if err != nil {
42
46
return nil , nil , err
@@ -55,7 +59,7 @@ func selectOneMachine(ctx context.Context, app *api.AppCompact, machineID string
55
59
56
60
func selectManyMachines (ctx context.Context , machineIDs []string ) ([]* api.Machine , context.Context , error ) {
57
61
haveMachineIDs := len (machineIDs ) > 0
58
- if err := checkSelectCmdline (ctx , haveMachineIDs ); err != nil {
62
+ if err := checkSelectConditions (ctx , haveMachineIDs ); err != nil {
59
63
return nil , nil , err
60
64
}
61
65
@@ -65,7 +69,7 @@ func selectManyMachines(ctx context.Context, machineIDs []string) ([]*api.Machin
65
69
}
66
70
67
71
var machines []* api.Machine
68
- if flag . GetBool (ctx , "select" ) {
72
+ if shouldPrompt (ctx , haveMachineIDs ) {
69
73
machines , err = promptForManyMachines (ctx )
70
74
if err != nil {
71
75
return nil , nil , err
@@ -88,7 +92,7 @@ func selectManyMachines(ctx context.Context, machineIDs []string) ([]*api.Machin
88
92
89
93
func selectManyMachineIDs (ctx context.Context , machineIDs []string ) ([]string , context.Context , error ) {
90
94
haveMachineIDs := len (machineIDs ) > 0
91
- if err := checkSelectCmdline (ctx , haveMachineIDs ); err != nil {
95
+ if err := checkSelectConditions (ctx , haveMachineIDs ); err != nil {
92
96
return nil , nil , err
93
97
}
94
98
@@ -97,7 +101,7 @@ func selectManyMachineIDs(ctx context.Context, machineIDs []string) ([]string, c
97
101
return nil , nil , err
98
102
}
99
103
100
- if flag . GetBool (ctx , "select" ) {
104
+ if shouldPrompt (ctx , haveMachineIDs ) {
101
105
// NOTE: machineIDs must be empty in this case.
102
106
machines , err := promptForManyMachines (ctx )
103
107
if err != nil {
@@ -235,17 +239,23 @@ func rewriteMachineNotFoundErrors(ctx context.Context, err error, machineID stri
235
239
}
236
240
}
237
241
238
- func checkSelectCmdline (ctx context.Context , haveMachineIDs bool ) error {
242
+ func checkSelectConditions (ctx context.Context , haveMachineIDs bool ) error {
239
243
haveSelectFlag := flag .GetBool (ctx , "select" )
240
244
appName := appconfig .NameFromContext (ctx )
241
245
switch {
242
246
case haveSelectFlag && haveMachineIDs :
243
247
return errors .New ("machine IDs can't be used with --select" )
244
- case ! haveSelectFlag && ! haveMachineIDs :
245
- return errors .New ("a machine ID must be provided unless --select is used" )
246
248
case haveSelectFlag && appName == "" :
247
249
return errors .New ("an app name must be specified to use --select" )
250
+ case ! haveMachineIDs && appName == "" :
251
+ return errors .New ("a machine ID or an app name is required" )
252
+ case shouldPrompt (ctx , haveMachineIDs ) && ! iostreams .FromContext (ctx ).IsInteractive ():
253
+ return errors .New ("a machine ID must be specified when not running interactively" )
248
254
default :
249
255
return nil
250
256
}
251
257
}
258
+
259
+ func shouldPrompt (ctx context.Context , haveMachineIDs bool ) bool {
260
+ return flag .GetBool (ctx , "select" ) || ! haveMachineIDs
261
+ }
0 commit comments