2025-06-19 11:31:47 +02:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
|
|
// Package prompt provides a simple way to prompt the user for input.
|
|
|
|
|
package prompt
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2025-09-12 12:33:46 -07:00
|
|
|
"os"
|
2025-06-19 11:31:47 +02:00
|
|
|
"strings"
|
2025-09-12 12:33:46 -07:00
|
|
|
|
|
|
|
|
"github.com/mattn/go-isatty"
|
2025-06-19 11:31:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// YesNo takes a question and prompts the user to answer the
|
|
|
|
|
// question with a yes or no. It appends a [y/n] to the message.
|
2025-09-12 12:33:46 -07:00
|
|
|
//
|
|
|
|
|
// If there is no TTY on both Stdin and Stdout, assume that we're in a script
|
|
|
|
|
// and return the dflt result.
|
|
|
|
|
func YesNo(msg string, dflt bool) bool {
|
|
|
|
|
if !(isatty.IsTerminal(os.Stdin.Fd()) && isatty.IsTerminal(os.Stdout.Fd())) {
|
|
|
|
|
return dflt
|
|
|
|
|
}
|
|
|
|
|
if dflt {
|
|
|
|
|
fmt.Print(msg + " [Y/n] ")
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Print(msg + " [y/N] ")
|
|
|
|
|
}
|
2025-06-19 11:31:47 +02:00
|
|
|
var resp string
|
|
|
|
|
fmt.Scanln(&resp)
|
|
|
|
|
resp = strings.ToLower(resp)
|
|
|
|
|
switch resp {
|
|
|
|
|
case "y", "yes", "sure":
|
|
|
|
|
return true
|
2025-09-12 12:33:46 -07:00
|
|
|
case "":
|
|
|
|
|
return dflt
|
2025-06-19 11:31:47 +02:00
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|