Commit 09d2277f authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

f -> run, Function type.

parent 7673ce6f
...@@ -6,11 +6,17 @@ import ( ...@@ -6,11 +6,17 @@ import (
"strings" "strings"
) )
// Command is an object that defines a command. // Function is the type of function that Commands use.
// It reads from the Request, and writes results to the Response.
type Function func(*Request, *Response)
// Command is a runnable command, with input arguments and options (flags).
// It can also have subcommands, to group units of work into sets.
type Command struct { type Command struct {
Help string Help string
Options []Option Options []Option
f func(*Request, *Response)
run Function
subcommands map[string]*Command subcommands map[string]*Command
} }
...@@ -51,7 +57,7 @@ func (c *Command) Call(req *Request) *Response { ...@@ -51,7 +57,7 @@ func (c *Command) Call(req *Request) *Response {
} }
cmd := cmds[len(cmds)-1] cmd := cmds[len(cmds)-1]
if cmd.f == nil { if cmd.run == nil {
res.SetError(ErrNotCallable, ErrClient) res.SetError(ErrNotCallable, ErrClient)
return res return res
} }
...@@ -68,7 +74,7 @@ func (c *Command) Call(req *Request) *Response { ...@@ -68,7 +74,7 @@ func (c *Command) Call(req *Request) *Response {
return res return res
} }
cmd.f(req, res) cmd.run(req, res)
return res return res
} }
......
...@@ -8,7 +8,7 @@ func TestOptionValidation(t *testing.T) { ...@@ -8,7 +8,7 @@ func TestOptionValidation(t *testing.T) {
Option{[]string{"b", "beep"}, Int}, Option{[]string{"b", "beep"}, Int},
Option{[]string{"B", "boop"}, String}, Option{[]string{"B", "boop"}, String},
}, },
f: func(req *Request, res *Response) {}, run: func(req *Request, res *Response) {},
} }
req := NewEmptyRequest() req := NewEmptyRequest()
...@@ -84,35 +84,35 @@ func TestRegistration(t *testing.T) { ...@@ -84,35 +84,35 @@ func TestRegistration(t *testing.T) {
Options: []Option{ Options: []Option{
Option{[]string{"beep"}, Int}, Option{[]string{"beep"}, Int},
}, },
f: func(req *Request, res *Response) {}, run: func(req *Request, res *Response) {},
}, },
&Command{ &Command{
Options: []Option{ Options: []Option{
Option{[]string{"boop"}, Int}, Option{[]string{"boop"}, Int},
}, },
f: func(req *Request, res *Response) {}, run: func(req *Request, res *Response) {},
}, },
&Command{ &Command{
Options: []Option{ Options: []Option{
Option{[]string{"boop"}, String}, Option{[]string{"boop"}, String},
}, },
f: func(req *Request, res *Response) {}, run: func(req *Request, res *Response) {},
}, },
&Command{ &Command{
Options: []Option{ Options: []Option{
Option{[]string{"bop"}, String}, Option{[]string{"bop"}, String},
}, },
f: func(req *Request, res *Response) {}, run: func(req *Request, res *Response) {},
}, },
&Command{ &Command{
Options: []Option{ Options: []Option{
Option{[]string{"enc"}, String}, Option{[]string{"enc"}, String},
}, },
f: func(req *Request, res *Response) {}, run: func(req *Request, res *Response) {},
}, },
} }
......
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