Skip to content

Commit e80948c

Browse files
authored
Show machine selection menu automatically (#2795)
This obsoletes the `--select` flag.
1 parent 811fab3 commit e80948c

File tree

13 files changed

+32
-22
lines changed

13 files changed

+32
-22
lines changed

internal/command/machine/clone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func newClone() *cobra.Command {
2828
short = "Clone a Fly machine"
2929
long = short + "\n"
3030

31-
usage = "clone <machine_id>"
31+
usage = "clone [machine_id]"
3232
)
3333

3434
cmd := command.New(usage, short, long, runMachineClone,

internal/command/machine/cordon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func newMachineCordon() *cobra.Command {
1515
const (
1616
short = "Deactivate all services on a machine"
1717
long = short + "\n"
18-
usage = "cordon <id> [<id>...]"
18+
usage = "cordon [<id>...]"
1919
)
2020

2121
cmd := command.New(usage, short, long, runMachineCordon,

internal/command/machine/destroy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func newDestroy() *cobra.Command {
2020
long = `Destroy a Fly machine.
2121
This command requires a machine to be in a stopped state unless the force flag is used.
2222
`
23-
usage = "destroy <id>"
23+
usage = "destroy [id]"
2424
)
2525

2626
cmd := command.New(usage, short, long, runMachineDestroy,

internal/command/machine/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func newMachineExec() *cobra.Command {
1919
const (
2020
short = "Execute a command on a machine"
2121
long = short + "\n"
22-
usage = "exec <machine-id> <command>"
22+
usage = "exec [machine-id] <command>"
2323
)
2424

2525
cmd := command.New(usage, short, long, runMachineExec,

internal/command/machine/kill.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func newKill() *cobra.Command {
1616
short = "Kill (SIGKILL) a Fly machine"
1717
long = short + "\n"
1818

19-
usage = "kill <id>"
19+
usage = "kill [id]"
2020
)
2121

2222
cmd := command.New(usage, short, long, runMachineKill,

internal/command/machine/leases.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func newLeaseView() *cobra.Command {
4141
const (
4242
short = "View machine leases"
4343
long = short + "\n"
44-
usage = "view <machine-id>"
44+
usage = "view [machine-id]"
4545
)
4646

4747
cmd := command.New(usage, short, long, runLeaseView,
@@ -66,7 +66,7 @@ func newLeaseClear() *cobra.Command {
6666
const (
6767
short = "Clear machine leases"
6868
long = short + "\n"
69-
usage = "clear <machine-id>"
69+
usage = "clear [machine-id]"
7070
)
7171

7272
cmd := command.New(usage, short, long, runLeaseClear,

internal/command/machine/restart.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func newRestart() *cobra.Command {
1818
short = "Restart one or more Fly machines"
1919
long = short + "\n"
2020

21-
usage = "restart <id> [<id>...]"
21+
usage = "restart [<id>...]"
2222
)
2323

2424
cmd := command.New(usage, short, long, runMachineRestart,

internal/command/machine/select.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ import (
1313
"github.com/superfly/flyctl/internal/appconfig"
1414
"github.com/superfly/flyctl/internal/flag"
1515
"github.com/superfly/flyctl/internal/prompt"
16+
"github.com/superfly/flyctl/iostreams"
1617
)
1718

19+
// We now prompt for a machine automatically when no machine IDs are
20+
// provided. This flag is retained for backward compatability.
1821
var selectFlag = flag.Bool{
1922
Name: "select",
2023
Description: "Select from a list of machines",
24+
Hidden: true,
2125
}
2226

2327
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 {
2529
return nil, nil, err
2630
}
2731

@@ -36,7 +40,7 @@ func selectOneMachine(ctx context.Context, app *api.AppCompact, machineID string
3640
}
3741

3842
var machine *api.Machine
39-
if flag.GetBool(ctx, "select") {
43+
if shouldPrompt(ctx, haveMachineID) {
4044
machine, err = promptForOneMachine(ctx)
4145
if err != nil {
4246
return nil, nil, err
@@ -55,7 +59,7 @@ func selectOneMachine(ctx context.Context, app *api.AppCompact, machineID string
5559

5660
func selectManyMachines(ctx context.Context, machineIDs []string) ([]*api.Machine, context.Context, error) {
5761
haveMachineIDs := len(machineIDs) > 0
58-
if err := checkSelectCmdline(ctx, haveMachineIDs); err != nil {
62+
if err := checkSelectConditions(ctx, haveMachineIDs); err != nil {
5963
return nil, nil, err
6064
}
6165

@@ -65,7 +69,7 @@ func selectManyMachines(ctx context.Context, machineIDs []string) ([]*api.Machin
6569
}
6670

6771
var machines []*api.Machine
68-
if flag.GetBool(ctx, "select") {
72+
if shouldPrompt(ctx, haveMachineIDs) {
6973
machines, err = promptForManyMachines(ctx)
7074
if err != nil {
7175
return nil, nil, err
@@ -88,7 +92,7 @@ func selectManyMachines(ctx context.Context, machineIDs []string) ([]*api.Machin
8892

8993
func selectManyMachineIDs(ctx context.Context, machineIDs []string) ([]string, context.Context, error) {
9094
haveMachineIDs := len(machineIDs) > 0
91-
if err := checkSelectCmdline(ctx, haveMachineIDs); err != nil {
95+
if err := checkSelectConditions(ctx, haveMachineIDs); err != nil {
9296
return nil, nil, err
9397
}
9498

@@ -97,7 +101,7 @@ func selectManyMachineIDs(ctx context.Context, machineIDs []string) ([]string, c
97101
return nil, nil, err
98102
}
99103

100-
if flag.GetBool(ctx, "select") {
104+
if shouldPrompt(ctx, haveMachineIDs) {
101105
// NOTE: machineIDs must be empty in this case.
102106
machines, err := promptForManyMachines(ctx)
103107
if err != nil {
@@ -235,17 +239,23 @@ func rewriteMachineNotFoundErrors(ctx context.Context, err error, machineID stri
235239
}
236240
}
237241

238-
func checkSelectCmdline(ctx context.Context, haveMachineIDs bool) error {
242+
func checkSelectConditions(ctx context.Context, haveMachineIDs bool) error {
239243
haveSelectFlag := flag.GetBool(ctx, "select")
240244
appName := appconfig.NameFromContext(ctx)
241245
switch {
242246
case haveSelectFlag && haveMachineIDs:
243247
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")
246248
case haveSelectFlag && appName == "":
247249
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")
248254
default:
249255
return nil
250256
}
251257
}
258+
259+
func shouldPrompt(ctx context.Context, haveMachineIDs bool) bool {
260+
return flag.GetBool(ctx, "select") || !haveMachineIDs
261+
}

internal/command/machine/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func newStart() *cobra.Command {
1616
short = "Start one or more Fly machines"
1717
long = short + "\n"
1818

19-
usage = "start <id> [<id>...]"
19+
usage = "start [<id>...]"
2020
)
2121

2222
cmd := command.New(usage, short, long, runMachineStart,

internal/command/machine/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func newStatus() *cobra.Command {
1818
short = "Show current status of a running machine"
1919
long = short + "\n"
2020

21-
usage = "status <id>"
21+
usage = "status [id]"
2222
)
2323

2424
cmd := command.New(usage, short, long, runMachineStatus,

0 commit comments

Comments
 (0)