commands: Refactored to make Request contain command path
Matt Bell committed
Oct 14, 2014 at 16:25 UTC
793a8de9c248b91903f28eb9ad5ee6dfe78b1875
3 files changed
+16
-9
commands/command.go
+3
-2
@@ -37,11 +37,11 @@ func (c *Command) Register(id string, sub *Command) error {
37
}
38
39
// Call invokes the command at the given subcommand path
40
-func (c *Command) Call(path []string, req *Request) *Response {
40
+func (c *Command) Call(req *Request) *Response {
41
cmd := c
42
res := &Response{req: req}
43
44
- options, err := cmd.GetOptions(path)
44
+ options, err := cmd.GetOptions(req.path)
45
if err != nil {
46
res.SetError(err, Client)
47
return res
@@ -82,6 +82,7 @@ func (c *Command) GetOptions(path []string) (map[string]Option, error) {
82
copy(options, c.Options)
83
options = append(options, globalOptions...)
84
85
+ // a nil path means this command, not a subcommand (same as an empty path)
86
if path != nil {
87
for i, id := range path {
88
cmd := c.Sub(id)
commands/command_test.go
+7
-7
@@ -13,7 +13,7 @@ func TestOptionValidation(t *testing.T) {
13
14
req := NewRequest()
15
req.options["foo"] = 5
16
- res := cmd.Call(nil, req)
16
+ res := cmd.Call(req)
17
if res.Error == nil {
18
t.Error("Should have failed (unrecognized command)")
19
}
@@ -21,21 +21,21 @@ func TestOptionValidation(t *testing.T) {
21
req = NewRequest()
22
req.options["beep"] = 5
23
req.options["b"] = 10
24
- res = cmd.Call(nil, req)
24
+ res = cmd.Call(req)
25
if res.Error == nil {
26
t.Error("Should have failed (duplicate options)")
27
}
28
29
req = NewRequest()
30
req.options["beep"] = "foo"
31
- res = cmd.Call(nil, req)
31
+ res = cmd.Call(req)
32
if res.Error == nil {
33
t.Error("Should have failed (incorrect type)")
34
}
35
36
req = NewRequest()
37
req.options["beep"] = 5
38
- res = cmd.Call(nil, req)
38
+ res = cmd.Call(req)
39
if res.Error != nil {
40
t.Error("Should have passed")
41
}
@@ -43,7 +43,7 @@ func TestOptionValidation(t *testing.T) {
43
req = NewRequest()
44
req.options["beep"] = 5
45
req.options["boop"] = "test"
46
- res = cmd.Call(nil, req)
46
+ res = cmd.Call(req)
47
if res.Error != nil {
48
t.Error("Should have passed")
49
}
@@ -51,14 +51,14 @@ func TestOptionValidation(t *testing.T) {
51
req = NewRequest()
52
req.options["b"] = 5
53
req.options["B"] = "test"
54
- res = cmd.Call(nil, req)
54
+ res = cmd.Call(req)
55
if res.Error != nil {
56
t.Error("Should have passed")
57
}
58
59
req = NewRequest()
60
req.options["enc"] = "json"
61
- res = cmd.Call(nil, req)
61
+ res = cmd.Call(req)
62
if res.Error != nil {
63
t.Error("Should have passed")
64
}
commands/request.go
+6
@@ -2,10 +2,15 @@ package commands
2
3
// Request represents a call to a command from a consumer
4
type Request struct {
5
+ path []string
6
options map[string]interface{}
7
arguments []string
8
}
9
10
+func (r *Request) Path() []string {
11
+ return r.path
12
+}
13
+
14
func (r *Request) Option(name string) interface{} {
15
return r.options[name]
16
}
@@ -24,6 +29,7 @@ func (r *Request) Arguments() []string {
29
30
func NewRequest() *Request {
31
return &Request{
32
+ make([]string, 0),
33
make(map[string]interface{}),
34
make([]string, 0),
35
}