Commit 793a8de9 authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

commands: Refactored to make Request contain command path

parent 86bc450b
......@@ -37,11 +37,11 @@ func (c *Command) Register(id string, sub *Command) error {
}
// Call invokes the command at the given subcommand path
func (c *Command) Call(path []string, req *Request) *Response {
func (c *Command) Call(req *Request) *Response {
cmd := c
res := &Response{req: req}
options, err := cmd.GetOptions(path)
options, err := cmd.GetOptions(req.path)
if err != nil {
res.SetError(err, Client)
return res
......@@ -82,6 +82,7 @@ func (c *Command) GetOptions(path []string) (map[string]Option, error) {
copy(options, c.Options)
options = append(options, globalOptions...)
// a nil path means this command, not a subcommand (same as an empty path)
if path != nil {
for i, id := range path {
cmd := c.Sub(id)
......
......@@ -13,7 +13,7 @@ func TestOptionValidation(t *testing.T) {
req := NewRequest()
req.options["foo"] = 5
res := cmd.Call(nil, req)
res := cmd.Call(req)
if res.Error == nil {
t.Error("Should have failed (unrecognized command)")
}
......@@ -21,21 +21,21 @@ func TestOptionValidation(t *testing.T) {
req = NewRequest()
req.options["beep"] = 5
req.options["b"] = 10
res = cmd.Call(nil, req)
res = cmd.Call(req)
if res.Error == nil {
t.Error("Should have failed (duplicate options)")
}
req = NewRequest()
req.options["beep"] = "foo"
res = cmd.Call(nil, req)
res = cmd.Call(req)
if res.Error == nil {
t.Error("Should have failed (incorrect type)")
}
req = NewRequest()
req.options["beep"] = 5
res = cmd.Call(nil, req)
res = cmd.Call(req)
if res.Error != nil {
t.Error("Should have passed")
}
......@@ -43,7 +43,7 @@ func TestOptionValidation(t *testing.T) {
req = NewRequest()
req.options["beep"] = 5
req.options["boop"] = "test"
res = cmd.Call(nil, req)
res = cmd.Call(req)
if res.Error != nil {
t.Error("Should have passed")
}
......@@ -51,14 +51,14 @@ func TestOptionValidation(t *testing.T) {
req = NewRequest()
req.options["b"] = 5
req.options["B"] = "test"
res = cmd.Call(nil, req)
res = cmd.Call(req)
if res.Error != nil {
t.Error("Should have passed")
}
req = NewRequest()
req.options["enc"] = "json"
res = cmd.Call(nil, req)
res = cmd.Call(req)
if res.Error != nil {
t.Error("Should have passed")
}
......
......@@ -2,10 +2,15 @@ package commands
// Request represents a call to a command from a consumer
type Request struct {
path []string
options map[string]interface{}
arguments []string
}
func (r *Request) Path() []string {
return r.path
}
func (r *Request) Option(name string) interface{} {
return r.options[name]
}
......@@ -24,6 +29,7 @@ func (r *Request) Arguments() []string {
func NewRequest() *Request {
return &Request{
make([]string, 0),
make(map[string]interface{}),
make([]string, 0),
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment