forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
149 lines (123 loc) · 4.04 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package ui
import (
"errors"
"fmt"
"io"
"strconv"
"github.com/vito/go-interact/interact"
"github.com/vito/go-interact/interact/terminal"
)
const sigIntExitCode = 130
var ErrInvalidIndex = errors.New("invalid list index")
type InvalidChoiceError struct {
Choice string
}
func (InvalidChoiceError) Error() string {
return "Some error"
}
//go:generate counterfeiter . Resolver
type Resolver interface {
Resolve(dst interface{}) error
SetIn(io.Reader)
SetOut(io.Writer)
}
// DisplayBoolPrompt outputs the prompt and waits for user input. It only
// allows for a boolean response. A default boolean response can be set with
// defaultResponse.
func (ui *UI) DisplayBoolPrompt(defaultResponse bool, template string, templateValues ...map[string]interface{}) (bool, error) {
ui.terminalLock.Lock()
defer ui.terminalLock.Unlock()
response := defaultResponse
interactivePrompt := ui.Interactor.NewInteraction(ui.TranslateText(template, templateValues...))
interactivePrompt.SetIn(ui.In)
interactivePrompt.SetOut(ui.OutForInteration)
err := interactivePrompt.Resolve(&response)
if isInterrupt(err) {
ui.Exiter.Exit(sigIntExitCode)
}
return response, err
}
// DisplayOptionalTextPrompt outputs the prompt and waits for user input.
func (ui *UI) DisplayOptionalTextPrompt(defaultValue string, template string, templateValues ...map[string]interface{}) (string, error) {
interactivePrompt := ui.Interactor.NewInteraction(ui.TranslateText(template, templateValues...))
var value = defaultValue
interactivePrompt.SetIn(ui.In)
interactivePrompt.SetOut(ui.OutForInteration)
err := interactivePrompt.Resolve(&value)
if isInterrupt(err) {
ui.Exiter.Exit(sigIntExitCode)
}
return value, err
}
// DisplayPasswordPrompt outputs the prompt and waits for user input. Hides
// user's response from the screen.
func (ui *UI) DisplayPasswordPrompt(template string, templateValues ...map[string]interface{}) (string, error) {
ui.terminalLock.Lock()
defer ui.terminalLock.Unlock()
var password interact.Password
interactivePrompt := ui.Interactor.NewInteraction(ui.TranslateText(template, templateValues...))
interactivePrompt.SetIn(ui.In)
interactivePrompt.SetOut(ui.OutForInteration)
err := interactivePrompt.Resolve(interact.Required(&password))
if isInterrupt(err) {
ui.Exiter.Exit(sigIntExitCode)
}
return string(password), err
}
// DisplayTextMenu lets the user choose from a list of options, either by name
// or by number.
func (ui *UI) DisplayTextMenu(choices []string, promptTemplate string, templateValues ...map[string]interface{}) (string, error) {
for i, c := range choices {
t := fmt.Sprintf("%d. %s", i+1, c)
ui.DisplayText(t)
}
translatedPrompt := ui.TranslateText(promptTemplate, templateValues...)
interactivePrompt := ui.Interactor.NewInteraction(translatedPrompt)
interactivePrompt.SetIn(ui.In)
interactivePrompt.SetOut(ui.OutForInteration)
var value string = "enter to skip"
err := interactivePrompt.Resolve(&value)
if isInterrupt(err) {
ui.Exiter.Exit(sigIntExitCode)
}
if err != nil {
return "", err
}
if value == "enter to skip" {
return "", nil
}
i, err := strconv.Atoi(value)
if err != nil {
if contains(choices, value) {
return value, nil
}
return "", InvalidChoiceError{Choice: value}
}
if i > len(choices) || i <= 0 {
return "", ErrInvalidIndex
}
return choices[i-1], nil
}
// DisplayTextPrompt outputs the prompt and waits for user input.
func (ui *UI) DisplayTextPrompt(template string, templateValues ...map[string]interface{}) (string, error) {
interactivePrompt := ui.Interactor.NewInteraction(ui.TranslateText(template, templateValues...))
var value string
interactivePrompt.SetIn(ui.In)
interactivePrompt.SetOut(ui.OutForInteration)
err := interactivePrompt.Resolve(interact.Required(&value))
if isInterrupt(err) {
ui.Exiter.Exit(sigIntExitCode)
}
return value, err
}
func contains(s []string, v string) bool {
for _, x := range s {
if x == v {
return true
}
}
return false
}
func isInterrupt(err error) bool {
return err == interact.ErrKeyboardInterrupt || err == terminal.ErrKeyboardInterrupt
}