-
Notifications
You must be signed in to change notification settings - Fork 1
/
pickers.go
149 lines (137 loc) · 3.28 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
package util
import (
"fmt"
"sort"
"strings"
"github.com/jenkins-x/jx/pkg/log"
"gopkg.in/AlecAivazis/survey.v1"
)
func PickValue(message string, defaultValue string, required bool) (string, error) {
answer := ""
prompt := &survey.Input{
Message: message,
Default: defaultValue,
}
validator := survey.Required
if !required {
validator = nil
}
err := survey.AskOne(prompt, &answer, validator)
if err != nil {
return "", err
}
return answer, nil
}
func PickPassword(message string) (string, error) {
answer := ""
prompt := &survey.Password{
Message: message,
}
validator := survey.Required
err := survey.AskOne(prompt, &answer, validator)
if err != nil {
return "", err
}
return strings.TrimSpace(answer), nil
}
func PickNameWithDefault(names []string, message string, defaultValue string) (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,
}
err := survey.AskOne(prompt, &name, nil)
if err != nil {
return "", err
}
}
return name, nil
}
func PickRequiredNameWithDefault(names []string, message string, defaultValue string) (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,
}
err := survey.AskOne(prompt, &name, survey.Required)
if err != nil {
return "", err
}
}
return name, nil
}
func PickName(names []string, message string) (string, error) {
return PickNameWithDefault(names, message, "")
}
func PickNames(names []string, message string) ([]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,
}
err := survey.AskOne(prompt, &picked, nil)
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) ([]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)
}
// SelectNames select which names from the list should be chosen
func SelectNames(names []string, message string, selectAll bool) ([]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,
}
if selectAll {
prompt.Default = names
}
err := survey.AskOne(prompt, &answer, nil)
return answer, err
}
// Confirm prompts the user to confirm something
func Confirm(message string, defaultValue bool, help string) bool {
answer := defaultValue
prompt := &survey.Confirm{
Message: message,
Default: defaultValue,
Help: help,
}
survey.AskOne(prompt, &answer, nil)
log.Blank()
return answer
}