forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
196 lines (162 loc) · 4.39 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
package legacy
import (
"context"
"fmt"
"io"
"os"
"reflect"
"gx/ipfs/QmNueRyPRQiV7PUEpnP4GgGLuK1rKQLaRW7sfPvUetYig1/go-ipfs-cmds"
"gx/ipfs/QmdE4gMduCKCGAcczM2F5ioYDfdeKuPix138wrES1YSr7f/go-ipfs-cmdkit"
"gx/ipfs/QmdE4gMduCKCGAcczM2F5ioYDfdeKuPix138wrES1YSr7f/go-ipfs-cmdkit/files"
oldcmds "github.com/ipfs/go-ipfs/commands"
)
// requestWrapper implements a oldcmds.Request from an Request
type requestWrapper struct {
req *cmds.Request
ctx *oldcmds.Context
}
func (r *requestWrapper) String() string {
return fmt.Sprintf("{%v, %v}", r.req, r.ctx)
}
func (r *requestWrapper) GoString() string {
return fmt.Sprintf("lgc.Request{%#v, %#v}", r.req, r.ctx)
}
// InvocContext retuns the invocation context of the oldcmds.Request.
// It is faked using OldContext().
func (r *requestWrapper) InvocContext() *oldcmds.Context {
return r.ctx
}
// SetInvocContext sets the invocation context. First the context is converted
// to a Context using NewContext().
func (r *requestWrapper) SetInvocContext(ctx oldcmds.Context) {
r.ctx = &ctx
}
// Command is an empty stub.
func (r *requestWrapper) Command() *oldcmds.Command { return nil }
func (r *requestWrapper) Arguments() []string {
cmdArgs := r.req.Command.Arguments
reqArgs := r.req.Arguments
// TODO figure out the exaclt policy for when to use these automatically
// TODO once that's done, change the log.Debug below to log.Error
// read arguments from body if we don't have all of them or the command has variadic arguemnts
if len(reqArgs) < len(cmdArgs) ||
len(cmdArgs) > 0 && cmdArgs[len(cmdArgs)-1].Variadic {
err := r.req.ParseBodyArgs()
if err != nil {
log.Debug("error reading arguments from stdin: ", err)
}
}
return r.req.Arguments
}
func (r *requestWrapper) Context() context.Context {
return r.req.Context
}
func (r *requestWrapper) ConvertOptions() error {
return convertOptions(r.req)
}
func (r *requestWrapper) Files() files.File {
return r.req.Files
}
func (r *requestWrapper) Option(name string) *cmdkit.OptionValue {
var option cmdkit.Option
optDefs, err := r.req.Root.GetOptions(r.req.Path)
if err != nil {
return &cmdkit.OptionValue{}
}
for _, def := range optDefs {
for _, optName := range def.Names() {
if name == optName {
option = def
break
}
}
}
if option == nil {
return nil
}
// try all the possible names, break if we find a value
for _, n := range option.Names() {
val, found := r.req.Options[n]
if found {
return &cmdkit.OptionValue{
Value: val,
ValueFound: found,
Def: option,
}
}
}
return &cmdkit.OptionValue{
Value: option.Default(),
ValueFound: false,
Def: option,
}
}
func (r *requestWrapper) Options() cmdkit.OptMap {
return r.req.Options
}
func (r *requestWrapper) Path() []string {
return r.req.Path
}
func (r *requestWrapper) SetArguments(args []string) {
r.req.Arguments = args
}
func (r *requestWrapper) SetFiles(f files.File) {
r.req.Files = f
}
func (r *requestWrapper) SetOption(name string, v interface{}) {
r.req.SetOption(name, v)
}
func (r *requestWrapper) SetOptions(om cmdkit.OptMap) error {
r.req.Options = om
return convertOptions(r.req)
}
func (r *requestWrapper) Stdin() io.Reader {
return os.Stdin
}
func (r *requestWrapper) StringArguments() []string {
return r.req.Arguments
}
func (r *requestWrapper) Values() map[string]interface{} {
return nil
}
// copied from go-ipfs-cmds/request.go
func convertOptions(req *cmds.Request) error {
optDefSlice := req.Command.Options
optDefs := make(map[string]cmdkit.Option)
for _, def := range optDefSlice {
for _, name := range def.Names() {
optDefs[name] = def
}
}
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
}