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