-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword.go
193 lines (151 loc) · 3.77 KB
/
password.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
package surveyexpect
import (
"strings"
)
var (
_ Prompt = (*PasswordPrompt)(nil)
_ Answer = (*PasswordAnswer)(nil)
)
// PasswordPrompt is an expectation of survey.Password.
type PasswordPrompt struct {
*basePrompt
message string
answer Answer
}
// ShowHelp sets help for the expectation.
//
// Survey.ExpectPassword("Enter password:").
// ShowHelp("Your shiny password")
func (p *PasswordPrompt) ShowHelp(help string, options ...string) {
p.lock()
defer p.unlock()
p.answer = helpAnswer(help, options...)
p.timesLocked(1)
}
// Interrupt marks the answer is interrupted.
//
// Survey.ExpectPassword("Enter password:").
// Interrupt()
func (p *PasswordPrompt) Interrupt() {
p.lock()
defer p.unlock()
p.answer = interruptAnswer()
p.timesLocked(1)
}
// Answer sets the answer to the password prompt.
//
// Survey.ExpectPassword("Enter password:").
// Answer("hello world!")
func (p *PasswordPrompt) Answer(answer string) *PasswordAnswer {
p.lock()
defer p.unlock()
a := newPasswordAnswer(p, answer)
p.answer = a
return a
}
// Do runs the step.
func (p *PasswordPrompt) Do(c Console) error {
if _, err := c.ExpectString(p.message); err != nil {
return err
}
_ = waitForCursorTwice(c) //nolint: errcheck
err := p.answer.Do(c)
if err != nil && !IsInterrupted(err) {
return err
}
p.lock()
defer p.unlock()
p.repeatability--
p.totalCalls++
return p.isDoneLocked(err)
}
// String represents the expectation as a string.
func (p *PasswordPrompt) String() string {
var sb stringsBuilder
sb.WriteLabelLinef("Expect", "Password Prompt").
WriteLabelLinef("Message", "%q", p.message).
WriteLabelLinef("Answer", p.answer.String())
if p.repeatability > 0 && (p.totalCalls != 0 || p.repeatability != 1) {
sb.WriteLinef("(called: %d time(s), remaining: %d time(s))", p.totalCalls, p.repeatability)
}
return sb.String()
}
// Once indicates that the message should only be asked once.
//
// Survey.ExpectPassword("Enter password:").
// Answer("hello world!").
// Once()
func (p *PasswordPrompt) Once() *PasswordPrompt {
return p.Times(1)
}
// Twice indicates that the message should only be asked twice.
//
// Survey.ExpectPassword("Enter password:").
// Answer("hello world!").
// Twice()
func (p *PasswordPrompt) Twice() *PasswordPrompt {
return p.Times(2)
}
// Times indicates that the message should only be asked the indicated number of times.
//
// Survey.ExpectPassword("Enter password:").
// Answer("hello world!").
// Times(5)
func (p *PasswordPrompt) Times(i int) *PasswordPrompt {
p.times(i)
return p
}
// PasswordAnswer is an answer for password question.
type PasswordAnswer struct {
parent *PasswordPrompt
answer string
interrupted bool
}
// Do runs the step.
// nolint: errcheck,gosec,nolintlint
func (a *PasswordAnswer) Do(c Console) error {
if a.interrupted {
c.Send(a.answer)
c.ExpectEOF()
return nil
}
if a.answer == "" {
c.SendLine("")
return nil
}
c.Send(a.answer)
// Expect asterisks.
if _, err := c.ExpectString(strings.Repeat("*", len(a.answer))); err != nil {
return err
}
c.SendLine("")
return nil
}
// Interrupted expects the answer will be interrupted.
func (a *PasswordAnswer) Interrupted() {
a.parent.lock()
defer a.parent.unlock()
a.interrupted = true
}
// String represents the answer as a string.
func (a *PasswordAnswer) String() string {
var sb stringsBuilder
sb.Writef("%q", a.answer)
if a.interrupted {
sb.WriteString(" and get interrupted")
}
return sb.String()
}
func newPassword(parent *Survey, message string) *PasswordPrompt {
return &PasswordPrompt{
basePrompt: &basePrompt{parent: parent},
message: message,
answer: noAnswer(),
}
}
func newPasswordAnswer(parent *PasswordPrompt, answer string) *PasswordAnswer {
return &PasswordAnswer{
parent: parent,
answer: answer,
}
}