commands: Changed Request arguments to a []interface{}
Matt Bell committed
Nov 2, 2014 at 16:55 UTC
827f1dd0b01847727ff97c2ce3d772cb61e2d47e
6 files changed
+36
-20
commands/argument.go
new
+14
@@ -0,0 +1,14 @@
1
+package commands
2
+
3
+type ArgumentType int
4
+
5
+const (
6
+ ArgString ArgumentType = iota
7
+ ArgPath
8
+)
9
+
10
+type Argument struct {
11
+ Name string
12
+ Type ArgumentType
13
+ Required, Variadic bool
14
+}
commands/cli/parse.go
+2
-2
@@ -65,9 +65,9 @@ func parsePath(input []string, root *cmds.Command) ([]string, []string, *cmds.Co
65
66
// parseOptions parses the raw string values of the given options
67
// returns the parsed options as strings, along with the CLI args
68
-func parseOptions(input []string) (map[string]interface{}, []string, error) {
68
+func parseOptions(input []string) (map[string]interface{}, []interface{}, error) {
69
opts := make(map[string]interface{})
70
- args := []string{}
70
+ args := []interface{}{}
71
72
for i := 0; i < len(input); i++ {
73
blob := input[i]
commands/command.go
+1
@@ -24,6 +24,7 @@ type Formatter func(Response) (string, error)
24
type Command struct {
25
Help string
26
Options []Option
27
+ Arguments []Argument
28
Run Function
29
Format Formatter
30
Type interface{}
commands/http/client.go
+5
-2
@@ -44,8 +44,11 @@ func Send(req cmds.Request) (cmds.Response, error) {
44
for k, v := range req.Options() {
45
query += "&" + k + "=" + v.(string)
46
}
47
- for _, v := range req.Arguments() {
48
- query += "&arg=" + v
47
+ for _, arg := range req.Arguments() {
48
+ s, ok := arg.(string)
49
+ if ok {
50
+ query += "&arg=" + s
51
+ }
52
}
53
54
httpRes, err := http.Post(url+query, "application/octet-stream", req.Stream())
commands/http/parse.go
+9
-11
@@ -10,7 +10,7 @@ import (
10
// Parse parses the data in a http.Request and returns a command Request object
11
func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
12
path := strings.Split(r.URL.Path, "/")[3:]
13
- args := make([]string, 0)
13
+ args := make([]interface{}, 0)
14
15
cmd, err := root.Get(path[:len(path)-1])
16
if err != nil {
@@ -34,28 +34,26 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
34
opts, args2 := parseOptions(r)
35
args = append(args, args2...)
36
37
- // TODO: make a way to send opts/args in request body
38
- // (e.g. if form-data or form-urlencoded, then treat the same as querystring)
39
- // for now, to be simple, we just use the whole request body as the input stream
40
- // (r.Body will be nil if there is no request body, like in GET requests)
41
- in := r.Body
42
-
43
- return cmds.NewRequest(path, opts, args, in, cmd), nil
37
+ return cmds.NewRequest(path, opts, args, nil, cmd), nil
38
}
39
46
-func parseOptions(r *http.Request) (map[string]interface{}, []string) {
40
+func parseOptions(r *http.Request) (map[string]interface{}, []interface{}) {
41
opts := make(map[string]interface{})
48
- var args []string
42
+ args := make([]interface{}, 0)
43
44
query := r.URL.Query()
45
for k, v := range query {
46
if k == "arg" {
53
- args = v
47
+ for _, s := range v {
48
+ args = append(args, interface{}(s))
49
+ }
50
} else {
51
opts[k] = v[0]
52
}
53
}
54
55
+ // TODO: create multipart streams for file args
56
+
57
// default to setting encoding to JSON
58
_, short := opts[cmds.EncShort]
59
_, long := opts[cmds.EncLong]
commands/request.go
+5
-5
@@ -24,7 +24,7 @@ type Request interface {
24
Option(name string) (interface{}, bool)
25
Options() map[string]interface{}
26
SetOption(name string, val interface{})
27
- Arguments() []string
27
+ Arguments() []interface{} // TODO: make argument value type instead of using interface{}
28
Stream() io.Reader
29
SetStream(io.Reader)
30
Context() *Context
@@ -37,7 +37,7 @@ type Request interface {
37
type request struct {
38
path []string
39
options optMap
40
- arguments []string
40
+ arguments []interface{}
41
in io.Reader
42
cmd *Command
43
ctx Context
@@ -69,7 +69,7 @@ func (r *request) SetOption(name string, val interface{}) {
69
}
70
71
// Arguments returns the arguments slice
72
-func (r *request) Arguments() []string {
72
+func (r *request) Arguments() []interface{} {
73
return r.arguments
74
}
75
@@ -165,7 +165,7 @@ func NewEmptyRequest() Request {
165
}
166
167
// NewRequest returns a request initialized with given arguments
168
-func NewRequest(path []string, opts optMap, args []string, in io.Reader, cmd *Command) Request {
168
+func NewRequest(path []string, opts optMap, args []interface{}, in io.Reader, cmd *Command) Request {
169
if path == nil {
170
path = make([]string, 0)
171
}
@@ -173,7 +173,7 @@ func NewRequest(path []string, opts optMap, args []string, in io.Reader, cmd *Co
173
opts = make(map[string]interface{})
174
}
175
if args == nil {
176
- args = make([]string, 0)
176
+ args = make([]interface{}, 0)
177
}
178
return &request{path, opts, args, in, cmd, Context{}}
179
}