Commit 970b570f authored by Matt Bell's avatar Matt Bell

commands: Refactored Command#Run function signature to (req Request, res Response)

parent d3f17bd4
...@@ -14,7 +14,7 @@ var log = u.Logger("command") ...@@ -14,7 +14,7 @@ var log = u.Logger("command")
// Function is the type of function that Commands use. // Function is the type of function that Commands use.
// It reads from the Request, and writes results to the Response. // It reads from the Request, and writes results to the Response.
type Function func(Request) (interface{}, error) type Function func(Request, Response)
// Marshaler is a function that takes in a Response, and returns an io.Reader // Marshaler is a function that takes in a Response, and returns an io.Reader
// (or an error on failure) // (or an error on failure)
...@@ -95,21 +95,12 @@ func (c *Command) Call(req Request) Response { ...@@ -95,21 +95,12 @@ func (c *Command) Call(req Request) Response {
return res return res
} }
output, err := cmd.Run(req) cmd.Run(req, res)
if err != nil { if res.Error() != nil {
// if returned error is a commands.Error, use its error code
// otherwise, just default the code to ErrNormal
switch e := err.(type) {
case *Error:
res.SetError(e, e.Code)
case Error:
res.SetError(e, e.Code)
default:
res.SetError(err, ErrNormal)
}
return res return res
} }
output := res.Output()
isChan := false isChan := false
actualType := reflect.TypeOf(output) actualType := reflect.TypeOf(output)
if actualType != nil { if actualType != nil {
...@@ -140,7 +131,6 @@ func (c *Command) Call(req Request) Response { ...@@ -140,7 +131,6 @@ func (c *Command) Call(req Request) Response {
} }
} }
res.SetOutput(output)
return res return res
} }
......
...@@ -2,8 +2,8 @@ package commands ...@@ -2,8 +2,8 @@ package commands
import "testing" import "testing"
func noop(req Request) (interface{}, error) { func noop(req Request, res Response) {
return nil, nil return
} }
func TestOptionValidation(t *testing.T) { func TestOptionValidation(t *testing.T) {
......
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