diff --git a/cmd/limactl/clone.go b/cmd/limactl/clone.go index 06a95f68c15..9afe6f9ce42 100644 --- a/cmd/limactl/clone.go +++ b/cmd/limactl/clone.go @@ -121,7 +121,7 @@ func cloneOrRenameAction(cmd *cobra.Command, args []string) error { } if tty && !flags.Changed("start") { - start, err = askWhetherToStart() + start, err = askWhetherToStart(cmd) if err != nil { return err } diff --git a/cmd/limactl/edit.go b/cmd/limactl/edit.go index f2212fe89d3..b13fea4b919 100644 --- a/cmd/limactl/edit.go +++ b/cmd/limactl/edit.go @@ -153,7 +153,7 @@ func editAction(cmd *cobra.Command, args []string) error { } if tty && !flags.Changed("start") { - start, err = askWhetherToStart() + start, err = askWhetherToStart(cmd) if err != nil { return err } @@ -180,9 +180,13 @@ func editAction(cmd *cobra.Command, args []string) error { return instance.Start(ctx, inst, false, false) } -func askWhetherToStart() (bool, error) { - message := "Do you want to start the instance now? " - return uiutil.Confirm(message, true) +func askWhetherToStart(cmd *cobra.Command) (bool, error) { + isTTY := uiutil.InputIsTTY(cmd.InOrStdin()) + if isTTY { + message := "Do you want to start the instance now? " + return uiutil.Confirm(message, true) + } + return false, nil } func editBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { diff --git a/cmd/limactl/shell.go b/cmd/limactl/shell.go index 188063d9801..66c56c4af13 100644 --- a/cmd/limactl/shell.go +++ b/cmd/limactl/shell.go @@ -70,6 +70,10 @@ func newShellCommand() *cobra.Command { func shellAction(cmd *cobra.Command, args []string) error { ctx := cmd.Context() flags := cmd.Flags() + tty, err := flags.GetBool("tty") + if err != nil { + return err + } // simulate the behavior of double dash newArg := []string{} if len(args) >= 2 && args[1] == "--" { @@ -106,8 +110,8 @@ func shellAction(cmd *cobra.Command, args []string) error { return err } - if !flags.Changed("start") { - startNow, err = askWhetherToStart() + if tty && !flags.Changed("start") { + startNow, err = askWhetherToStart(cmd) if err != nil { return err } diff --git a/hack/bats/tests/shell.bats b/hack/bats/tests/shell.bats new file mode 100644 index 00000000000..f65a10f93ab --- /dev/null +++ b/hack/bats/tests/shell.bats @@ -0,0 +1,31 @@ +# SPDX-FileCopyrightText: Copyright The Lima Authors +# SPDX-License-Identifier: Apache-2.0 + +load "../helpers/load" + +NAME=dummy + +local_setup_file() { + for INSTANCE in "$NAME"; do + limactl delete --force "$INSTANCE" || : + done +} + +@test 'create dummy instance' { + run -0 create_dummy_instance "$NAME" '.disk = "1M"' +} + +@test 'lima stopped lima instance' { + # check that the "tty" flag is used, also for stdin + limactl shell --tty=false "$NAME" true