-
Notifications
You must be signed in to change notification settings - Fork 9
/
surveys.go
160 lines (129 loc) · 4.16 KB
/
surveys.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
package bundle
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/pluralsh/plural-cli/pkg/api"
"github.com/pluralsh/plural-cli/pkg/manifest"
"github.com/pluralsh/plural-cli/pkg/utils"
"github.com/mitchellh/go-homedir"
)
func stringValidator(item *api.ConfigurationItem) survey.AskOpt {
return survey.WithValidator(func(val interface{}) error {
res, _ := val.(string)
if item.Validation != nil && item.Validation.Type == "REGEX" {
valid := item.Validation
return utils.ValidateRegex(res, valid.Regex, valid.Message)
}
return nil
})
}
func stringSurvey(def string, item *api.ConfigurationItem) (survey.Prompt, []survey.AskOpt) {
opts := []survey.AskOpt{stringValidator(item)}
if !item.Optional {
opts = append(opts, survey.WithValidator(survey.Required))
}
return &survey.Input{
Message: "Enter the value",
Default: def,
}, opts
}
func passwordSurvey(item *api.ConfigurationItem) (survey.Prompt, []survey.AskOpt) {
opts := []survey.AskOpt{stringValidator(item)}
if !item.Optional {
opts = append(opts, survey.WithValidator(survey.Required))
}
return &survey.Password{Message: "Enter the value"}, opts
}
func boolSurvey() (survey.Prompt, []survey.AskOpt) {
return &survey.Confirm{Message: "Yes or no?"}, []survey.AskOpt{survey.WithValidator(survey.Required)}
}
func intSurvey(def string) (survey.Prompt, []survey.AskOpt) {
return &survey.Input{
Message: "Enter the value",
Default: def,
}, []survey.AskOpt{survey.WithValidator(survey.Required)}
}
func domainSurvey(def string, item *api.ConfigurationItem, proj *manifest.ProjectManifest, context *manifest.Context) (survey.Prompt, []survey.AskOpt) {
opts := []survey.AskOpt{
survey.WithValidator(func(val interface{}) error {
res, _ := val.(string)
if res == "" && item.Optional {
return nil
}
if context.HasDomain(res) && res != def {
return fmt.Errorf("domain %s has already been used elsewhere in this project, please chose another", res)
}
if proj.Network != nil && !strings.HasSuffix(res, proj.Network.Subdomain) {
return fmt.Errorf("domain must end with %s", proj.Network.Subdomain)
}
if err := utils.ValidateDns(res); err != nil {
return err
}
return nil
}),
}
msg := fmt.Sprintf("Enter a domain, which must be beneath %s ", proj.Network.Subdomain)
return &survey.Input{Message: msg, Default: def}, opts
}
func fileSurvey(def string, item *api.ConfigurationItem) (prompt survey.Prompt, opts []survey.AskOpt) {
opts = []survey.AskOpt{}
if !item.Optional {
opts = append(opts, survey.WithValidator(survey.Required))
}
prompt = &survey.Input{
Message: "select a file (use tab to list files in the directory):",
Default: def,
Suggest: func(toComplete string) []string {
path, err := homedir.Expand(toComplete)
if err != nil {
path = toComplete
}
files, _ := filepath.Glob(CleanPath(path) + "*")
return files
},
}
return
}
func CleanPath(path string) string {
if strings.HasSuffix(path, "/") {
return path
}
fi, err := os.Stat(path)
if err != nil {
return path
}
if fi.Mode().IsDir() {
return path + string(filepath.Separator)
}
return path
}
func bucketSurvey(def string, proj *manifest.ProjectManifest, context *manifest.Context) (survey.Prompt, []survey.AskOpt) {
prompt := "Enter a globally unique object store bucket name "
if proj.BucketPrefix != "" {
prompt = fmt.Sprintf("Enter a globally unique bucket name, will be prefixed with %s-%s-", proj.BucketPrefix, proj.Cluster)
}
opts := []survey.AskOpt{
survey.WithValidator(func(val interface{}) error {
res, _ := val.(string)
name := bucketName(res, proj)
if len(name) > 63 || len(name) < 3 {
return fmt.Errorf("bucket name must be between 3 and 63 characters long")
}
if context.HasBucket(name) {
return fmt.Errorf("bucket %s has already been used elsewhere in this project, please chose another", name)
}
if err := utils.ValidateRegex(name, "[a-z][a-z0-9\\-]+[a-z0-9]", "Name must be a hyphenated alphanumeric string"); err != nil {
return err
}
return nil
}),
survey.WithValidator(survey.Required),
}
return &survey.Input{
Message: prompt,
Default: def,
}, opts
}