-
Notifications
You must be signed in to change notification settings - Fork 161
/
fake_ui.go
162 lines (117 loc) · 3.17 KB
/
fake_ui.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
package fakes
import (
"fmt"
"sync"
. "github.com/cloudfoundry/bosh-cli/ui/table"
)
type FakeUI struct {
Said []string
Errors []string
Blocks []string // keep as string to make ginkgo err msgs easier
Table Table
Tables []Table
AskedTextLabels []string
AskedText []Answer
AskedPasswordLabels []string
AskedPasswords []Answer
AskedChoiceCalled bool
AskedChoiceLabel string
AskedChoiceOptions []string
AskedChoiceChosens []int
AskedChoiceErrs []error
AskedConfirmationCalled bool
AskedConfirmationErr error
Interactive bool
Flushed bool
mutex sync.Mutex
}
type Answer struct {
Text string
Error error
}
func (ui *FakeUI) ErrorLinef(pattern string, args ...interface{}) {
ui.mutex.Lock()
defer ui.mutex.Unlock()
ui.Errors = append(ui.Errors, fmt.Sprintf(pattern, args...))
}
func (ui *FakeUI) PrintLinef(pattern string, args ...interface{}) {
ui.mutex.Lock()
defer ui.mutex.Unlock()
ui.Said = append(ui.Said, fmt.Sprintf(pattern, args...))
}
func (ui *FakeUI) BeginLinef(pattern string, args ...interface{}) {
ui.mutex.Lock()
defer ui.mutex.Unlock()
ui.Said = append(ui.Said, fmt.Sprintf(pattern, args...))
}
func (ui *FakeUI) EndLinef(pattern string, args ...interface{}) {
ui.mutex.Lock()
defer ui.mutex.Unlock()
ui.Said = append(ui.Said, fmt.Sprintf(pattern, args...))
}
func (ui *FakeUI) PrintBlock(block []byte) {
ui.mutex.Lock()
defer ui.mutex.Unlock()
ui.Blocks = append(ui.Blocks, string(block))
}
func (ui *FakeUI) PrintErrorBlock(block string) {
ui.mutex.Lock()
defer ui.mutex.Unlock()
ui.Blocks = append(ui.Blocks, block)
}
func (ui *FakeUI) PrintTable(table Table) {
ui.mutex.Lock()
defer ui.mutex.Unlock()
ui.Table = table
ui.Tables = append(ui.Tables, table)
}
func (ui *FakeUI) PrintTableFiltered(table Table, filterHeader []Header) {
ui.mutex.Lock()
defer ui.mutex.Unlock()
ui.Table = table
ui.Tables = append(ui.Tables, table)
}
func (ui *FakeUI) AskForText(label string) (string, error) {
ui.mutex.Lock()
defer ui.mutex.Unlock()
ui.AskedTextLabels = append(ui.AskedTextLabels, label)
answer := ui.AskedText[0]
ui.AskedText = ui.AskedText[1:]
return answer.Text, answer.Error
}
func (ui *FakeUI) AskForChoice(label string, options []string) (int, error) {
ui.mutex.Lock()
defer ui.mutex.Unlock()
ui.AskedChoiceCalled = true
ui.AskedChoiceLabel = label
ui.AskedChoiceOptions = options
chosen := ui.AskedChoiceChosens[0]
ui.AskedChoiceChosens = ui.AskedChoiceChosens[1:]
err := ui.AskedChoiceErrs[0]
ui.AskedChoiceErrs = ui.AskedChoiceErrs[1:]
return chosen, err
}
func (ui *FakeUI) AskForPassword(label string) (string, error) {
ui.mutex.Lock()
defer ui.mutex.Unlock()
ui.AskedPasswordLabels = append(ui.AskedPasswordLabels, label)
answer := ui.AskedPasswords[0]
ui.AskedPasswords = ui.AskedPasswords[1:]
return answer.Text, answer.Error
}
func (ui *FakeUI) AskForConfirmation() error {
ui.mutex.Lock()
defer ui.mutex.Unlock()
ui.AskedConfirmationCalled = true
return ui.AskedConfirmationErr
}
func (ui *FakeUI) IsInteractive() bool {
ui.mutex.Lock()
defer ui.mutex.Unlock()
return ui.Interactive
}
func (ui *FakeUI) Flush() {
ui.mutex.Lock()
defer ui.mutex.Unlock()
ui.Flushed = true
}