1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-20 19:19:06 +08:00

commands: Changed Request arguments to a []interface{}

This commit is contained in:
Matt Bell
2014-11-02 16:55:13 -08:00
committed by Juan Batiz-Benet
parent 30e968754e
commit 827f1dd0b0
6 changed files with 36 additions and 20 deletions

14
commands/argument.go Normal file
View File

@ -0,0 +1,14 @@
package commands
type ArgumentType int
const (
ArgString ArgumentType = iota
ArgPath
)
type Argument struct {
Name string
Type ArgumentType
Required, Variadic bool
}

View File

@ -65,9 +65,9 @@ func parsePath(input []string, root *cmds.Command) ([]string, []string, *cmds.Co
// parseOptions parses the raw string values of the given options // parseOptions parses the raw string values of the given options
// returns the parsed options as strings, along with the CLI args // returns the parsed options as strings, along with the CLI args
func parseOptions(input []string) (map[string]interface{}, []string, error) { func parseOptions(input []string) (map[string]interface{}, []interface{}, error) {
opts := make(map[string]interface{}) opts := make(map[string]interface{})
args := []string{} args := []interface{}{}
for i := 0; i < len(input); i++ { for i := 0; i < len(input); i++ {
blob := input[i] blob := input[i]

View File

@ -24,6 +24,7 @@ type Formatter func(Response) (string, error)
type Command struct { type Command struct {
Help string Help string
Options []Option Options []Option
Arguments []Argument
Run Function Run Function
Format Formatter Format Formatter
Type interface{} Type interface{}

View File

@ -44,8 +44,11 @@ func Send(req cmds.Request) (cmds.Response, error) {
for k, v := range req.Options() { for k, v := range req.Options() {
query += "&" + k + "=" + v.(string) query += "&" + k + "=" + v.(string)
} }
for _, v := range req.Arguments() { for _, arg := range req.Arguments() {
query += "&arg=" + v s, ok := arg.(string)
if ok {
query += "&arg=" + s
}
} }
httpRes, err := http.Post(url+query, "application/octet-stream", req.Stream()) httpRes, err := http.Post(url+query, "application/octet-stream", req.Stream())

View File

@ -10,7 +10,7 @@ import (
// Parse parses the data in a http.Request and returns a command Request object // Parse parses the data in a http.Request and returns a command Request object
func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) { func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
path := strings.Split(r.URL.Path, "/")[3:] path := strings.Split(r.URL.Path, "/")[3:]
args := make([]string, 0) args := make([]interface{}, 0)
cmd, err := root.Get(path[:len(path)-1]) cmd, err := root.Get(path[:len(path)-1])
if err != nil { if err != nil {
@ -34,28 +34,26 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
opts, args2 := parseOptions(r) opts, args2 := parseOptions(r)
args = append(args, args2...) args = append(args, args2...)
// TODO: make a way to send opts/args in request body return cmds.NewRequest(path, opts, args, nil, cmd), nil
// (e.g. if form-data or form-urlencoded, then treat the same as querystring)
// for now, to be simple, we just use the whole request body as the input stream
// (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, cmd), nil
} }
func parseOptions(r *http.Request) (map[string]interface{}, []string) { func parseOptions(r *http.Request) (map[string]interface{}, []interface{}) {
opts := make(map[string]interface{}) opts := make(map[string]interface{})
var args []string args := make([]interface{}, 0)
query := r.URL.Query() query := r.URL.Query()
for k, v := range query { for k, v := range query {
if k == "arg" { if k == "arg" {
args = v for _, s := range v {
args = append(args, interface{}(s))
}
} else { } else {
opts[k] = v[0] opts[k] = v[0]
} }
} }
// TODO: create multipart streams for file args
// default to setting encoding to JSON // default to setting encoding to JSON
_, short := opts[cmds.EncShort] _, short := opts[cmds.EncShort]
_, long := opts[cmds.EncLong] _, long := opts[cmds.EncLong]

View File

@ -24,7 +24,7 @@ type Request interface {
Option(name string) (interface{}, bool) Option(name string) (interface{}, bool)
Options() map[string]interface{} Options() map[string]interface{}
SetOption(name string, val interface{}) SetOption(name string, val interface{})
Arguments() []string Arguments() []interface{} // TODO: make argument value type instead of using interface{}
Stream() io.Reader Stream() io.Reader
SetStream(io.Reader) SetStream(io.Reader)
Context() *Context Context() *Context
@ -37,7 +37,7 @@ type Request interface {
type request struct { type request struct {
path []string path []string
options optMap options optMap
arguments []string arguments []interface{}
in io.Reader in io.Reader
cmd *Command cmd *Command
ctx Context ctx Context
@ -69,7 +69,7 @@ func (r *request) SetOption(name string, val interface{}) {
} }
// Arguments returns the arguments slice // Arguments returns the arguments slice
func (r *request) Arguments() []string { func (r *request) Arguments() []interface{} {
return r.arguments return r.arguments
} }
@ -165,7 +165,7 @@ func NewEmptyRequest() Request {
} }
// NewRequest returns a request initialized with given arguments // NewRequest returns a request initialized with given arguments
func NewRequest(path []string, opts optMap, args []string, in io.Reader, cmd *Command) Request { func NewRequest(path []string, opts optMap, args []interface{}, in io.Reader, cmd *Command) Request {
if path == nil { if path == nil {
path = make([]string, 0) path = make([]string, 0)
} }
@ -173,7 +173,7 @@ func NewRequest(path []string, opts optMap, args []string, in io.Reader, cmd *Co
opts = make(map[string]interface{}) opts = make(map[string]interface{})
} }
if args == nil { if args == nil {
args = make([]string, 0) args = make([]interface{}, 0)
} }
return &request{path, opts, args, in, cmd, Context{}} return &request{path, opts, args, in, cmd, Context{}}
} }