-
Notifications
You must be signed in to change notification settings - Fork 32
/
prompts.go
342 lines (296 loc) · 8.4 KB
/
prompts.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
package cmd
import (
"context"
"fmt"
"strings"
"github.com/asaskevich/govalidator"
"github.com/arigatomachine/cli/api"
"github.com/arigatomachine/cli/errs"
"github.com/arigatomachine/cli/identity"
"github.com/arigatomachine/cli/promptui"
)
const slugPattern = "^[a-z][a-z0-9\\-\\_]{0,63}$"
const namePattern = "^[a-zA-Z\\s,\\.'\\-pL]{1,64}$"
const inviteCodePattern = "^[0-9a-ht-zjkmnpqr]{10}$"
const verifyCodePattern = "^[0-9a-ht-zjkmnpqr]{9}$"
func validateSlug(slugType string) promptui.ValidateFunc {
msg := slugType + " names can only use a-z, 0-9, hyphens and underscores"
err := promptui.NewValidationError(msg)
return func(input string) error {
if govalidator.StringMatches(input, slugPattern) {
return nil
}
return err
}
}
func validateInviteCode(input string) error {
if govalidator.StringMatches(input, inviteCodePattern) {
return nil
}
return promptui.NewValidationError("Please enter a valid invite code")
}
// NamePrompt prompts the user to input a person's name
func NamePrompt(override *string, defaultValue string, autoAccept bool) (string, error) {
var prompt promptui.Prompt
label := "Name"
if override != nil {
label = *override
}
if autoAccept {
err := validateSlug(strings.ToLower(label))(defaultValue)
if err != nil {
fmt.Println(promptui.FailedValue(label, defaultValue))
} else {
fmt.Println(promptui.SuccessfulValue(label, defaultValue))
}
return defaultValue, err
}
prompt = promptui.Prompt{
Label: label,
Default: defaultValue,
Validate: validateSlug(strings.ToLower(label)),
}
return prompt.Run()
}
// VerificationPrompt prompts the user to input an email verify code
func VerificationPrompt() (string, error) {
prompt := promptui.Prompt{
Label: "Verification code",
Validate: func(input string) error {
input = strings.ToLower(input)
if govalidator.StringMatches(input, verifyCodePattern) {
return nil
}
return promptui.NewValidationError("Please enter a valid code")
},
}
return prompt.Run()
}
// SelectProjectPrompt prompts the user to select an org from a list, or enter a new name
func SelectProjectPrompt(projects []api.ProjectResult) (int, string, error) {
names := make([]string, len(projects))
for i, p := range projects {
names[i] = p.Body.Name
}
// Get the user's org selection
prompt := promptui.SelectWithAdd{
Label: "Select project",
Items: names,
AddLabel: "Create a new project",
Validate: validateSlug("project"),
}
return prompt.Run()
}
// SelectOrgPrompt prompts the user to select an org from a list, or enter a new name
func SelectOrgPrompt(orgs []api.OrgResult) (int, string, error) {
names := make([]string, len(orgs))
for i, o := range orgs {
names[i] = o.Body.Name
}
// Get the user's org selection
prompt := promptui.SelectWithAdd{
Label: "Select organization",
Items: names,
AddLabel: "Create a new organization",
Validate: validateSlug("org"),
}
return prompt.Run()
}
func handleSelectError(err error, generic string) error {
if err == promptui.ErrEOF || err == promptui.ErrInterrupt {
return err
}
return errs.NewExitError(generic)
}
// SelectCreateProject prompts the user to select a project from at list of projects
// populated via api request.
//
// The user may select to create a new project, or they may preselect an project
// via a non-empty name parameter.
//
// It returns the object of the selected project (if created a new project was not chosed),
// the name of the selected project, and a boolean indicating if a new project should
// be created.
func SelectCreateProject(c context.Context, client *api.Client, orgID *identity.ID, name string) (*api.ProjectResult, string, bool, error) {
var projects []api.ProjectResult
var err error
if orgID != nil {
// Get the list of projects the user has access to in the specified org
projects, err = listProjects(&c, client, orgID, nil)
if err != nil {
return nil, "", false, errs.NewExitError("Error fetching projects list.")
}
}
var idx int
if name == "" {
idx, name, err = SelectProjectPrompt(projects)
if err != nil {
return nil, "", false, err
}
} else {
found := false
for i, p := range projects {
if p.Body.Name == name {
found = true
idx = i
break
}
}
if !found {
fmt.Println(promptui.FailedValue("Project name", name))
return nil, "", false, errs.NewExitError("Project not found.")
}
fmt.Println(promptui.SuccessfulValue("Project name", name))
}
if idx == promptui.SelectedAdd {
return nil, name, true, nil
}
return &projects[idx], name, false, nil
}
// SelectCreateOrg prompts the user to select an org from at list of orgs
// populated via api request.
//
// The user may select to create a new org, or they may preselect an org
// via a non-empty name parameter.
//
// It returns the object of the selected org (if created a new org was not chosed),
// the name of the selected org, and a boolean indicating if a new org should
// be created.
func SelectCreateOrg(c context.Context, client *api.Client, name string) (*api.OrgResult, string, bool, error) {
// Get the list of orgs the user has access to
orgs, err := client.Orgs.List(c)
if err != nil {
return nil, "", false, errs.NewExitError("Error fetching orgs list")
}
var idx int
if name == "" {
idx, name, err = SelectOrgPrompt(orgs)
if err != nil {
return nil, "", false, err
}
} else {
found := false
for i, o := range orgs {
if o.Body.Name == name {
found = true
idx = i
break
}
}
if !found {
fmt.Println(promptui.FailedValue("Org name", name))
return nil, "", false, errs.NewExitError("Org not found")
}
fmt.Println(promptui.SuccessfulValue("Org name", name))
}
if idx == promptui.SelectedAdd {
return nil, name, true, nil
}
return &orgs[idx], name, false, nil
}
// PasswordPrompt prompts the user to input a password value
func PasswordPrompt(shouldConfirm bool) (string, error) {
prompt := promptui.Prompt{
Label: "Password",
Mask: '●',
Validate: func(input string) error {
length := len(input)
if length >= 8 {
return nil
}
if length > 0 {
return promptui.NewValidationError("Passwords must be at least 8 characters")
}
return promptui.NewValidationError("Please enter your password")
},
}
password, err := prompt.Run()
if err != nil {
return "", err
}
if !shouldConfirm {
return password, err
}
prompt = promptui.Prompt{
Label: "Confirm Password",
Mask: '●',
Validate: func(input string) error {
if len(input) > 0 {
if input != password {
return promptui.NewValidationError("Passwords do not match")
}
return nil
}
return promptui.NewValidationError("Please confirm your password")
},
}
_, err = prompt.Run()
if err != nil {
return "", err
}
return password, nil
}
// EmailPrompt prompts the user to input an email
func EmailPrompt(defaultValue string) (string, error) {
prompt := promptui.Prompt{
Label: "Email",
Validate: func(input string) error {
if govalidator.IsEmail(input) {
return nil
}
return promptui.NewValidationError("Please enter a valid email address")
},
}
if defaultValue != "" {
prompt.Default = defaultValue
}
return prompt.Run()
}
// UsernamePrompt prompts the user to input a person's name
func UsernamePrompt() (string, error) {
prompt := promptui.Prompt{
Label: "Username",
Validate: func(input string) error {
if govalidator.StringMatches(input, slugPattern) {
return nil
}
return promptui.NewValidationError("Please enter a valid username")
},
}
return prompt.Run()
}
// FullNamePrompt prompts the user to input a person's name
func FullNamePrompt() (string, error) {
prompt := promptui.Prompt{
Label: "Name",
Validate: func(input string) error {
if govalidator.StringMatches(input, namePattern) {
return nil
}
return promptui.NewValidationError("Please enter a valid name")
},
}
return prompt.Run()
}
// InviteCodePrompt prompts the user to input an invite code
func InviteCodePrompt(defaultValue string) (string, error) {
prompt := promptui.Prompt{
Label: "Invite Code",
Default: defaultValue,
Validate: validateInviteCode,
}
return prompt.Run()
}
// SelectAcceptAction prompts the user to select an org from a list, or enter a new name
func SelectAcceptAction() (int, string, error) {
names := []string{
"Login",
"Signup",
}
// Get the user's org selection
prompt := promptui.Select{
Label: "Do you want to login or create an account?",
Items: names,
}
return prompt.Run()
}