-
Notifications
You must be signed in to change notification settings - Fork 787
/
pickers.go
176 lines (163 loc) · 5.17 KB
/
pickers.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package util
import (
"fmt"
"sort"
"strings"
"github.com/jenkins-x/jx/v2/pkg/log"
"gopkg.in/AlecAivazis/survey.v1"
)
// PickValue gets an answer to a prompt from a user's free-form input
func PickValue(message string, defaultValue string, required bool, help string, handles IOFileHandles) (string, error) {
answer := ""
prompt := &survey.Input{
Message: message,
Default: defaultValue,
Help: help,
}
validator := survey.Required
if !required {
validator = nil
}
surveyOpts := survey.WithStdio(handles.In, handles.Out, handles.Err)
err := survey.AskOne(prompt, &answer, validator, surveyOpts)
if err != nil {
return "", err
}
return answer, nil
}
// PickPassword gets a password (via hidden input) from a user's free-form input
func PickPassword(message string, help string, handles IOFileHandles) (string, error) {
answer := ""
prompt := &survey.Password{
Message: message,
Help: help,
}
validator := survey.Required
surveyOpts := survey.WithStdio(handles.In, handles.Out, handles.Err)
err := survey.AskOne(prompt, &answer, validator, surveyOpts)
if err != nil {
return "", err
}
return strings.TrimSpace(answer), nil
}
// PickNameWithDefault gets the user to pick an option from a list of options, with a default option specified
func PickNameWithDefault(names []string, message string, defaultValue string, help string, handles IOFileHandles) (string, error) {
name := ""
if len(names) == 0 {
return "", nil
} else if len(names) == 1 {
name = names[0]
} else {
prompt := &survey.Select{
Message: message,
Options: names,
Default: defaultValue,
}
surveyOpts := survey.WithStdio(handles.In, handles.Out, handles.Err)
err := survey.AskOne(prompt, &name, nil, surveyOpts)
if err != nil {
return "", err
}
}
return name, nil
}
// PickRequiredNameWithDefault gets the user to pick an option from a list of options, with a default option specified
func PickRequiredNameWithDefault(names []string, message string, defaultValue string, help string, handles IOFileHandles) (string, error) {
name := ""
if len(names) == 0 {
return "", nil
} else if len(names) == 1 {
name = names[0]
} else {
prompt := &survey.Select{
Message: message,
Options: names,
Default: defaultValue,
Help: help,
}
surveyOpts := survey.WithStdio(handles.In, handles.Out, handles.Err)
err := survey.AskOne(prompt, &name, survey.Required, surveyOpts)
if err != nil {
return "", err
}
}
return name, nil
}
// PickName gets the user to pick an option from a list of options
func PickName(names []string, message string, help string, handles IOFileHandles) (string, error) {
return PickNameWithDefault(names, message, "", help, handles)
}
// PickNames gets the user to pick multiple selections from a list of options
func PickNames(names []string, message string, help string, handles IOFileHandles) ([]string, error) {
return PickNamesWithDefaults(names, nil, message, help, handles)
}
// PickNamesWithDefaults gets the user to pick multiple selections from a list of options with a set of default selections
func PickNamesWithDefaults(names []string, defaults []string, message string, help string, handles IOFileHandles) ([]string, error) {
picked := []string{}
if len(names) == 0 {
return picked, nil
} else if len(names) == 1 {
return names, nil
} else {
prompt := &survey.MultiSelect{
Message: message,
Options: names,
Default: defaults,
Help: help,
}
surveyOpts := survey.WithStdio(handles.In, handles.Out, handles.Err)
err := survey.AskOne(prompt, &picked, nil, surveyOpts)
if err != nil {
return picked, err
}
}
return picked, nil
}
// SelectNamesWithFilter selects from a list of names with a given filter. Optionally selecting them all
func SelectNamesWithFilter(names []string, message string, selectAll bool, filter string, help string, handles IOFileHandles) ([]string, error) {
filtered := []string{}
for _, name := range names {
if filter == "" || strings.Index(name, filter) >= 0 {
filtered = append(filtered, name)
}
}
if len(filtered) == 0 {
return nil, fmt.Errorf("No names match filter: %s", filter)
}
return SelectNames(filtered, message, selectAll, help, handles)
}
// SelectNames select which names from the list should be chosen
func SelectNames(names []string, message string, selectAll bool, help string, handles IOFileHandles) ([]string, error) {
answer := []string{}
if len(names) == 0 {
return answer, fmt.Errorf("No names to choose from")
}
sort.Strings(names)
prompt := &survey.MultiSelect{
Message: message,
Options: names,
Help: help,
}
if selectAll {
prompt.Default = names
}
surveyOpts := survey.WithStdio(handles.In, handles.Out, handles.Err)
err := survey.AskOne(prompt, &answer, nil, surveyOpts)
return answer, err
}
// Confirm prompts the user to confirm something
func Confirm(message string, defaultValue bool, help string, handles IOFileHandles) (bool, error) {
answer := defaultValue
prompt := &survey.Confirm{
Message: message,
Default: defaultValue,
Help: help,
}
surveyOpts := survey.WithStdio(handles.In, handles.Out, handles.Err)
err := survey.AskOne(prompt, &answer, nil, surveyOpts)
if err != nil {
return false, err
}
log.Blank()
return answer, nil
}