提交 81f29257 作者: Matt Bell 提交者: Juan Batiz-Benet

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

上级 507192ef
......@@ -4,23 +4,26 @@ import (
"fmt"
"strings"
"github.com/jbenet/go-ipfs/commands"
cmds "github.com/jbenet/go-ipfs/commands"
)
// Parse parses the input commandline string (cmd, flags, and args).
// returns the corresponding command Request object.
func Parse(input []string, root *commands.Command) (commands.Request, error) {
path, input := parsePath(input, root)
func Parse(input []string, root *cmds.Command) (cmds.Request, error) {
path, input, cmd := parsePath(input, root)
opts, args, err := parseOptions(input)
if err != nil {
return nil, err
}
return commands.NewRequest(path, opts, args, nil), nil
// TODO: figure out how to know when to read given file(s) as an input stream
// (instead of filename arg string)
return cmds.NewRequest(path, opts, args, nil, cmd), nil
}
// parsePath gets the command path from the command line input
func parsePath(input []string, root *commands.Command) ([]string, []string) {
func parsePath(input []string, root *cmds.Command) ([]string, []string, *cmds.Command) {
cmd := root
i := 0
......@@ -29,15 +32,16 @@ func parsePath(input []string, root *commands.Command) ([]string, []string) {
break
}
cmd := cmd.Subcommand(blob)
if cmd == nil {
sub := cmd.Subcommand(blob)
if sub == nil {
break
}
cmd = sub
i++
}
return input[:i], input[i:]
return input[:i], input[i:], cmd
}
// parseOptions parses the raw string values of the given options
......
......@@ -10,18 +10,24 @@ import (
// Parse parses the data in a http.Request and returns a command Request object
func Parse(r *http.Request) (cmds.Request, error) {
// TODO: take root cmd as a param, like the commands/cli Parse
path := strings.Split(r.URL.Path, "/")[3:]
args := make([]string, 0)
if cmd, err := commands.Root.Get(path[:len(path)-1]); err != nil {
cmd, err := commands.Root.Get(path[:len(path)-1])
if err != nil {
// 404 if there is no command at that path
return nil, ErrNotFound
} else if cmd.Subcommand(path[len(path)-1]) == nil {
} else if sub := cmd.Subcommand(path[len(path)-1]); sub == nil {
// if the last string in the path isn't a subcommand, use it as an argument
// e.g. /objects/Qabc12345 (we are passing "Qabc12345" to the "objects" command)
args = append(args, path[len(path)-1])
path = path[:len(path)-1]
} else {
cmd = sub
}
opts, args2 := parseOptions(r)
......@@ -33,7 +39,7 @@ func Parse(r *http.Request) (cmds.Request, error) {
// (r.Body will be nil if there is no request body, like in GET requests)
in := r.Body
return cmds.NewRequest(path, opts, args, in), nil
return cmds.NewRequest(path, opts, args, in, cmd), nil
}
func parseOptions(r *http.Request) (map[string]interface{}, []string) {
......
......@@ -29,6 +29,7 @@ type Request interface {
SetStream(io.Reader)
Context() *Context
SetContext(Context)
Command() *Command
ConvertOptions(options map[string]Option) error
}
......@@ -38,6 +39,7 @@ type request struct {
options optMap
arguments []string
in io.Reader
cmd *Command
ctx Context
}
......@@ -89,6 +91,10 @@ func (r *request) SetContext(ctx Context) {
r.ctx = ctx
}
func (r *request) Command() *Command {
return r.cmd
}
type converter func(string) (interface{}, error)
var converters = map[reflect.Kind]converter{
......@@ -155,11 +161,11 @@ func (r *request) ConvertOptions(options map[string]Option) error {
// NewEmptyRequest initializes an empty request
func NewEmptyRequest() Request {
return NewRequest(nil, nil, nil, nil)
return NewRequest(nil, nil, nil, nil, nil)
}
// NewRequest returns a request initialized with given arguments
func NewRequest(path []string, opts optMap, args []string, in io.Reader) Request {
func NewRequest(path []string, opts optMap, args []string, in io.Reader, cmd *Command) Request {
if path == nil {
path = make([]string, 0)
}
......@@ -169,5 +175,5 @@ func NewRequest(path []string, opts optMap, args []string, in io.Reader) Request
if args == nil {
args = make([]string, 0)
}
return &request{path, opts, args, in, Context{}}
return &request{path, opts, args, in, cmd, Context{}}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论