-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
terminal.go
53 lines (43 loc) · 1.07 KB
/
terminal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package terminal
import (
"bufio"
"fmt"
"os"
"strings"
)
// ReadInput Capture user input for a question
func ReadInput() (string, error) {
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSuffix(input, "\n"), nil
}
// PromptYesNo Returns true if the user has enterred Y or YES, capitalization is ignored, we are
// matching elasticsearch behavior
func PromptYesNo(prompt string, defaultAnswer bool) bool {
var defaultYNprompt string
if defaultAnswer == true {
defaultYNprompt = "[Y/n]"
} else {
defaultYNprompt = "[y/N]"
}
fmt.Printf("%s %s: ", prompt, defaultYNprompt)
for {
input, err := ReadInput()
if err != nil {
panic("could not read from input")
}
response := strings.TrimSpace(input)
response = strings.ToLower(response)
if response == "" {
return defaultAnswer
} else if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
fmt.Printf("Did not understand the answer '%s'\n", input)
}
}