5
5
"fmt"
6
6
"reflect"
7
7
8
- "github.com/ipfs/go-ipfs-files"
8
+ files "github.com/ipfs/go-ipfs-files"
9
9
)
10
10
11
11
// Request represents a call to a command from a consumer
@@ -24,27 +24,32 @@ type Request struct {
24
24
25
25
// NewRequest returns a request initialized with given arguments
26
26
// An non-nil error will be returned if the provided option values are invalid
27
- func NewRequest (ctx context.Context , path []string , opts OptMap , args []string , file files.Directory , root * Command ) (* Request , error ) {
28
- if opts == nil {
29
- opts = make (OptMap )
30
- }
27
+ func NewRequest (ctx context.Context ,
28
+ path []string ,
29
+ opts OptMap ,
30
+ args []string ,
31
+ file files.Directory ,
32
+ root * Command ,
33
+ ) (* Request , error ) {
31
34
32
35
cmd , err := root .Get (path )
33
36
if err != nil {
34
37
return nil , err
35
38
}
36
39
40
+ options , err := checkAndConvertOptions (root , opts , path )
41
+
37
42
req := & Request {
38
43
Path : path ,
39
- Options : opts ,
44
+ Options : options ,
40
45
Arguments : args ,
41
46
Files : file ,
42
47
Root : root ,
43
48
Command : cmd ,
44
49
Context : ctx ,
45
50
}
46
51
47
- return req , req . convertOptions ( root )
52
+ return req , err
48
53
}
49
54
50
55
// BodyArgs returns a scanner that returns arguments passed in the body as tokens.
@@ -94,13 +99,18 @@ func (req *Request) SetOption(name string, value interface{}) {
94
99
return
95
100
}
96
101
97
- func (req * Request ) convertOptions (root * Command ) error {
98
- optDefs , err := root .GetOptions (req .Path )
102
+ func checkAndConvertOptions (root * Command , opts OptMap , path []string ) (OptMap , error ) {
103
+ optDefs , err := root .GetOptions (path )
104
+ options := make (OptMap )
105
+
99
106
if err != nil {
100
- return err
107
+ return options , err
108
+ }
109
+ for k , v := range opts {
110
+ options [k ] = v
101
111
}
102
112
103
- for k , v := range req . Options {
113
+ for k , v := range opts {
104
114
opt , ok := optDefs [k ]
105
115
if ! ok {
106
116
continue
@@ -115,26 +125,26 @@ func (req *Request) convertOptions(root *Command) error {
115
125
if len (str ) == 0 {
116
126
value = "empty value"
117
127
}
118
- return fmt .Errorf ("Could not convert %s to type %q (for option %q)" ,
128
+ return options , fmt .Errorf ("Could not convert %s to type %q (for option %q)" ,
119
129
value , opt .Type ().String (), "-" + k )
120
130
}
121
- req . Options [k ] = val
131
+ options [k ] = val
122
132
123
133
} else {
124
- return fmt .Errorf ("Option %q should be type %q, but got type %q" ,
134
+ return options , fmt .Errorf ("Option %q should be type %q, but got type %q" ,
125
135
k , opt .Type ().String (), kind .String ())
126
136
}
127
137
}
128
138
129
139
for _ , name := range opt .Names () {
130
- if _ , ok := req . Options [name ]; name != k && ok {
131
- return fmt .Errorf ("Duplicate command options were provided (%q and %q)" ,
140
+ if _ , ok := options [name ]; name != k && ok {
141
+ return options , fmt .Errorf ("Duplicate command options were provided (%q and %q)" ,
132
142
k , name )
133
143
}
134
144
}
135
145
}
136
146
137
- return nil
147
+ return options , nil
138
148
}
139
149
140
150
// GetEncoding returns the EncodingType set in a request, falling back to JSON
0 commit comments