-
Notifications
You must be signed in to change notification settings - Fork 0
/
step.go
229 lines (174 loc) · 3.5 KB
/
step.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package surveyexpect
import (
"errors"
"sync"
"time"
"github.com/AlecAivazis/survey/v2/terminal"
)
// Step is an execution step for a survey.
type Step interface {
// Do runs the step.
Do(c Console) error
// String represents the step as a string.
String() string
}
// Steps is a chain of Step.
type Steps struct {
steps []Step
closed bool
mu sync.Mutex
}
// lock locks the steps from changing its state.
func (s *Steps) lock() {
s.mu.Lock()
}
// unlock releases the lock.
func (s *Steps) unlock() {
s.mu.Unlock()
}
// Close closes the steps.
func (s *Steps) Close() {
s.lock()
defer s.unlock()
s.closed = true
}
// Append appends an expectation to the sequence.
func (s *Steps) Append(more ...Step) *Steps { //nolint: unparam
s.lock()
defer s.unlock()
mustNotClosed(s.closed)
s.steps = append(s.steps, more...)
return s
}
// DoFirst runs the first step.
func (s *Steps) DoFirst(c Console) error {
if s.HasNothingToDo() {
return ErrNothingToDo
}
s.lock()
step := s.steps[0]
s.unlock()
if err := step.Do(c); err != nil {
isNotFinished := errors.Is(err, ErrNotFinished)
if !errors.Is(err, terminal.InterruptErr) && !isNotFinished {
return err
}
if isNotFinished {
return nil
}
}
// Remove the expectation from the queue if it is not recurrent.
s.lock()
defer s.unlock()
s.steps[0] = nil
s.steps = s.steps[1:]
return nil
}
// Do runs all the steps.
func (s *Steps) Do(c Console) error {
for {
if err := s.DoFirst(c); err != nil {
if IsNothingTodo(err) {
return nil
}
if !IsInterrupted(err) {
return err
}
}
<-time.After(ReactionTime)
}
}
// String represents the steps as a string.
func (s *Steps) String() string {
if s.HasNothingToDo() {
return ""
}
s.lock()
defer s.unlock()
var sb stringsBuilder
for i, step := range s.steps {
if i > 0 {
sb.WriteString("\n\n")
}
sb.WriteString(step.String())
}
return sb.String()
}
// Reset removes all the steps.
func (s *Steps) Reset() {
s.lock()
defer s.unlock()
s.steps = nil
}
// Len returns the number of steps.
func (s *Steps) Len() int {
s.lock()
defer s.unlock()
return len(s.steps)
}
// HasNothingToDo checks whether the steps are not empty.
func (s *Steps) HasNothingToDo() bool {
return s.Len() == 0
}
// ExpectationsWereMet checks whether all queued expectations were met in order.
// If any of them was not met - an error is returned.
func (s *Steps) ExpectationsWereMet() error {
if s.HasNothingToDo() {
return nil
}
//nolint:goerr113
return errors.New(s.String())
}
func steps(steps ...Step) *Steps {
return &Steps{
steps: steps,
}
}
// InlineSteps is for internal steps and they are part of an expectation.
type InlineSteps struct {
*Steps
}
// String represents the answer as a string.
func (s *InlineSteps) String() string {
if s.HasNothingToDo() {
return ""
}
s.lock()
defer s.unlock()
var sb stringsBuilder
for i, step := range s.steps {
if i > 0 {
sb.WriteRune('\n')
}
sb.WriteString(step.String())
}
return sb.String()
}
func inlineSteps(inlineSteps ...Step) *InlineSteps {
return &InlineSteps{
Steps: steps(inlineSteps...),
}
}
func totalTimes(times ...int) int {
cnt := len(times)
switch cnt {
case 0:
return 1
case 1:
return times[0]
default:
var result int
for i := 0; i < cnt; i++ {
result += times[i]
}
return result
}
}
func repeatStep(s Step, times ...int) []Step {
cnt := totalTimes(times...)
result := make([]Step, 0, cnt)
for i := 0; i < cnt; i++ {
result = append(result, s)
}
return result
}