forked from raystack/salt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prompt.go
73 lines (57 loc) · 1.41 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
package prompt
import (
"fmt"
"github.com/AlecAivazis/survey/v2"
)
type Prompter interface {
Select(string, string, []string) (int, error)
MultiSelect(string, string, []string) (int, error)
Input(string, string) (string, error)
Confirm(string, bool) (bool, error)
}
func New() Prompter {
return &surveyPrompter{}
}
type surveyPrompter struct {
}
func (p *surveyPrompter) ask(q survey.Prompt, response interface{}) error {
err := survey.AskOne(q, response)
if err == nil {
return nil
}
return fmt.Errorf("could not prompt: %w", err)
}
func (p *surveyPrompter) Select(message, defaultValue string, options []string) (result int, err error) {
q := &survey.Select{
Message: message,
Default: defaultValue,
Options: options,
PageSize: 20,
}
err = p.ask(q, &result)
return
}
func (p *surveyPrompter) MultiSelect(message, defaultValue string, options []string) (result int, err error) {
q := &survey.MultiSelect{
Message: message,
Default: defaultValue,
Options: options,
PageSize: 20,
}
err = p.ask(q, &result)
return
}
func (p *surveyPrompter) Input(prompt, defaultValue string) (result string, err error) {
err = p.ask(&survey.Input{
Message: prompt,
Default: defaultValue,
}, &result)
return
}
func (p *surveyPrompter) Confirm(prompt string, defaultValue bool) (result bool, err error) {
err = p.ask(&survey.Confirm{
Message: prompt,
Default: defaultValue,
}, &result)
return
}