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,7 +24,8 @@ 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 ) {
27
+ func NewRequest (ctx context.Context , path []string , opts OptMap , args []string ,
28
+ file files.Directory , root * Command ) (* Request , error ) {
28
29
if opts == nil {
29
30
opts = make (OptMap )
30
31
}
@@ -34,6 +35,7 @@ func NewRequest(ctx context.Context, path []string, opts OptMap, args []string,
34
35
return nil , err
35
36
}
36
37
38
+ err = convertOptions (root , opts , path )
37
39
req := & Request {
38
40
Path : path ,
39
41
Options : opts ,
@@ -44,7 +46,7 @@ func NewRequest(ctx context.Context, path []string, opts OptMap, args []string,
44
46
Context : ctx ,
45
47
}
46
48
47
- return req , req . convertOptions ( root )
49
+ return req , err
48
50
}
49
51
50
52
// BodyArgs returns a scanner that returns arguments passed in the body as tokens.
@@ -94,13 +96,13 @@ func (req *Request) SetOption(name string, value interface{}) {
94
96
return
95
97
}
96
98
97
- func ( req * Request ) convertOptions (root * Command ) error {
98
- optDefs , err := root .GetOptions (req . Path )
99
+ func convertOptions (root * Command , opts OptMap , path [] string ) error {
100
+ optDefs , err := root .GetOptions (path )
99
101
if err != nil {
100
102
return err
101
103
}
102
104
103
- for k , v := range req . Options {
105
+ for k , v := range opts {
104
106
opt , ok := optDefs [k ]
105
107
if ! ok {
106
108
continue
@@ -118,7 +120,7 @@ func (req *Request) convertOptions(root *Command) error {
118
120
return fmt .Errorf ("Could not convert %s to type %q (for option %q)" ,
119
121
value , opt .Type ().String (), "-" + k )
120
122
}
121
- req . Options [k ] = val
123
+ opts [k ] = val
122
124
123
125
} else {
124
126
return fmt .Errorf ("Option %q should be type %q, but got type %q" ,
@@ -127,7 +129,7 @@ func (req *Request) convertOptions(root *Command) error {
127
129
}
128
130
129
131
for _ , name := range opt .Names () {
130
- if _ , ok := req . Options [name ]; name != k && ok {
132
+ if _ , ok := opts [name ]; name != k && ok {
131
133
return fmt .Errorf ("Duplicate command options were provided (%q and %q)" ,
132
134
k , name )
133
135
}
0 commit comments