-
Notifications
You must be signed in to change notification settings - Fork 7
/
survey.go
98 lines (77 loc) · 2.03 KB
/
survey.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
//go:build !integration
package prompter
import (
"fmt"
"github.com/AlecAivazis/survey/v2"
)
var pageSize = survey.WithPageSize(20)
func (s srv) AskForString(direction string, validator Validator) (string, error) {
result := ""
input := survey.Input{
Message: direction,
}
err := survey.AskOne(&input, &result, survey.WithValidator(survey.Validator(validator)))
return result, err
}
func (s srv) AskForSelectionFromList(direction string, list []fmt.Stringer) (any, error) {
count := len(list)
if count == 0 {
return nil, EmptyList
}
options := make(map[string]fmt.Stringer)
keys := make([]string, 0)
for _, item := range list {
choice := item.String()
options[choice] = item
keys = append(keys, choice)
}
selectedChoice := ""
err := survey.AskOne(&survey.Select{
Message: direction,
Options: keys,
}, &selectedChoice, pageSize)
return options[selectedChoice], err
}
func (s srv) AskForMultipleSelectionFromList(direction string, list []fmt.Stringer) ([]any, error) {
count := len(list)
if count == 0 {
return nil, EmptyList
}
options := make(map[string]fmt.Stringer)
keys := make([]string, 0)
for _, item := range list {
choice := item.String()
options[choice] = item
keys = append(keys, choice)
}
selectedChoices := make([]string, 0)
err := survey.AskOne(&survey.MultiSelect{
Message: direction,
Options: keys,
}, &selectedChoices, pageSize)
results := make([]any, 0)
for i := range selectedChoices {
results = append(results, options[selectedChoices[i]])
}
return results, err
}
func (s srv) AskForYesOrNo(direction string) (bool, error) {
name := false
prompt := &survey.Confirm{
Message: direction,
}
err := survey.AskOne(prompt, &name)
return name, err
}
func (s srv) AskForMultilineString(direction, defaultValue, pattern string) (string, error) {
prompt := &survey.Editor{
Message: direction,
Default: defaultValue,
HideDefault: true,
AppendDefault: true,
FileName: pattern,
}
result := ""
err := survey.AskOne(prompt, &result)
return result, err
}