-
Notifications
You must be signed in to change notification settings - Fork 0
/
addUser.prompt.go
73 lines (68 loc) Β· 1.49 KB
/
addUser.prompt.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
package prompt
import (
"errors"
"regexp"
"unicode"
"github.com/AlecAivazis/survey/v2"
)
func isEmailValid(e string) bool {
emailRegex := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
return emailRegex.MatchString(e)
}
func isNumber(s string) bool {
for _, c := range s {
if !unicode.IsDigit(c) {
return false
}
}
return true
}
// the questions to ask
func AddUserPrompt(groupChoices []string) []*survey.Question {
return []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{Message: "What is your full name?"},
Validate: survey.Required,
Transform: survey.Title,
},
{
Name: "email",
Prompt: &survey.Input{Message: "What is your email address?"},
Validate: func(val interface{}) error {
if str, ok := val.(string); !ok || !isEmailValid(str) {
return errors.New("input is not valid email address")
}
return nil
},
},
{
Name: "password",
Prompt: &survey.Password{
Message: "Your password?",
},
Validate: survey.MinLength(8),
},
{
Name: "group",
Prompt: &survey.MultiSelect{
Message: "Which groups this user should belong to?",
Options: groupChoices,
},
Validate: survey.Required,
},
{
Name: "expire",
Prompt: &survey.Input{
Message: "Expires in (days)?",
Default: "365",
},
Validate: func(val interface{}) error {
if str, ok := val.(string); !ok || !isNumber(str) {
return errors.New("input is not valid number")
}
return nil
},
},
}
}