From a5e9bd5bc6e066848cb6bd12d53af76486798c92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Sun, 30 Nov 2025 10:10:35 +0100 Subject: [PATCH 1/3] Check tty flag also for the shell command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anders F Björklund --- cmd/limactl/shell.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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 } From 4a0287ec17d02cb4e6c8fc6b292f953919f9b2ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Sun, 30 Nov 2025 10:11:11 +0100 Subject: [PATCH 2/3] Check that input is a tty before using it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current UI library will crash, if trying to read input from something that is not a terminal (a TTY). Signed-off-by: Anders F Björklund --- cmd/limactl/clone.go | 2 +- cmd/limactl/edit.go | 12 ++++++++---- pkg/uiutil/uiutil.go | 6 ++++++ 3 files changed, 15 insertions(+), 5 deletions(-) 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/pkg/uiutil/uiutil.go b/pkg/uiutil/uiutil.go index 91e64ceb419..12a7fbf6868 100644 --- a/pkg/uiutil/uiutil.go +++ b/pkg/uiutil/uiutil.go @@ -41,6 +41,12 @@ func Select(message string, options []string) (int, error) { return ans, nil } +// InputIsTTY returns true if reader is coming from stdin, and stdin is a terminal device, +// not a regular file, stream, or pipe etc. +func InputIsTTY(reader io.Reader) bool { + return reader == os.Stdin && (isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd())) +} + // OutputIsTTY returns true if writer is going to stdout, and stdout is a terminal device, // not a regular file, stream, or pipe etc. func OutputIsTTY(writer io.Writer) bool { From 42b77c17edf982b3de2fb374f4d71094572a962f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Wed, 17 Dec 2025 07:36:54 +0100 Subject: [PATCH 3/3] Add bats test for the shell tty fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anders F Björklund --- hack/bats/tests/shell.bats | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 hack/bats/tests/shell.bats 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