forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
191 lines (158 loc) · 3.9 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
package cmds
import (
"context"
"fmt"
"reflect"
"github.com/ipfs/go-ipfs-cmdkit"
"github.com/ipfs/go-ipfs-cmdkit/files"
)
// Request represents a call to a command from a consumer
type Request struct {
Context context.Context
Root, Command *Command
Path []string
Arguments []string
Options cmdkit.OptMap
Files files.File
bodyArgs *arguments
}
// NewRequest returns a request initialized with given arguments
// An non-nil error will be returned if the provided option values are invalid
func NewRequest(ctx context.Context, path []string, opts cmdkit.OptMap, args []string, file files.File, root *Command) (*Request, error) {
if opts == nil {
opts = make(cmdkit.OptMap)
}
cmd, err := root.Get(path)
if err != nil {
return nil, err
}
req := &Request{
Path: path,
Options: opts,
Arguments: args,
Files: file,
Root: root,
Command: cmd,
Context: ctx,
}
return req, req.convertOptions(root)
}
// BodyArgs returns a scanner that returns arguments passed in the body as tokens.
//
// Returns nil if there are no arguments to be consumed via stdin.
func (req *Request) BodyArgs() StdinArguments {
// dance to make sure we return an *untyped* nil.
// DO NOT just return `req.bodyArgs`.
// If you'd like to complain, go to
// https://github.com/golang/go/issues/.
if req.bodyArgs != nil {
return req.bodyArgs
}
return nil
}
func (req *Request) ParseBodyArgs() error {
s := req.BodyArgs()
if s == nil {
return nil
}
for s.Scan() {
req.Arguments = append(req.Arguments, s.Argument())
}
return s.Err()
}
func (req *Request) SetOption(name string, value interface{}) {
optDefs, err := req.Root.GetOptions(req.Path)
optDef, found := optDefs[name]
if req.Options == nil {
req.Options = map[string]interface{}{}
}
// unknown option, simply set the value and return
// TODO we might error out here instead
if err != nil || !found {
req.Options[name] = value
return
}
name = optDef.Name()
req.Options[name] = value
return
}
func (req *Request) convertOptions(root *Command) error {
optDefs, err := root.GetOptions(req.Path)
if err != nil {
return err
}
for k, v := range req.Options {
opt, ok := optDefs[k]
if !ok {
continue
}
kind := reflect.TypeOf(v).Kind()
if kind != opt.Type() {
if str, ok := v.(string); ok {
val, err := opt.Parse(str)
if err != nil {
value := fmt.Sprintf("value %q", v)
if len(str) == 0 {
value = "empty value"
}
return fmt.Errorf("Could not convert %q to type %q (for option %q)",
value, opt.Type().String(), "-"+k)
}
req.Options[k] = val
} else {
return fmt.Errorf("Option %q should be type %q, but got type %q",
k, opt.Type().String(), kind.String())
}
}
for _, name := range opt.Names() {
if _, ok := req.Options[name]; name != k && ok {
return fmt.Errorf("Duplicate command options were provided (%q and %q)",
k, name)
}
}
}
return nil
}
// GetEncoding returns the EncodingType set in a request, falling back to JSON
func GetEncoding(req *Request) EncodingType {
encIface := req.Options[EncLong]
if encIface == nil {
return JSON
}
switch enc := encIface.(type) {
case string:
return EncodingType(enc)
case EncodingType:
return enc
default:
return JSON
}
}
// fillDefault fills in default values if option has not been set
func (req *Request) FillDefaults() error {
optDefMap, err := req.Root.GetOptions(req.Path)
if err != nil {
return err
}
optDefs := map[cmdkit.Option]struct{}{}
for _, optDef := range optDefMap {
optDefs[optDef] = struct{}{}
}
Outer:
for optDef := range optDefs {
dflt := optDef.Default()
if dflt == nil {
// option has no dflt, continue
continue
}
names := optDef.Names()
for _, name := range names {
if _, ok := req.Options[name]; ok {
// option has been set, continue with next option
continue Outer
}
}
req.Options[optDef.Name()] = dflt
}
return nil
}