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