-
Notifications
You must be signed in to change notification settings - Fork 1
/
prompter.go
384 lines (331 loc) · 8.63 KB
/
prompter.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package prompt
import (
"bufio"
"fmt"
"io"
"sort"
"strings"
"github.com/sirupsen/logrus"
)
type Option struct {
text string
value string
isDefault bool
}
// String returns the text of the option, which is used for display
func (o *Option) String() string {
return o.text
}
// Value returns the value of the option, which could be an ID or enum value
func (o *Option) Value() string {
return o.value
}
type ValidatorFunc func(*Prompt, string) (bool, error)
type FilterFunc func(prompt *Prompt) bool
type PromptsContext struct {
answers map[string]string
}
// Set sets the value of the given key
func (c *PromptsContext) Set(key string, value string) {
c.answers[key] = value
}
func (c *PromptsContext) Lookup(key string) (string, bool) {
val, ok := c.answers[key]
return val, ok
}
// NewPromptsContext creates a new PromptsContext
func NewPromptsContext() *PromptsContext {
return &PromptsContext{
answers: make(map[string]string),
}
}
type Prompt struct {
context *PromptsContext
parent *Prompt
path string
text string
options []Option
optionHandler ValueGetter
shortCircuit FilterFunc
validator ValidatorFunc
subPrompts []Prompt
defaultValue string
}
func (p *Prompt) GetAnswer(key string) string {
val, ok := p.context.Lookup(key)
if ok {
return val
}
return ""
}
func (p *Prompt) LookupAnswer(key string) (string, bool) {
return p.context.Lookup(key)
}
// VarMap returns a _copy_ of the variable map
func (p *Prompt) VarMap() map[string]string {
result := make(map[string]string)
for k, v := range p.context.answers {
result[k] = v
}
return result
}
func (p *Prompt) SubPrompts() []Prompt {
return p.subPrompts
}
func (p *Prompt) AddSubPrompt(prompt *Prompt) {
prompt.parent = p
prompt.context = p.context
p.subPrompts = append(p.subPrompts, *prompt)
}
func (p *Prompt) String() string {
// I decided not to use the text format, which does allow for some very
// complex formatting here, for readability reasons
var result string
// If it has options, get the sorted list of options
if len(p.AvailableOptions()) == 0 {
result = p.text
if val, exists := p.DefaultValue(); exists {
result = fmt.Sprintf("%s (default: \"%s\")", result, val)
}
} else {
result = fmt.Sprintf("%s [\"%s\"]", p.text, strings.Join(p.OptionsToStrings(), "\"/\""))
if opt, exists := p.DefaultOption(); exists {
result = fmt.Sprintf("%s (default: \"%s\")", result, opt.text)
}
}
return result
}
func (p *Prompt) OptionsToStrings() []string {
result := make([]string, len(p.AvailableOptions()))
for i, o := range p.AvailableOptions() {
result[i] = o.text
}
sort.Strings(result)
return result
}
// ValueOf resolves the value of t, which is assumed to be text, to the
// value. With options, this is the `value` field of the `Option` with
// the matching text.
func (p *Prompt) ValueOf(t string) string {
avail := p.AvailableOptions()
if avail != nil && len(avail) >= 0 {
for _, o := range avail {
if o.text == t {
return o.Value()
}
}
}
return t
}
func (p *Prompt) DefaultOption() (*Option, bool) {
// Otherwise, look through the default options and find the default option
for _, o := range p.AvailableOptions() {
if o.isDefault {
return &o, true
}
}
return nil, false
}
func (p *Prompt) DefaultValue() (string, bool) {
if len(p.defaultValue) > 0 {
return p.defaultValue, true
} else if len(p.AvailableOptions()) > 0 {
opt, exists := p.DefaultOption()
if exists {
return opt.value, true
}
}
return "", false
}
func (p *Prompt) AvailableOptions() []Option {
allOptions := make([]Option, 0)
if p.optionHandler != nil {
values, _ := p.optionHandler()
if len(values) > 0 {
for k, v := range values {
allOptions = append(allOptions, Option{
text: v,
value: k,
isDefault: false,
})
}
allOptions = append(p.options, allOptions...)
return allOptions
}
}
return p.options
}
func (p *Prompt) Record(answer string) bool {
p.context.Set(p.path, strings.TrimSpace(answer))
return true
}
func (p *Prompt) Itr() func() *Prompt {
// Basically, this is a delegate for closures. Based on different
// criteria, we want to
logrus.Trace("Building iterator now...")
var current *Prompt
var myIdx int = 0
return func() *Prompt {
logrus.Trace("Getting next prompt now...")
// The first thing we'll do is see if the current one is nil
// and also make sure we don't have a parent. This is a special
// case and so set the current to ourselves and return a self-referencing
// iterator
if current == nil && p.parent == nil {
current = p
return current
}
// Now we'll make sure we're valid. If we're not valid, then we
// also return the self-refernce iterator
isValid, _ := current.isValid()
if !isValid {
return current
}
currIdx := 0
// Now that we're past all this, our goal in life is to return
// the next child object
if currIdx < len(current.subPrompts) {
next := current.subPrompts[currIdx]
current = &next
currIdx++
return current
}
// but once I'm at the end of my sub items, I need to return my parent's
// next sub item (my sibling)
if len(current.subPrompts) == 0 {
if len(p.subPrompts) > (myIdx + 1) {
next := p.subPrompts[myIdx+1]
current = &next
myIdx++
return current
}
}
return nil
}
}
func (p *Prompt) isValid() (bool, error) {
// If it does not have a validator at all, it's just assumed good.
if p.validator == nil {
return true, nil
}
val, ok := p.context.Lookup(p.path)
if !ok {
return false, nil
}
return p.validator(p, val)
}
type ByText []Option
func (a ByText) Len() int { return len(a) }
func (a ByText) Swap(i int, j int) { a[i], a[j] = a[j], a[i] }
func (a ByText) Less(i int, j int) bool { return a[i].text < a[j].text }
type PromptBuilder struct {
ctx *PromptsContext
path string
text string
defaultValue string
filter FilterFunc
options []Option
optionFunc ValueGetter
validatorFunc ValidatorFunc
}
func (b *PromptBuilder) Context(ctx *PromptsContext) *PromptBuilder {
b.ctx = ctx
return b
}
func (b *PromptBuilder) WithLogging() *PromptBuilder {
logrus.SetLevel(logrus.DebugLevel)
return b
}
func (b *PromptBuilder) Path(p string) *PromptBuilder {
b.path = p
return b
}
func (b *PromptBuilder) Text(t string) *PromptBuilder {
b.text = t
return b
}
func (b *PromptBuilder) Textf(format string, a ...interface{}) *PromptBuilder {
b.text = fmt.Sprintf(format, a...)
return b
}
func (b *PromptBuilder) AskWhen(filter FilterFunc) *PromptBuilder {
b.filter = filter
return b
}
func (b *PromptBuilder) WithDefault(val string) *PromptBuilder {
b.defaultValue = val
return b
}
func (b *PromptBuilder) AddOption(text string, value string) *PromptBuilder {
opt := &Option{
text: text,
value: value,
isDefault: false,
}
b.options = append(b.options, *opt)
return b
}
func (b *PromptBuilder) AddDefaultOption(text string, value string) *PromptBuilder {
opt := &Option{
text: text,
value: value,
isDefault: true,
}
b.options = append(b.options, *opt)
return b
}
func (b *PromptBuilder) WithOptions(optionFunc ValueGetter) *PromptBuilder {
b.optionFunc = optionFunc
return b
}
func (b *PromptBuilder) WithValidator(f ValidatorFunc) *PromptBuilder {
b.validatorFunc = f
return b
}
func (b *PromptBuilder) Build() (*Prompt, error) {
if len(b.path) == 0 {
return nil, fmt.Errorf("there must be a path defined")
}
return &Prompt{
context: b.ctx,
path: b.path,
text: b.text,
defaultValue: b.defaultValue,
shortCircuit: b.filter,
options: b.options,
validator: b.validatorFunc,
optionHandler: b.optionFunc,
}, nil
}
func NewPromptBuilder() *PromptBuilder {
return &PromptBuilder{
ctx: NewPromptsContext(),
options: []Option{},
}
}
func Ask(prompt *Prompt, out io.Writer, in io.Reader) error {
buf := bufio.NewReader(in)
_, err := out.Write([]byte(fmt.Sprintf("%s ", prompt.String())))
if err != nil {
return err
}
answer, _ := buf.ReadString('\n')
answer = strings.TrimSpace(answer)
if len(answer) == 0 {
if def, exists := prompt.DefaultValue(); exists {
logrus.Tracef("Trying to use default value: %s...", def)
prompt.Record(def)
} else if defOpt, optExists := prompt.DefaultOption(); optExists {
logrus.Tracef("Trying to use default option: %s...", defOpt.Value())
prompt.Record(defOpt.text)
}
} else {
// TODO: The user will have answered using the display text, but
// we want to record the actual value. Here, the value needs to be
// resolved
logrus.Tracef("Recording answer: %s", answer)
val := prompt.ValueOf(answer)
prompt.Record(val)
}
return nil
}