Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/limactl/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/limactl/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions cmd/limactl/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] == "--" {
Expand Down Expand Up @@ -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
}
Expand Down
31 changes: 31 additions & 0 deletions hack/bats/tests/shell.bats
Original file line number Diff line number Diff line change
@@ -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 </dev/null
}

@test 'yes | stopped lima instance' {
# check that stdin is verified and not just crashing
bash -c "yes | limactl shell --tty=true $NAME true"
}

@test 'delete dummy instance' {
run_e -0 limactl delete --force "$NAME"
assert_info "Deleted \"${NAME}\"…"
}
6 changes: 6 additions & 0 deletions pkg/uiutil/uiutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down