-
Notifications
You must be signed in to change notification settings - Fork 15
/
funcs.go
72 lines (63 loc) · 1.38 KB
/
funcs.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
package prompter
import (
"regexp"
"strings"
)
// Prompt simple prompting
func Prompt(message, defaultAnswer string) string {
return (&Prompter{
Message: message,
Default: defaultAnswer,
}).Prompt()
}
// YN y/n choice
func YN(message string, defaultToYes bool) bool {
defaultChoice := "n"
if defaultToYes {
defaultChoice = "y"
}
input := (&Prompter{
Message: message,
Choices: []string{"y", "n"},
IgnoreCase: true,
Default: defaultChoice,
}).Prompt()
return strings.ToLower(input) == "y"
}
// YesNo yes/no choice
func YesNo(message string, defaultToYes bool) bool {
defaultChoice := "no"
if defaultToYes {
defaultChoice = "yes"
}
input := (&Prompter{
Message: message,
Choices: []string{"yes", "no"},
IgnoreCase: true,
Default: defaultChoice,
}).Prompt()
return strings.ToLower(input) == "yes"
}
// Password asks password
func Password(message string) string {
return (&Prompter{
Message: message,
NoEcho: true,
}).Prompt()
}
// Choose make a choice
func Choose(message string, choices []string, defaultChoice string) string {
return (&Prompter{
Message: message,
Choices: choices,
Default: defaultChoice,
}).Prompt()
}
// Regexp checks the answer by regexp
func Regexp(message string, reg *regexp.Regexp, defaultAnswer string) string {
return (&Prompter{
Message: message,
Regexp: reg,
Default: defaultAnswer,
}).Prompt()
}