-
Notifications
You must be signed in to change notification settings - Fork 0
/
stub_prompter.go
181 lines (163 loc) · 3.75 KB
/
stub_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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package ui
import (
"fmt"
"strings"
"sync"
"github.com/spf13/cast"
)
// Stub is a stubbed Prompter invocation.
type Stub struct {
Matched bool
Matcher Matcher
Responder Responder
}
func NewStubPrompter(ios *IOStreams) *StubPrompter {
return &StubPrompter{
ios: ios,
stubs: []*Stub{},
}
}
// StubPrompter is an implementation of Prompter that invokes stubbed prompts.
type StubPrompter struct {
ios *IOStreams
mu sync.Mutex
stubs []*Stub
}
var (
_ Prompter = &StubPrompter{}
)
// Confirm prompts for a boolean yes/no value.
func (sp *StubPrompter) Confirm(msg string, value bool, opts ...PromptOpt) (bool, error) {
prompt := Prompt{
Type: PromptTypeConfirm,
Message: msg,
Value: value,
}
stub, err := sp.match(prompt)
if err != nil {
return false, err
}
response, err := stub.Responder(prompt)
if err != nil {
return false, err
}
casted := cast.ToBool(response)
formatted := "No"
if casted {
formatted = "Yes"
}
fmt.Fprintf(sp.ios.Out, "? %s %s\n", msg, formatted)
return casted, nil
}
// Input prompts for single string value.
func (sp *StubPrompter) Input(msg string, value string, opts ...PromptOpt) (string, error) {
prompt := Prompt{
Type: PromptTypeInput,
Message: msg,
Value: value,
}
stub, err := sp.match(prompt)
if err != nil {
return "", err
}
response, err := stub.Responder(prompt)
if err != nil {
return "", err
}
casted := cast.ToString(response)
fmt.Fprintf(sp.ios.Out, "? %s %s\n", msg, casted)
return casted, nil
}
// MultiSelect prompts for a slice of string values w/ a fixed set of options.
func (sp *StubPrompter) MultiSelect(msg string, options []string, values []string, opts ...PromptOpt) ([]string, error) {
prompt := Prompt{
Type: PromptTypeMultiSelect,
Message: msg,
Value: values,
}
stub, err := sp.match(prompt)
if err != nil {
return nil, err
}
response, err := stub.Responder(prompt)
if err != nil {
return nil, err
}
casted := cast.ToStringSlice(response)
fmt.Fprintf(sp.ios.Out, "? %s %s\n", msg, strings.Join(casted, ", "))
return casted, nil
}
// Select prompts for single string value w/ a fixed set of options.
func (sp *StubPrompter) Select(msg string, options []string, value string, opts ...PromptOpt) (string, error) {
prompt := Prompt{
Type: PromptTypeSelect,
Message: msg,
Value: value,
}
stub, err := sp.match(prompt)
if err != nil {
return "", err
}
response, err := stub.Responder(prompt)
if err != nil {
return "", err
}
casted := cast.ToString(response)
fmt.Fprintf(sp.ios.Out, "? %s %s\n", msg, casted)
return response.(string), nil
}
func (e *StubPrompter) match(prompt Prompt) (*Stub, error) {
e.mu.Lock()
var stub *Stub
var matches []*Stub
for _, s := range e.stubs {
if s.Matcher(prompt) {
matches = append(matches, s)
if s.Matched {
continue
}
if stub == nil {
s.Matched = true
stub = s
}
}
}
if stub == nil {
e.mu.Unlock()
n := len(matches)
if n == 0 {
return nil, fmt.Errorf("no registered stubs matching: %s", prompt)
} else {
return nil, fmt.Errorf("wanted %d of only %d stubs matching: %s", n+1, n, prompt)
}
}
e.mu.Unlock()
return stub, nil
}
// RegisterStub registers a new stub for the given matcher/responder pair.
func (sp *StubPrompter) RegisterStub(matcher Matcher, responder Responder) *StubPrompter {
sp.mu.Lock()
sp.stubs = append(sp.stubs, &Stub{
Matcher: matcher,
Responder: responder,
})
sp.mu.Unlock()
return sp
}
// VerifyStubs fails the test if there are unmatched stubs.
func (sp *StubPrompter) VerifyStubs(test testable) {
test.Helper()
n := 0
for _, s := range sp.stubs {
if !s.Matched {
n++
}
}
if n > 0 {
test.Errorf("found %d unmatched stub(s)", n)
}
}
type testable interface {
Errorf(string, ...interface{})
Helper()
}