-
Notifications
You must be signed in to change notification settings - Fork 1
/
terminal.go
296 lines (235 loc) · 7.84 KB
/
terminal.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package cli
import (
"errors"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/lithammer/dedent"
"github.com/manifoldco/promptui"
"golang.org/x/term"
)
// Prompt will ask the following "label" question to the user and will transform the received `string`
// into the generic type T via the `transformer` function. There is a few predefined transformer for
// common types like `PromptTypeString`, `PromptTypeInt`, `PromptTypeInt64`, `PromptTypeUint64`,
// `PromptTypeYesNo`. Using the right transformer will automatically infers the right T genric type.
//
// userID := cli.Prompt("Please enter the user ID to issue the token to", cli.PromptTypeString)
//
// In this case, the `userID` variable will be of type `string`.
func Prompt[T any](label string, transformer PromptTransformer[T], opts ...PromptOption) T {
out, err := MaybePrompt(label, transformer, opts...)
if err != nil {
panic(fmt.Errorf("prompt failed: %w", err))
}
return out
}
// MaybePrompt is just like [Prompt] but may return an error
func MaybePrompt[T any](label string, transformer PromptTransformer[T], opts ...PromptOption) (T, error) {
choice, err := PromptRaw(label, opts...)
if err == nil {
return transformer(choice)
}
var empty T
return empty, err
}
// PromptConfirm is just like [Prompt] but enforce `IsConfirm` and returns a boolean which is either
// `true` for yes answer or `false` for a no answer.
func PromptConfirm(label string, opts ...PromptOption) (answer bool, wasAnswered bool) {
answer, wasAnswered, err := MaybePromptConfirm(label, opts...)
if err != nil {
panic(fmt.Errorf("prompt confirm failed: %w", err))
}
return answer, wasAnswered
}
// MaybePromptConfirm is just like [PromptConfirm] but returns an error instead of panicking.
func MaybePromptConfirm(label string, opts ...PromptOption) (answer bool, wasAnswered bool, err error) {
if !term.IsTerminal(int(os.Stdout.Fd())) {
wasAnswered = false
return
}
opts = append([]PromptOption{WithPromptValidate("invalid", PrompValidateYesNo), WithPromptConfirm()}, opts...)
answer, err = MaybePrompt(label, PromptTypeYesNo, opts...)
return answer, err == nil, err
}
func PromptSelect[T any](label string, items []string, transformer PromptTransformer[T], opts ...PromptSelectOption) T {
out, err := MaybePromptSelect(label, items, transformer, opts...)
if err != nil {
panic(fmt.Errorf("prompt select failed: %w", err))
}
return out
}
func MaybePromptSelect[T any](label string, items []string, transformer PromptTransformer[T], opts ...PromptSelectOption) (T, error) {
options := promptSelectOptions{}
for _, opt := range opts {
opt.Apply(&options)
}
choice := promptui.Select{
Label: label,
Items: items,
HideHelp: true,
Templates: options.selectTemplates,
}
_, selection, err := choice.Run()
if err != nil {
if errors.Is(err, promptui.ErrInterrupt) {
// We received Ctrl-C, users wants to abort, nothing else to do, quit immediately
Exit(1)
}
var empty T
return empty, fmt.Errorf("running protocol prompt: %w", err)
}
return transformer(selection)
}
func AskConfirmation(label string, args ...interface{}) (answeredYes bool, wasAnswered bool) {
if !term.IsTerminal(int(os.Stdout.Fd())) {
wasAnswered = false
return
}
prompt := promptui.Prompt{
Label: dedent.Dedent(fmt.Sprintf(label, args...)),
Default: "N",
AllowEdit: true,
IsConfirm: true,
HideEntered: true,
}
_, err := prompt.Run()
if err != nil {
// zlog.Debug("unable to aks user to see diff right now, too bad", zap.Error(err))
wasAnswered = false
return
}
wasAnswered = true
answeredYes = true
return
}
type PromptOption interface {
Apply(opts *promptOptions)
}
type promptIsDefaultValue string
func (o promptIsDefaultValue) Apply(opts *promptOptions) {
opts.defaultValue = string(o)
}
func WithPromptDefaultValue(in string) PromptOption {
return promptIsDefaultValue(in)
}
type promptIsConfirmOption bool
func (o promptIsConfirmOption) Apply(opts *promptOptions) {
opts.isConfirm = bool(o)
}
func WithPromptConfirm() PromptOption {
return promptIsConfirmOption(true)
}
type validatePromptOption promptui.ValidateFunc
func (o validatePromptOption) Apply(opts *promptOptions) {
opts.validate = promptui.ValidateFunc(o)
}
func WithPromptValidate(label string, fn promptui.ValidateFunc) PromptOption {
return validatePromptOption(func(x string) error {
err := fn(x)
if err != nil {
return fmt.Errorf(label+": %w", err)
}
return nil
})
}
type promptTemplatesOption promptui.PromptTemplates
func (o *promptTemplatesOption) Apply(opts *promptOptions) {
opts.promptTemplates = (*promptui.PromptTemplates)(o)
}
func WithPromptTemplates(templates *promptui.PromptTemplates) PromptOption {
return (*promptTemplatesOption)(templates)
}
type promptOptions struct {
validate promptui.ValidateFunc
isConfirm bool
promptTemplates *promptui.PromptTemplates
defaultValue string
}
func PromptRaw(label string, opts ...PromptOption) (answer string, err error) {
options := promptOptions{}
for _, opt := range opts {
opt.Apply(&options)
}
templates := options.promptTemplates
if templates == nil {
templates = &promptui.PromptTemplates{
Success: `{{ . | faint }}{{ ":" | faint}} `,
}
}
if options.isConfirm {
// We don't have no differences
templates.Valid = `{{ "?" | blue}} {{ . | bold }} {{ "[y/N]" | faint}} `
templates.Invalid = templates.Valid
}
prompt := promptui.Prompt{
Label: label,
Templates: templates,
}
if options.validate != nil {
prompt.Validate = options.validate
}
if prompt.Validate == nil && options.isConfirm {
prompt.Validate = PrompValidateYesNo
}
if options.defaultValue != "" {
prompt.Default = options.defaultValue
prompt.AllowEdit = true
}
choice, err := prompt.Run()
if err != nil {
if errors.Is(err, promptui.ErrInterrupt) {
Exit(1)
}
if prompt.IsConfirm && errors.Is(err, promptui.ErrAbort) {
return "false", nil
}
return "", fmt.Errorf("running prompt: %w", err)
}
return choice, nil
}
// Various prompt transformer declared like you are using standard type and generics make the rest
//
// To be used like:
//
// PromptT("Input your age", opts, PromptTypeUint64)
var (
PromptTypeString = func(x string) (string, error) { return x, nil }
PromptTypeInt = strconv.Atoi
PromptTypeInt64 = func(x string) (int64, error) { return strconv.ParseInt(x, 0, 64) }
PromptTypeUint64 = func(x string) (uint64, error) { return strconv.ParseUint(x, 0, 64) }
PromptTypeYesNo = func(in string) (bool, error) { in = strings.ToLower(in); return in == "y" || in == "yes", nil }
)
// Various prompt validation declared like you are using standard validation.
//
// To be used like:
//
// cli.PromptT("Input your age", cli.PromptTypeUint64, cli.WithPromptValidate("invalid age", cli.PrompValidateUint64))
var (
PrompValidateString = func(x string) error { return nil }
PrompValidateInt = func(x string) error { _, err := strconv.Atoi(x); return err }
PrompValidateInt64 = func(x string) error { _, err := strconv.ParseInt(x, 0, 64); return err }
PrompValidateUint64 = func(x string) error { _, err := strconv.ParseUint(x, 0, 64); return err }
PrompValidateYesNo = func(in string) error {
if !confirmPromptRegex.MatchString(in) {
return errors.New("answer with y/yes/Yes or n/no/No")
}
return nil
}
)
var confirmPromptRegex = regexp.MustCompile("(y|Y|n|N|No|Yes|YES|NO)")
type PromptTransformer[T any] func(string) (T, error)
type promptSelectOptions struct {
selectTemplates *promptui.SelectTemplates
}
type PromptSelectOption interface {
Apply(opts *promptSelectOptions)
}
type promptSelectTemplatesOption promptui.SelectTemplates
func (o *promptSelectTemplatesOption) Apply(opts *promptSelectOptions) {
opts.selectTemplates = (*promptui.SelectTemplates)(o)
}
func WithPromptSelectTemplates(templates *promptui.SelectTemplates) PromptSelectOption {
return (*promptSelectTemplatesOption)(templates)
}