-
Notifications
You must be signed in to change notification settings - Fork 4
/
interactive.go
118 lines (90 loc) · 2.54 KB
/
interactive.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
package interactive
import (
"errors"
"log"
"path/filepath"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
)
func Ask(question string, validate survey.Validator) (string, error) {
var answer string
return answer, askPrompt(&survey.Input{
Message: question,
}, &answer, validate)
}
func AskWithHelp(question string, help string, validate survey.Validator) (string, error) {
prompt := NewInput(question)
prompt.Help = help
prompt.SetValidate(validate)
return prompt.AskString()
}
func AskInt32(question string, validate survey.Validator) (int32, error) {
var answer int32
return answer, askPrompt(&survey.Input{
Message: question,
}, &answer, validate)
}
func AskSecretWithHelp(question string, help string, validate survey.Validator) (string, error) {
var answer string
return answer, askPrompt(&survey.Password{
Message: question,
Help: help,
}, &answer, validate)
}
func AskPath(question string, value string, validate survey.Validator) (string, error) {
var answer string
return answer, askPrompt(&survey.Input{
Message: question,
Default: value,
Suggest: suggestPaths,
}, &answer, validate)
}
func Confirm(question string) (bool, error) {
var answer bool
return answer, askPrompt(&survey.Confirm{
Message: question,
}, &answer, nil)
}
func ConfirmWithHelp(question string, help string) (bool, error) {
var answer bool
return answer, askPrompt(&survey.Confirm{
Message: question,
Help: help,
}, &answer, nil)
}
func Choose(question string, items []string) (int, string, error) {
var answerIndex int
return answerIndex, items[answerIndex], askPrompt(&survey.Select{
Message: question,
Options: items,
}, &answerIndex, nil)
}
func ChooseWithSize(size int, question string, items []string) (int, string, error) {
var answerIndex int
return answerIndex, items[answerIndex], askPrompt(&survey.Select{
Message: question,
Options: items,
PageSize: size,
}, &answerIndex, nil)
}
func askPrompt(input survey.Prompt, answer any, validate survey.Validator) error {
// safeguard: it should really be handled upstream
if getSettings().NonInteractive {
return ErrNonInteractive
}
err := survey.AskOne(input, answer, withValidator(validate))
if errors.Is(err, terminal.InterruptErr) {
log.Fatal("interrupted")
}
return err
}
func withValidator(validate survey.Validator) survey.AskOpt {
if validate == nil {
return nil
}
return survey.WithValidator(validate)
}
func suggestPaths(toComplete string) []string {
files, _ := filepath.Glob(toComplete + "*")
return files
}