This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
request.go
281 lines (241 loc) · 5.89 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package commands
import (
"fmt"
"io"
"os"
"reflect"
"strconv"
"time"
"golang.org/x/net/context"
"github.com/ipfs/go-commands/files"
"github.com/ipfs/go-commands/util"
)
type OptMap map[string]interface{}
// Request represents a call to a command from a consumer
type Request interface {
Path() []string
Option(name string) *OptionValue
Options() OptMap
SetOption(name string, val interface{})
SetOptions(opts OptMap) error
Arguments() []string
SetArguments([]string)
Files() files.File
SetFiles(files.File)
Context() context.Context
SetRootContext(context.Context) error
Command() *Command
Values() map[string]interface{}
Stdin() io.Reader
ConvertOptions() error
}
type request struct {
path []string
options OptMap
arguments []string
files files.File
cmd *Command
rctx context.Context
optionDefs map[string]Option
values map[string]interface{}
stdin io.Reader
}
// Path returns the command path of this request
func (r *request) Path() []string {
return r.path
}
// Option returns the value of the option for given name.
func (r *request) Option(name string) *OptionValue {
// find the option with the specified name
option, found := r.optionDefs[name]
if !found {
return nil
}
// try all the possible names, break if we find a value
for _, n := range option.Names() {
val, found := r.options[n]
if found {
return &OptionValue{val, found, option}
}
}
// MAYBE_TODO: use default value instead of nil
return &OptionValue{nil, false, option}
}
// Options returns a copy of the option map
func (r *request) Options() OptMap {
output := make(OptMap)
for k, v := range r.options {
output[k] = v
}
return output
}
func (r *request) SetRootContext(ctx context.Context) error {
ctx, err := getContext(ctx, r)
if err != nil {
return err
}
r.rctx = ctx
return nil
}
// SetOption sets the value of the option for given name.
func (r *request) SetOption(name string, val interface{}) {
// find the option with the specified name
option, found := r.optionDefs[name]
if !found {
return
}
// try all the possible names, if we already have a value then set over it
for _, n := range option.Names() {
_, found := r.options[n]
if found {
r.options[n] = val
return
}
}
r.options[name] = val
}
// SetOptions sets the option values, unsetting any values that were previously set
func (r *request) SetOptions(opts OptMap) error {
r.options = opts
return r.ConvertOptions()
}
// Arguments returns the arguments slice
func (r *request) Arguments() []string {
return r.arguments
}
func (r *request) SetArguments(args []string) {
r.arguments = args
}
func (r *request) Files() files.File {
return r.files
}
func (r *request) SetFiles(f files.File) {
r.files = f
}
func (r *request) Context() context.Context {
return r.rctx
}
func getContext(base context.Context, req Request) (context.Context, error) {
tout, found, err := req.Option("timeout").String()
if err != nil {
return nil, fmt.Errorf("error parsing timeout option: %s", err)
}
var ctx context.Context
if found {
duration, err := time.ParseDuration(tout)
if err != nil {
return nil, fmt.Errorf("error parsing timeout option: %s", err)
}
tctx, _ := context.WithTimeout(base, duration)
ctx = tctx
} else {
cctx, _ := context.WithCancel(base)
ctx = cctx
}
return ctx, nil
}
func (r *request) Command() *Command {
return r.cmd
}
type converter func(string) (interface{}, error)
var converters = map[reflect.Kind]converter{
Bool: func(v string) (interface{}, error) {
if v == "" {
return true, nil
}
return strconv.ParseBool(v)
},
Int: func(v string) (interface{}, error) {
val, err := strconv.ParseInt(v, 0, 32)
if err != nil {
return nil, err
}
return int(val), err
},
Uint: func(v string) (interface{}, error) {
val, err := strconv.ParseUint(v, 0, 32)
if err != nil {
return nil, err
}
return int(val), err
},
Float: func(v string) (interface{}, error) {
return strconv.ParseFloat(v, 64)
},
}
func (r *request) Values() map[string]interface{} {
return r.values
}
func (r *request) Stdin() io.Reader {
return r.stdin
}
func (r *request) ConvertOptions() error {
for k, v := range r.options {
opt, ok := r.optionDefs[k]
if !ok {
continue
}
kind := reflect.TypeOf(v).Kind()
if kind != opt.Type() {
if kind == String {
convert := converters[opt.Type()]
str, ok := v.(string)
if !ok {
return util.ErrCast()
}
val, err := convert(str)
if err != nil {
value := fmt.Sprintf("value '%v'", v)
if len(str) == 0 {
value = "empty value"
}
return fmt.Errorf("Could not convert %s to type '%s' (for option '-%s')",
value, opt.Type().String(), k)
}
r.options[k] = val
} else {
return fmt.Errorf("Option '%s' should be type '%s', but got type '%s'",
k, opt.Type().String(), kind.String())
}
} else {
r.options[k] = v
}
for _, name := range opt.Names() {
if _, ok := r.options[name]; name != k && ok {
return fmt.Errorf("Duplicate command options were provided ('%s' and '%s')",
k, name)
}
}
}
return nil
}
// NewEmptyRequest initializes an empty request
func NewEmptyRequest() (Request, error) {
return NewRequest(nil, nil, nil, nil, nil, nil)
}
// NewRequest returns a request initialized with given arguments
// An non-nil error will be returned if the provided option values are invalid
func NewRequest(path []string, opts OptMap, args []string, file files.File, cmd *Command, optDefs map[string]Option) (Request, error) {
if opts == nil {
opts = make(OptMap)
}
if optDefs == nil {
optDefs = make(map[string]Option)
}
values := make(map[string]interface{})
req := &request{
path: path,
options: opts,
arguments: args,
files: file,
cmd: cmd,
optionDefs: optDefs,
values: values,
stdin: os.Stdin,
}
err := req.ConvertOptions()
if err != nil {
return nil, err
}
return req, nil
}