commands/cli: Open argument files when creating Requests (Moved out of HTTP client)
Matt Bell committed
Nov 2, 2014 at 18:48 UTC
ee2c76992a88cbe94b6eda9a3c28d3941b94d9c9
2 files changed
+37
-12
commands/cli/parse.go
+36
-6
@@ -3,6 +3,7 @@ package cli
3
import (
4
"errors"
5
"fmt"
6
+ "os"
7
"strings"
8
9
cmds "github.com/jbenet/go-ipfs/commands"
@@ -18,7 +19,7 @@ func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command,
19
maxLength := 0
20
for _, r := range roots {
21
path, input, cmd := parsePath(input, r)
21
- opts, args, err := parseOptions(input)
22
+ opts, stringArgs, err := parseOptions(input)
23
if err != nil {
24
return nil, nil, err
25
}
@@ -26,6 +27,12 @@ func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command,
27
length := len(path)
28
if length > maxLength {
29
maxLength = length
30
+
31
+ args, err := parseArgs(stringArgs, cmd)
32
+ if err != nil {
33
+ return nil, nil, err
34
+ }
35
+
36
req = cmds.NewRequest(path, opts, args, cmd)
37
root = r
38
}
@@ -35,9 +42,6 @@ func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command,
42
return nil, nil, errors.New("Not a valid subcommand")
43
}
44
38
- // TODO: figure out how to know when to read given file(s) as an input stream
39
- // (instead of filename arg string)
40
-
45
return req, root, nil
46
}
47
@@ -65,9 +69,9 @@ func parsePath(input []string, root *cmds.Command) ([]string, []string, *cmds.Co
69
70
// parseOptions parses the raw string values of the given options
71
// returns the parsed options as strings, along with the CLI args
68
-func parseOptions(input []string) (map[string]interface{}, []interface{}, error) {
72
+func parseOptions(input []string) (map[string]interface{}, []string, error) {
73
opts := make(map[string]interface{})
70
- args := []interface{}{}
74
+ args := []string{}
75
76
for i := 0; i < len(input); i++ {
77
blob := input[i]
@@ -100,3 +104,29 @@ func parseOptions(input []string) (map[string]interface{}, []interface{}, error)
104
105
return opts, args, nil
106
}
107
+
108
+// Note that the argument handling here is dumb, it does not do any error-checking.
109
+// (Arguments are further processed when the request is passed to the command to run)
110
+func parseArgs(stringArgs []string, cmd *cmds.Command) ([]interface{}, error) {
111
+ args := make([]interface{}, len(cmd.Arguments))
112
+
113
+ for i, arg := range cmd.Arguments {
114
+ // TODO: handle variadic args
115
+ if i >= len(stringArgs) {
116
+ break
117
+ }
118
+
119
+ if arg.Type == cmds.ArgString {
120
+ args[i] = stringArgs[i]
121
+
122
+ } else {
123
+ in, err := os.Open(stringArgs[i])
124
+ if err != nil {
125
+ return nil, err
126
+ }
127
+ args[i] = in
128
+ }
129
+ }
130
+
131
+ return args, nil
132
+}
commands/http/client.go
+1
-6
@@ -6,7 +6,6 @@ import (
6
"fmt"
7
"io"
8
"net/http"
9
- "os"
9
"strings"
10
11
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
@@ -59,11 +58,7 @@ func Send(req cmds.Request) (cmds.Response, error) {
58
if in != nil {
59
return nil, fmt.Errorf("Currently, only one file stream is possible per request")
60
}
62
- in, err = os.Open(arg.(string))
63
- if err != nil {
64
- return nil, err
65
- }
66
- args[i] = in
61
+ in = arg.(io.Reader)
62
}
63
}
64