Skip to content

Commit cb0e125

Browse files
authored
Merge pull request #4416 from afbjorklund/tty-input
Check that input is a tty before using it
2 parents 78d5e79 + 42b77c1 commit cb0e125

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

cmd/limactl/clone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func cloneOrRenameAction(cmd *cobra.Command, args []string) error {
121121
}
122122

123123
if tty && !flags.Changed("start") {
124-
start, err = askWhetherToStart()
124+
start, err = askWhetherToStart(cmd)
125125
if err != nil {
126126
return err
127127
}

cmd/limactl/edit.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func editAction(cmd *cobra.Command, args []string) error {
153153
}
154154

155155
if tty && !flags.Changed("start") {
156-
start, err = askWhetherToStart()
156+
start, err = askWhetherToStart(cmd)
157157
if err != nil {
158158
return err
159159
}
@@ -180,9 +180,13 @@ func editAction(cmd *cobra.Command, args []string) error {
180180
return instance.Start(ctx, inst, false, false)
181181
}
182182

183-
func askWhetherToStart() (bool, error) {
184-
message := "Do you want to start the instance now? "
185-
return uiutil.Confirm(message, true)
183+
func askWhetherToStart(cmd *cobra.Command) (bool, error) {
184+
isTTY := uiutil.InputIsTTY(cmd.InOrStdin())
185+
if isTTY {
186+
message := "Do you want to start the instance now? "
187+
return uiutil.Confirm(message, true)
188+
}
189+
return false, nil
186190
}
187191

188192
func editBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {

cmd/limactl/shell.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ func newShellCommand() *cobra.Command {
7070
func shellAction(cmd *cobra.Command, args []string) error {
7171
ctx := cmd.Context()
7272
flags := cmd.Flags()
73+
tty, err := flags.GetBool("tty")
74+
if err != nil {
75+
return err
76+
}
7377
// simulate the behavior of double dash
7478
newArg := []string{}
7579
if len(args) >= 2 && args[1] == "--" {
@@ -106,8 +110,8 @@ func shellAction(cmd *cobra.Command, args []string) error {
106110
return err
107111
}
108112

109-
if !flags.Changed("start") {
110-
startNow, err = askWhetherToStart()
113+
if tty && !flags.Changed("start") {
114+
startNow, err = askWhetherToStart(cmd)
111115
if err != nil {
112116
return err
113117
}

hack/bats/tests/shell.bats

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# SPDX-FileCopyrightText: Copyright The Lima Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
load "../helpers/load"
5+
6+
NAME=dummy
7+
8+
local_setup_file() {
9+
for INSTANCE in "$NAME"; do
10+
limactl delete --force "$INSTANCE" || :
11+
done
12+
}
13+
14+
@test 'create dummy instance' {
15+
run -0 create_dummy_instance "$NAME" '.disk = "1M"'
16+
}
17+
18+
@test 'lima stopped lima instance' {
19+
# check that the "tty" flag is used, also for stdin
20+
limactl shell --tty=false "$NAME" true </dev/null
21+
}
22+
23+
@test 'yes | stopped lima instance' {
24+
# check that stdin is verified and not just crashing
25+
bash -c "yes | limactl shell --tty=true $NAME true"
26+
}
27+
28+
@test 'delete dummy instance' {
29+
run_e -0 limactl delete --force "$NAME"
30+
assert_info "Deleted \"${NAME}\""
31+
}

pkg/uiutil/uiutil.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ func Select(message string, options []string) (int, error) {
4141
return ans, nil
4242
}
4343

44+
// InputIsTTY returns true if reader is coming from stdin, and stdin is a terminal device,
45+
// not a regular file, stream, or pipe etc.
46+
func InputIsTTY(reader io.Reader) bool {
47+
return reader == os.Stdin && (isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd()))
48+
}
49+
4450
// OutputIsTTY returns true if writer is going to stdout, and stdout is a terminal device,
4551
// not a regular file, stream, or pipe etc.
4652
func OutputIsTTY(writer io.Writer) bool {

0 commit comments

Comments
 (0)