@cryptotaxi247 / kubo / commits / 38f8f1c27

commands: Added a Format function to Command, for creating human-readable output based on a Response

Matt Bell committed Oct 28, 2014 at 19:28 UTC 38f8f1c279cd95183a464144be1383ccc70b1fbd
1 file changed +8
commands/command.go
+8
@@ -14,18 +14,26 @@ var log = u.Logger("command")
14 // It reads from the Request, and writes results to the Response.
15 type Function func(Request, Response)
16
17 +// Formatter is a function that takes in a Response, and returns a human-readable string
18 +// (or an error on failure)
19 +// MAYBE_TODO: maybe this should be a io.Reader instead of a string?
20 +type Formatter func(Response) (string, error)
21 +
22 // Command is a runnable command, with input arguments and options (flags).
23 // It can also have Subcommands, to group units of work into sets.
24 type Command struct {
25 Help string
26 Options []Option
27 Run Function
28 + Format Formatter
29 Subcommands map[string]*Command
30 }
31
32 // ErrNotCallable signals a command that cannot be called.
33 var ErrNotCallable = errors.New("This command can't be called directly. Try one of its subcommands.")
34
35 +var ErrNoFormatter = errors.New("This command cannot be formatted to plain text")
36 +
37 // Call invokes the command for the given Request
38 func (c *Command) Call(req Request) Response {
39 res := NewResponse(req)