forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathask.go
154 lines (122 loc) · 3.36 KB
/
ask.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package cmd
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"golang.org/x/crypto/ssh/terminal"
"github.com/lxc/lxd/shared"
)
var stdin = bufio.NewReader(os.Stdin)
// AskBool asks a question and expect a yes/no answer.
func AskBool(question string, defaultAnswer string) bool {
for {
answer := askQuestion(question, defaultAnswer)
if shared.StringInSlice(strings.ToLower(answer), []string{"yes", "y"}) {
return true
} else if shared.StringInSlice(strings.ToLower(answer), []string{"no", "n"}) {
return false
}
invalidInput()
}
}
// AskChoice asks the user to select one of multiple options
func AskChoice(question string, choices []string, defaultAnswer string) string {
for {
answer := askQuestion(question, defaultAnswer)
if shared.StringInSlice(answer, choices) {
return answer
}
invalidInput()
}
}
// AskInt asks the user to enter an integer between a min and max value
func AskInt(question string, min int64, max int64, defaultAnswer string, validate func(int64) error) int64 {
for {
answer := askQuestion(question, defaultAnswer)
result, err := strconv.ParseInt(answer, 10, 64)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid input: %v\n\n", err)
continue
}
if !((min == -1 || result >= min) && (max == -1 || result <= max)) {
fmt.Fprintf(os.Stderr, "Invalid input: out of range\n\n")
continue
}
if validate != nil {
err = validate(result)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid input: %v\n\n", err)
continue
}
}
return result
}
}
// AskString asks the user to enter a string, which optionally
// conforms to a validation function.
func AskString(question string, defaultAnswer string, validate func(string) error) string {
for {
answer := askQuestion(question, defaultAnswer)
if validate != nil {
error := validate(answer)
if error != nil {
fmt.Fprintf(os.Stderr, "Invalid input: %s\n\n", error)
continue
}
return answer
}
if len(answer) != 0 {
return answer
}
invalidInput()
}
}
// AskPassword asks the user to enter a password.
func AskPassword(question string) string {
for {
fmt.Printf(question)
pwd, _ := terminal.ReadPassword(0)
fmt.Println("")
inFirst := string(pwd)
inFirst = strings.TrimSuffix(inFirst, "\n")
fmt.Printf("Again: ")
pwd, _ = terminal.ReadPassword(0)
fmt.Println("")
inSecond := string(pwd)
inSecond = strings.TrimSuffix(inSecond, "\n")
if inFirst == inSecond {
return inFirst
}
invalidInput()
}
}
// AskPasswordOnce asks the user to enter a password.
//
// It's the same as AskPassword, but it won't ask to enter it again.
func AskPasswordOnce(question string) string {
fmt.Printf(question)
pwd, _ := terminal.ReadPassword(0)
fmt.Println("")
return string(pwd)
}
// Ask a question on the output stream and read the answer from the input stream
func askQuestion(question, defaultAnswer string) string {
fmt.Printf(question)
return readAnswer(defaultAnswer)
}
// Read the user's answer from the input stream, trimming newline and providing a default.
func readAnswer(defaultAnswer string) string {
answer, _ := stdin.ReadString('\n')
answer = strings.TrimSuffix(answer, "\n")
answer = strings.TrimSpace(answer)
if answer == "" {
answer = defaultAnswer
}
return answer
}
// Print an invalid input message on the error stream
func invalidInput() {
fmt.Fprintf(os.Stderr, "Invalid input, try again.\n\n")
}