Commit 827f1dd0 authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

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

parent 30e96875
package commands
type ArgumentType int
const (
ArgString ArgumentType = iota
ArgPath
)
type Argument struct {
Name string
Type ArgumentType
Required, Variadic bool
}
......@@ -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
// 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{})
args := []string{}
args := []interface{}{}
for i := 0; i < len(input); i++ {
blob := input[i]
......
......@@ -24,6 +24,7 @@ type Formatter func(Response) (string, error)
type Command struct {
Help string
Options []Option
Arguments []Argument
Run Function
Format Formatter
Type interface{}
......
......@@ -44,8 +44,11 @@ func Send(req cmds.Request) (cmds.Response, error) {
for k, v := range req.Options() {
query += "&" + k + "=" + v.(string)
}
for _, v := range req.Arguments() {
query += "&arg=" + v
for _, arg := range req.Arguments() {
s, ok := arg.(string)
if ok {
query += "&arg=" + s
}
}
httpRes, err := http.Post(url+query, "application/octet-stream", req.Stream())
......
......@@ -10,7 +10,7 @@ import (
// 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) {
path := strings.Split(r.URL.Path, "/")[3:]
args := make([]string, 0)
args := make([]interface{}, 0)
cmd, err := root.Get(path[:len(path)-1])
if err != nil {
......@@ -34,28 +34,26 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
opts, args2 := parseOptions(r)
args = append(args, args2...)
// TODO: make a way to send opts/args in request body
// (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
return cmds.NewRequest(path, opts, args, nil, 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{})
var args []string
args := make([]interface{}, 0)
query := r.URL.Query()
for k, v := range query {
if k == "arg" {
args = v
for _, s := range v {
args = append(args, interface{}(s))
}
} else {
opts[k] = v[0]
}
}
// TODO: create multipart streams for file args
// default to setting encoding to JSON
_, short := opts[cmds.EncShort]
_, long := opts[cmds.EncLong]
......
......@@ -24,7 +24,7 @@ type Request interface {
Option(name string) (interface{}, bool)
Options() map[string]interface{}
SetOption(name string, val interface{})
Arguments() []string
Arguments() []interface{} // TODO: make argument value type instead of using interface{}
Stream() io.Reader
SetStream(io.Reader)
Context() *Context
......@@ -37,7 +37,7 @@ type Request interface {
type request struct {
path []string
options optMap
arguments []string
arguments []interface{}
in io.Reader
cmd *Command
ctx Context
......@@ -69,7 +69,7 @@ func (r *request) SetOption(name string, val interface{}) {
}
// Arguments returns the arguments slice
func (r *request) Arguments() []string {
func (r *request) Arguments() []interface{} {
return r.arguments
}
......@@ -165,7 +165,7 @@ func NewEmptyRequest() Request {
}
// 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 {
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{})
}
if args == nil {
args = make([]string, 0)
args = make([]interface{}, 0)
}
return &request{path, opts, args, in, cmd, Context{}}
}
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