/
prompt.go
52 lines (48 loc) · 984 Bytes
/
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
package templates
import (
"fmt"
"github.com/manifoldco/promptui"
"regexp"
"strconv"
)
func NewPromptSelect(label string, items []string) (string, error) {
prompt := promptui.Select{
Label: label,
Items: items,
}
_, result, err := prompt.Run()
return result, err
}
func NewPromptPassword() (string, error) {
validate := func(input string) error {
if len(input) < 1 {
return fmt.Errorf("password must not be empty")
}
return nil
}
prompt := promptui.Prompt{
Label: "Password",
Validate: validate,
Mask: '*',
}
return prompt.Run()
}
func NewPromptNumber() (int, error) {
validate := func(input string) error {
r := regexp.MustCompile("^\\d+$")
if !r.Match([]byte(input)) {
return fmt.Errorf("mobile Number must not be empty")
}
return nil
}
prompt := promptui.Prompt{
Label: "Mobile Number",
Validate: validate,
Mask: '*',
}
num, err := prompt.Run()
if err != nil {
return 0, err
}
return strconv.Atoi(num)
}