@cryptotaxi247 / kubo / commits / 81f29257a

commands: Gave Requests a reference to the command they are being called on

Matt Bell committed Oct 28, 2014 at 19:27 UTC 81f29257a143eee542a575625397a25688c1f40d
3 files changed +30 -14
commands/cli/parse.go
+12 -8
@@ -4,23 +4,26 @@ import (
4 "fmt"
5 "strings"
6
7 - "github.com/jbenet/go-ipfs/commands"
7 + cmds "github.com/jbenet/go-ipfs/commands"
8 )
9
10 // Parse parses the input commandline string (cmd, flags, and args).
11 // returns the corresponding command Request object.
12 -func Parse(input []string, root *commands.Command) (commands.Request, error) {
13 - path, input := parsePath(input, root)
12 +func Parse(input []string, root *cmds.Command) (cmds.Request, error) {
13 + path, input, cmd := parsePath(input, root)
14 opts, args, err := parseOptions(input)
15 if err != nil {
16 return nil, err
17 }
18
19 - return commands.NewRequest(path, opts, args, nil), nil
19 + // TODO: figure out how to know when to read given file(s) as an input stream
20 + // (instead of filename arg string)
21 +
22 + return cmds.NewRequest(path, opts, args, nil, cmd), nil
23 }
24
25 // parsePath gets the command path from the command line input
23 -func parsePath(input []string, root *commands.Command) ([]string, []string) {
26 +func parsePath(input []string, root *cmds.Command) ([]string, []string, *cmds.Command) {
27 cmd := root
28 i := 0
29
@@ -29,15 +32,16 @@ func parsePath(input []string, root *commands.Command) ([]string, []string) {
32 break
33 }
34
32 - cmd := cmd.Subcommand(blob)
33 - if cmd == nil {
35 + sub := cmd.Subcommand(blob)
36 + if sub == nil {
37 break
38 }
39 + cmd = sub
40
41 i++
42 }
43
40 - return input[:i], input[i:]
44 + return input[:i], input[i:], cmd
45 }
46
47 // parseOptions parses the raw string values of the given options
commands/http/parse.go
+9 -3
@@ -10,18 +10,24 @@ import (
10
11 // Parse parses the data in a http.Request and returns a command Request object
12 func Parse(r *http.Request) (cmds.Request, error) {
13 + // TODO: take root cmd as a param, like the commands/cli Parse
14 +
15 path := strings.Split(r.URL.Path, "/")[3:]
16 args := make([]string, 0)
17
16 - if cmd, err := commands.Root.Get(path[:len(path)-1]); err != nil {
18 + cmd, err := commands.Root.Get(path[:len(path)-1])
19 + if err != nil {
20 // 404 if there is no command at that path
21 return nil, ErrNotFound
22
20 - } else if cmd.Subcommand(path[len(path)-1]) == nil {
23 + } else if sub := cmd.Subcommand(path[len(path)-1]); sub == nil {
24 // if the last string in the path isn't a subcommand, use it as an argument
25 // e.g. /objects/Qabc12345 (we are passing "Qabc12345" to the "objects" command)
26 args = append(args, path[len(path)-1])
27 path = path[:len(path)-1]
28 +
29 + } else {
30 + cmd = sub
31 }
32
33 opts, args2 := parseOptions(r)
@@ -33,7 +39,7 @@ func Parse(r *http.Request) (cmds.Request, error) {
39 // (r.Body will be nil if there is no request body, like in GET requests)
40 in := r.Body
41
36 - return cmds.NewRequest(path, opts, args, in), nil
42 + return cmds.NewRequest(path, opts, args, in, cmd), nil
43 }
44
45 func parseOptions(r *http.Request) (map[string]interface{}, []string) {
commands/request.go
+9 -3
@@ -29,6 +29,7 @@ type Request interface {
29 SetStream(io.Reader)
30 Context() *Context
31 SetContext(Context)
32 + Command() *Command
33
34 ConvertOptions(options map[string]Option) error
35 }
@@ -38,6 +39,7 @@ type request struct {
39 options optMap
40 arguments []string
41 in io.Reader
42 + cmd *Command
43 ctx Context
44 }
45
@@ -89,6 +91,10 @@ func (r *request) SetContext(ctx Context) {
91 r.ctx = ctx
92 }
93
94 +func (r *request) Command() *Command {
95 + return r.cmd
96 +}
97 +
98 type converter func(string) (interface{}, error)
99
100 var converters = map[reflect.Kind]converter{
@@ -155,11 +161,11 @@ func (r *request) ConvertOptions(options map[string]Option) error {
161
162 // NewEmptyRequest initializes an empty request
163 func NewEmptyRequest() Request {
158 - return NewRequest(nil, nil, nil, nil)
164 + return NewRequest(nil, nil, nil, nil, nil)
165 }
166
167 // NewRequest returns a request initialized with given arguments
162 -func NewRequest(path []string, opts optMap, args []string, in io.Reader) Request {
168 +func NewRequest(path []string, opts optMap, args []string, in io.Reader, cmd *Command) Request {
169 if path == nil {
170 path = make([]string, 0)
171 }
@@ -169,5 +175,5 @@ func NewRequest(path []string, opts optMap, args []string, in io.Reader) Request
175 if args == nil {
176 args = make([]string, 0)
177 }
172 - return &request{path, opts, args, in, Context{}}
178 + return &request{path, opts, args, in, cmd, Context{}}
179 }