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 @@ -100,8 +104,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
11 changes: 11 additions & 0 deletions pkg/uiutil/uiutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ 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 {
// This setting is needed so we can write integration tests for the TTY input.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test seems missing?

// It is probably not useful otherwise.
if os.Getenv("_LIMA_INPUT_IS_TTY") != "" {
return true
}
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
Loading