Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 2153cbb

Browse files
author
David Chung
authored
fix bool parsing from prompt and flag (#463)
Signed-off-by: David Chung <[email protected]>
1 parent c465c19 commit 2153cbb

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

pkg/cli/local/context.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ func missing(t string, v interface{}) bool {
151151
return true
152152
}
153153

154+
func parseBool(text string) (bool, error) {
155+
if b, err := strconv.ParseBool(text); err == nil {
156+
return b, nil
157+
}
158+
switch text {
159+
case "y", "Y", "yes", "ok", "OK":
160+
return true, nil
161+
case "n", "N", "no", "nope":
162+
return false, nil
163+
}
164+
v, err := strconv.Atoi(text)
165+
return v > 0, err
166+
}
167+
154168
func (c *Context) prompt(prompt, ftype string) (interface{}, error) {
155169
input := bufio.NewReader(c.input)
156170
fmt.Fprintf(os.Stderr, "%s ", prompt)
@@ -159,24 +173,29 @@ func (c *Context) prompt(prompt, ftype string) (interface{}, error) {
159173
switch ftype {
160174
case "string":
161175
return text, nil
162-
case "int":
163-
return strconv.Atoi(text)
164176
case "float":
165177
return strconv.ParseFloat(text, 64)
166-
case "bool":
167-
if b, err := strconv.ParseBool(text); err == nil {
168-
return b, nil
178+
case "int":
179+
if i, err := strconv.Atoi(text); err == nil {
180+
return i, nil
169181
}
170-
switch text {
171-
case "y", "Y", "yes", "ok", "OK":
172-
return true, nil
182+
// special case -- int can be used to implement a bool if a default is not provided
183+
// so we need to handle parsing int from text for purpose of determining a bool
184+
b, err := parseBool(text)
185+
if err != nil {
186+
return b, err
173187
}
174-
if v, err := strconv.Atoi(text); err == nil {
175-
return v > 0, nil
188+
if b {
189+
return 1, nil
190+
}
191+
return 0, nil
192+
case "bool":
193+
if b, err := parseBool(text); err == nil {
194+
return b, nil
176195
}
177196
return nil, fmt.Errorf("cannot parse input for boolean: %v", text)
178197
}
179-
return nil, nil
198+
return nil, nil // don't err, just pass through
180199
}
181200

182201
// Funcs returns the template functions

0 commit comments

Comments
 (0)