/
command_context.go
68 lines (57 loc) · 1.95 KB
/
command_context.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
package cmd
import "strconv"
import "github.com/chzyer/readline"
// CommandContext encapsulates the state of a command execution lifecycle
type CommandContext struct {
args []string
result interface{}
err error
}
// IsError returns whether any step within the command execution lifecycle encountered an error
func (c *CommandContext) IsError() bool {
return c.err != nil
}
// HasResult returns whether any step within the command execution returned a valid response
func (c *CommandContext) HasResult() bool {
return c.GetResult() != nil
}
// GetIntArg returns an int cli command argument at the specified index
func (c *CommandContext) GetIntArg(index int) (result int, err error) {
return strconv.Atoi(c.args[index])
}
// GetStringArg returns a string cli command argument at the specified index
func (c *CommandContext) GetStringArg(index int) (result string) {
return c.args[index]
}
// GetBooleanArg returns a bool cli command argument at the specified index
func (c *CommandContext) GetBooleanArg(index int) (result bool, err error) {
return strconv.ParseBool(c.args[index])
}
// GetResult returns the result of the command execution lifecycle
func (c *CommandContext) GetResult() interface{} {
return c.result
}
// SetResult sets the result of the command execution lifecycle
func (c *CommandContext) SetResult(result interface{}, err error) {
c.result = result
c.err = err
}
// ReadLine reads a line from the command CLI
func (c *CommandContext) ReadLine(prompt string) (result string, err error) {
readLine, err := readline.New(prompt)
if err == nil {
defer readLine.Close()
return readLine.Readline()
}
return "", err
}
// ReadPassword reads a password string from the command CLI
func (c *CommandContext) ReadPassword(prompt string) (result string, err error) {
readLine, err := readline.New(prompt)
if err == nil {
defer readLine.Close()
password, err := readLine.ReadPassword(prompt)
return string(password[:]), err
}
return "", err
}