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

Commit a4210cd

Browse files
author
David Chung
authored
cli context utility functions (#481)
Signed-off-by: David Chung <[email protected]>
1 parent b26c3bd commit a4210cd

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

pkg/cli/context.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ func (c *Context) getFromFlag(name, ftype, desc string, def interface{}) (interf
145145
return nil, nil
146146
}
147147

148-
// returns true if the value v is missing of the type t
149-
func missing(t string, v interface{}) bool {
148+
// Missing returns true if the value v is missing of the type t
149+
func Missing(t string, v interface{}) bool {
150150
if v == nil {
151151
return true
152152
}
@@ -177,7 +177,8 @@ func parseBool(text string) (bool, error) {
177177
return v > 0, err
178178
}
179179

180-
func (c *Context) prompt(prompt, ftype string, optional ...interface{}) (interface{}, error) {
180+
// Prompt handles prompting the user using the given prompt message, type string and optional values.
181+
func Prompt(in io.Reader, prompt, ftype string, optional ...interface{}) (interface{}, error) {
181182
def, label := "", ""
182183
if len(optional) > 0 {
183184
def = fmt.Sprintf("%v", optional[0])
@@ -186,7 +187,7 @@ func (c *Context) prompt(prompt, ftype string, optional ...interface{}) (interfa
186187
}
187188
}
188189

189-
input := bufio.NewReader(c.input)
190+
input := bufio.NewReader(in)
190191
fmt.Fprintf(os.Stderr, "%s %s: ", prompt, label)
191192
text, _ := input.ReadString('\n')
192193
text = strings.Trim(text, " \t\n")
@@ -255,11 +256,11 @@ func (c *Context) Funcs() []template.Function {
255256
}
256257
// The last value in the optional var args is the value from the previous
257258
// pipeline.
258-
if len(optional) > 0 && !missing(ftype, optional[len(optional)-1]) {
259+
if len(optional) > 0 && !Missing(ftype, optional[len(optional)-1]) {
259260
return optional[len(optional)-1], nil
260261
}
261262

262-
return c.prompt(prompt, ftype, optional...)
263+
return Prompt(c.input, prompt, ftype, optional...)
263264
},
264265
},
265266
}

pkg/cli/context_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
)
1212

1313
func TestMissing(t *testing.T) {
14-
require.True(t, missing("string", ""))
15-
require.True(t, missing("int", 0))
16-
require.True(t, missing("float", 0.))
17-
require.True(t, missing("bool", none))
18-
require.False(t, missing("bool", false))
19-
require.False(t, missing("bool", true))
14+
require.True(t, Missing("string", ""))
15+
require.True(t, Missing("int", 0))
16+
require.True(t, Missing("float", 0.))
17+
require.True(t, Missing("bool", none))
18+
require.False(t, Missing("bool", false))
19+
require.False(t, Missing("bool", true))
2020
}
2121

2222
func TestContext(t *testing.T) {

0 commit comments

Comments
 (0)