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

commands: Updated Command to use Response for output rather than (interface{}, error)

parent f31fd53d
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
type Command struct { type Command struct {
Help string Help string
Options []Option Options []Option
f func(*Request) (interface{}, error) f func(*Request, *Response)
subcommands map[string]*Command subcommands map[string]*Command
} }
...@@ -37,11 +37,12 @@ func (c *Command) Register(id string, sub *Command) error { ...@@ -37,11 +37,12 @@ func (c *Command) Register(id string, sub *Command) error {
} }
// Call invokes the command at the given subcommand path // Call invokes the command at the given subcommand path
func (c *Command) Call(path []string, req *Request) (interface{}, error) { func (c *Command) Call(path []string, req *Request) *Response {
options := make([]Option, len(c.Options)) options := make([]Option, len(c.Options))
copy(options, c.Options) copy(options, c.Options)
options = append(options, globalOptions...) options = append(options, globalOptions...)
cmd := c cmd := c
res := &Response{ req: req }
if path != nil { if path != nil {
for i, id := range path { for i, id := range path {
...@@ -49,7 +50,8 @@ func (c *Command) Call(path []string, req *Request) (interface{}, error) { ...@@ -49,7 +50,8 @@ func (c *Command) Call(path []string, req *Request) (interface{}, error) {
if cmd == nil { if cmd == nil {
pathS := strings.Join(path[0:i], "/") pathS := strings.Join(path[0:i], "/")
return nil, fmt.Errorf("Undefined command: '%s'", pathS) res.SetError(fmt.Errorf("Undefined command: '%s'", pathS), Client)
return res
} }
options = append(options, cmd.Options...) options = append(options, cmd.Options...)
...@@ -67,24 +69,29 @@ func (c *Command) Call(path []string, req *Request) (interface{}, error) { ...@@ -67,24 +69,29 @@ func (c *Command) Call(path []string, req *Request) (interface{}, error) {
opt, ok := optionsMap[k] opt, ok := optionsMap[k]
if !ok { if !ok {
return nil, fmt.Errorf("Unrecognized command option: '%s'", k) res.SetError(fmt.Errorf("Unrecognized command option: '%s'", k), Client)
return res
} }
for _, name := range opt.Names { for _, name := range opt.Names {
if _, ok = req.options[name]; name != k && ok { if _, ok = req.options[name]; name != k && ok {
return nil, fmt.Errorf("Duplicate command options were provided ('%s' and '%s')", res.SetError(fmt.Errorf("Duplicate command options were provided ('%s' and '%s')",
k, name) k, name), Client)
return res
} }
} }
kind := reflect.TypeOf(v).Kind() kind := reflect.TypeOf(v).Kind()
if kind != opt.Type { if kind != opt.Type {
return nil, fmt.Errorf("Option '%s' should be type '%s', but got type '%s'", res.SetError(fmt.Errorf("Option '%s' should be type '%s', but got type '%s'",
k, opt.Type.String(), kind.String()) k, opt.Type.String(), kind.String()), Client)
return res
} }
} }
return cmd.f(req) cmd.f(req, res)
return res
} }
// Sub returns the subcommand with the given id // Sub returns the subcommand with the given id
......
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