-
Notifications
You must be signed in to change notification settings - Fork 70
/
prompt_ui_prompter.go
56 lines (43 loc) · 1.08 KB
/
prompt_ui_prompter.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
package ux
import (
survey "github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/newrelic/newrelic-cli/internal/install/types"
"github.com/newrelic/newrelic-cli/internal/utils"
)
type PromptUIPrompter struct{}
func NewPromptUIPrompter() *PromptUIPrompter {
return &PromptUIPrompter{}
}
func (p *PromptUIPrompter) PromptYesNo(msg string) (bool, error) {
yes := false
prompt := &survey.Confirm{
Default: true,
Message: msg,
}
err := survey.AskOne(prompt, &yes)
if err != nil {
return false, err
}
return yes, nil
}
func (p *PromptUIPrompter) MultiSelect(msg string, options []string) ([]string, error) {
defaults := utils.MakeRange(0, len(options)-1)
selected := []string{}
prompt := &survey.MultiSelect{
Message: msg,
Options: options,
Default: defaults,
}
err := survey.AskOne(prompt, &selected)
if err != nil {
if err == terminal.InterruptErr {
return nil, types.ErrInterrupt
}
if err.Error() == terminal.InterruptErr.Error() {
return nil, types.ErrInterrupt
}
return nil, err
}
return selected, nil
}