Skip to content

Commit f6b6326

Browse files
committed
Check that input is a tty before using it
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 <[email protected]>
1 parent 4a4a3dd commit f6b6326

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
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) {

pkg/uiutil/uiutil.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ 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+
// This setting is needed so we can write integration tests for the TTY input.
48+
// It is probably not useful otherwise.
49+
if os.Getenv("_LIMA_INPUT_IS_TTY") != "" {
50+
return true
51+
}
52+
return reader == os.Stdin && (isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd()))
53+
}
54+
4455
// OutputIsTTY returns true if writer is going to stdout, and stdout is a terminal device,
4556
// not a regular file, stream, or pipe etc.
4657
func OutputIsTTY(writer io.Writer) bool {

0 commit comments

Comments
 (0)