-
Notifications
You must be signed in to change notification settings - Fork 313
/
survey.go
130 lines (109 loc) · 2.83 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
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
package survey
import (
"os"
"regexp"
"sort"
surveypkg "github.com/AlecAivazis/survey/v2"
"github.com/pkg/errors"
)
// QuestionOptions defines a question and its options
type QuestionOptions struct {
Question string
DefaultValue string
DefaultValueSet bool
ValidationRegexPattern string
ValidationMessage string
ValidationFunc func(value string) error
Options []string
Sort bool
IsPassword bool
}
// DefaultValidationRegexPattern is the default regex pattern to validate the input
var DefaultValidationRegexPattern = regexp.MustCompile("^.*$")
// Survey is the interface for asking questions
type Survey interface {
Question(params *QuestionOptions) (string, error)
}
type survey struct{}
// NewSurvey creates a new survey object
func NewSurvey() Survey {
return &survey{}
}
// Question asks the user a question and returns the answer
func (s *survey) Question(params *QuestionOptions) (string, error) {
var prompt surveypkg.Prompt
compiledRegex := DefaultValidationRegexPattern
if params.ValidationRegexPattern != "" {
compiledRegex = regexp.MustCompile(params.ValidationRegexPattern)
}
if params.Options != nil {
if params.Sort {
params.Options = copyStringArray(params.Options)
sort.Strings(params.Options)
}
prompt = &surveypkg.Select{
Message: params.Question,
Options: params.Options,
Default: params.DefaultValue,
}
} else if params.IsPassword {
prompt = &surveypkg.Password{
Message: params.Question,
}
} else {
prompt = &surveypkg.Input{
Message: params.Question,
Default: params.DefaultValue,
}
}
question := []*surveypkg.Question{
{
Name: "question",
Prompt: prompt,
},
}
if params.Options == nil {
question[0].Validate = func(val interface{}) error {
str, ok := val.(string)
if !ok {
return errors.New("Input was not a string")
}
// Check regex
if !compiledRegex.MatchString(str) {
if params.ValidationMessage != "" {
return errors.New(params.ValidationMessage)
}
return errors.Errorf("Answer has to match pattern: %s", compiledRegex.String())
}
// Check function
if params.ValidationFunc != nil {
err := params.ValidationFunc(str)
if err != nil {
if params.ValidationMessage != "" {
return errors.New(params.ValidationMessage)
}
return errors.Errorf("%v", err)
}
}
return nil
}
}
// Ask it
answers := struct {
Question string
}{}
err := surveypkg.Ask(question, &answers)
if err != nil {
// Keyboard interrupt
os.Exit(0)
}
if answers.Question == "" && len(params.Options) > 0 {
answers.Question = params.Options[0]
}
return answers.Question, nil
}
func copyStringArray(strings []string) []string {
retStrings := []string{}
retStrings = append(retStrings, strings...)
return retStrings
}