-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.go
204 lines (162 loc) · 3.34 KB
/
cli.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
package regression
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"testing"
"time"
)
const notEmpty = "NOT_EMPTY"
const terminalName = "TERMINAL_NAME"
const previousAmount = "PREVIOUS_AMOUNT"
const newTxRef = "TX_REF"
var previousTxRef = txRefN(-1)
func txRefN(n int) string {
return refString(txRefPrefix, n)
}
var previousTxID = txIDN(-1)
func txIDN(n int) string {
return refString(txIDPrefix, n)
}
var previousBatchID = batchIDN(-1)
func batchIDN(n int) string {
return refString(batchIDPrefix, n)
}
func tokenN(n int) string {
return refString(tokenPrefix, n)
}
func customerIDN(n int) string {
return refString(customerIDPrefix, n)
}
const (
txRefPrefix = "TX_REF["
txIDPrefix = "TX_ID["
batchIDPrefix = "BATCH_ID["
tokenPrefix = "TOKEN["
customerIDPrefix = "CUSTOMER_ID["
)
func refString(prefix string, n int) string {
return fmt.Sprintf(prefix+"%d]", n)
}
const defaultFlatRateFee = 0.035
const defaultPerTransactionFee = 50
type cli struct {
path string
t *testing.T
}
type style int
func (s style) String() string {
return strconv.Itoa(int(s))
}
const (
noColor style = iota + 30
red
green
yellow
blue
magenta
cyan
)
const (
normal style = iota
bold
underline
blink
)
func format(styles ...style) string {
result := "\x1b["
if len(styles) == 0 {
return result + "0m"
}
for i, s := range styles {
result += s.String()
if i+1 < len(styles) {
result += ";"
}
}
return result + "m"
}
func (app *TestRunner) setup(instructions string, pause bool) {
if instructions == "" {
return
}
fmt.Println("\nSteps: " + format(magenta) + instructions + format() + "\n")
if pause {
fmt.Println(format(green) + "Press 'Enter' to continue" + format())
f, err := os.Open("/dev/tty")
if err != nil {
app.log.Fatal(err)
}
defer f.Close()
bufio.NewReader(f).ReadBytes('\n')
}
}
func (app *TestRunner) validate(v validation) error {
if v.prompt == "" {
return nil
}
if v.serve != "" {
showInBrowser(v.serve)
}
if prompt(v.prompt, !v.expect) != v.expect {
return fmt.Errorf("expected: %v", v.expect)
}
return nil
}
func prompt(msg string, def bool) bool {
var s string
if def {
s = "\n%s%s Y/n:%s "
} else {
s = "\n%s%s y/N:%s "
}
fmt.Printf(s, format(yellow), msg, format())
f, err := os.Open("/dev/tty")
if err != nil {
return false
}
defer f.Close()
res, _ := bufio.NewReader(f).ReadBytes('\n')
if def {
return !strings.HasPrefix(strings.ToLower(string(res)), "n")
}
return strings.HasPrefix(strings.ToLower(string(res)), "y")
}
func multiprompt(choices []string, def int) int {
fmt.Printf("\n%s%s: %s", format(yellow), strings.Join(choices, ", "), format())
f, err := os.Open("/dev/tty")
if err != nil {
return def
}
defer f.Close()
for {
res, _ := bufio.NewReader(f).ReadBytes('\n')
if len(strings.TrimSpace(string(res))) == 0 {
return def
}
for i, choice := range choices {
for _, c := range strings.ToLower(choice) {
if 'a' <= c && c <= 'z' {
if rune(strings.ToLower(string(res))[0]) == c {
return i
}
break
}
}
}
}
}
func wait(duration time.Duration) {
for i := duration; i > 0; i -= time.Second {
fmt.Printf("\x1b[2K\r" + format(yellow) + "Wait " + i.String() + format())
time.Sleep(1 * time.Second)
}
fmt.Printf("\n")
}
type validation struct {
prompt string
serve string
expect bool
}