diff options
Diffstat (limited to 'pkg/cli/query.go')
| -rw-r--r-- | pkg/cli/query.go | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/pkg/cli/query.go b/pkg/cli/query.go index 0d367be..40baaec 100644 --- a/pkg/cli/query.go +++ b/pkg/cli/query.go @@ -28,6 +28,26 @@ import ( terminal "golang.org/x/crypto/ssh/terminal" ) +//Confirm displays a yes/no question and returns whether the user answered "yes". +func Confirm(question string) bool { + os.Stdout.Write([]byte(strings.TrimSpace(question) + " [y/n] ")) + + buf := buffer{Input: os.Stdin} + for { + switch string(buf.getNextInput()) { + case "y", "Y": + os.Stdout.Write([]byte("-> yes\n")) + return true + case "n", "N": + os.Stdout.Write([]byte("-> no\n")) + return false + case "\x03": // Ctrl-C + fmt.Fprintln(os.Stderr, "\nInterrupted!") + os.Exit(255) + } + } +} + //Choice is a thing that the user can choose during Query(). type Choice struct { //If given, the choice can be selected by pressing the key that @@ -89,8 +109,6 @@ OUTER: case "\x03": // Ctrl-C fmt.Fprintln(os.Stderr, "Interrupted!") os.Exit(255) - default: - fmt.Printf("%#v\n", string(input)) } //prepare to re-render choices |
