forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
survey.go
95 lines (83 loc) · 2.09 KB
/
survey.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
package prompter
import (
"errors"
"fmt"
"github.com/AlecAivazis/survey"
)
// CliPrompter used to prompt for cli input
type CliPrompter struct {
}
// NewCli builds a new cli prompter
func NewCli() *CliPrompter {
return &CliPrompter{}
}
// RequestSecurityCode request a security code to be entered by the user
func (cli *CliPrompter) RequestSecurityCode(pattern string) string {
token := ""
prompt := &survey.Input{
Message: fmt.Sprintf("Security Token [%s]", pattern),
}
survey.AskOne(prompt, &token, survey.Required)
return token
}
// ChooseWithDefault given the choice return the option selected with a default
func (cli *CliPrompter) ChooseWithDefault(pr string, defaultValue string, options []string) (string, error) {
selected := ""
prompt := &survey.Select{
Message: pr,
Options: options,
Default: defaultValue,
}
survey.AskOne(prompt, &selected, survey.Required)
// return the selected element index
for i, option := range options {
if selected == option {
return options[i], nil
}
}
return "", errors.New("bad input")
}
// Choose given the choice return the option selected
func (cli *CliPrompter) Choose(pr string, options []string) int {
selected := ""
prompt := &survey.Select{
Message: pr,
Options: options,
}
survey.AskOne(prompt, &selected, survey.Required)
// return the selected element index
for i, option := range options {
if selected == option {
return i
}
}
return 0
}
// StringRequired prompt for string which is required
func (cli *CliPrompter) String(pr string, defaultValue string) string {
val := ""
prompt := &survey.Input{
Message: pr,
Default: defaultValue,
}
survey.AskOne(prompt, &val, nil)
return val
}
// StringRequired prompt for string which is required
func (cli *CliPrompter) StringRequired(pr string) string {
val := ""
prompt := &survey.Input{
Message: pr,
}
survey.AskOne(prompt, &val, survey.Required)
return val
}
// Password prompt for password which is required
func (cli *CliPrompter) Password(pr string) string {
val := ""
prompt := &survey.Password{
Message: pr,
}
survey.AskOne(prompt, &val, nil)
return val
}