-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
prompt.go
80 lines (72 loc) · 1.82 KB
/
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
74
75
76
77
78
79
80
package util
import (
"github.com/AlecAivazis/survey/v2"
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
)
func GetYesNoPrompt(message string) (promptResp bool, err error) {
var op errors.Op = "util.GetYesNoPrompt"
prompt := &survey.Confirm{
Message: message,
Default: true,
}
err = survey.AskOne(prompt, &promptResp)
if err != nil {
return promptResp, errors.E(op, err)
}
return promptResp, nil
}
func GetSelectPrompt(message string, options []string) (selection string, err error) {
var op errors.Op = "util.GetSelectPrompt"
prompt := &survey.Select{
Message: message,
Options: options,
}
err = survey.AskOne(prompt, &selection)
if err != nil {
return selection, errors.E(op, err)
}
return selection, nil
}
func GetInputPrompt(message string) (input string, err error) {
var op errors.Op = "util.GetInputPrompt"
prompt := &survey.Input{
Message: message,
}
err = survey.AskOne(prompt, &input)
if err != nil {
return input, errors.E(op, err)
}
return input, nil
}
func GetInputPromptWithDefault(message string, def string) (input string, err error) {
var op errors.Op = "util.GetInputPromptWithDefault"
prompt := &survey.Input{
Message: message,
Default: def,
}
err = survey.AskOne(prompt, &input)
if err != nil {
return input, errors.E(op, err)
}
return input, nil
}
func validateDirPath(a interface{}) error {
var op errors.Op = "util.validateDirPath"
err := FSCheckIfDirPathExists(a.(string))
if err != nil {
return errors.E(op, err)
}
return nil
}
func GetFSPathPrompt(message string, def string) (input string, err error) {
var op errors.Op = "util.GetFSPathPrompt"
prompt := &survey.Input{
Message: message,
Default: def,
}
err = survey.AskOne(prompt, &input, survey.WithValidator(validateDirPath))
if err != nil {
return input, errors.E(op, err)
}
return input, nil
}