@@ -13,6 +13,8 @@ import (
13
13
"fmt"
14
14
"reflect"
15
15
16
+ "github.com/ipfs/go-ipfs-cmds/cmdsutil"
17
+
16
18
oldcmds "github.com/ipfs/go-ipfs/commands"
17
19
"github.com/ipfs/go-ipfs/path"
18
20
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
@@ -24,28 +26,11 @@ var log = logging.Logger("command")
24
26
// It reads from the Request, and writes results to the ResponseEmitter.
25
27
type Function func (Request , ResponseEmitter )
26
28
27
- // HelpText is a set of strings used to generate command help text. The help
28
- // text follows formats similar to man pages, but not exactly the same.
29
- type HelpText struct {
30
- // required
31
- Tagline string // used in <cmd usage>
32
- ShortDescription string // used in DESCRIPTION
33
- SynopsisOptionsValues map [string ]string // mappings for synopsis generator
34
-
35
- // optional - whole section overrides
36
- Usage string // overrides USAGE section
37
- LongDescription string // overrides DESCRIPTION section
38
- Options string // overrides OPTIONS section
39
- Arguments string // overrides ARGUMENTS section
40
- Subcommands string // overrides SUBCOMMANDS section
41
- Synopsis string // overrides SYNOPSIS field
42
- }
43
-
44
29
// Command is a runnable command, with input arguments and options (flags).
45
30
// It can also have Subcommands, to group units of work into sets.
46
31
type Command struct {
47
- Options []Option
48
- Arguments []Argument
32
+ Options []cmdsutil. Option
33
+ Arguments []cmdsutil. Argument
49
34
PreRun func (req Request ) error
50
35
51
36
// Run is the function that processes the request to generate a response.
@@ -55,7 +40,7 @@ type Command struct {
55
40
Run interface {}
56
41
PostRun interface {}
57
42
Encoders map [EncodingType ]Encoder
58
- Helptext HelpText
43
+ Helptext cmdsutil. HelpText
59
44
60
45
// External denotes that a command is actually an external binary.
61
46
// fewer checks and validations will be performed on such commands.
@@ -170,7 +155,7 @@ func (c *Command) Call(req Request, re ResponseEmitter) error {
170
155
} else if ch , ok := output .(chan interface {}); ok {
171
156
output = (<- chan interface {})(ch )
172
157
} else {
173
- re .SetError (ErrIncorrectType , ErrNormal )
158
+ re .SetError (ErrIncorrectType , cmdsutil . ErrNormal )
174
159
return ErrIncorrectType
175
160
}
176
161
@@ -186,7 +171,7 @@ func (c *Command) Call(req Request, re ResponseEmitter) error {
186
171
expectedType := reflect .TypeOf (cmd .Type )
187
172
188
173
if actualType != expectedType {
189
- re .SetError (ErrIncorrectType , ErrNormal )
174
+ re .SetError (ErrIncorrectType , cmdsutil . ErrNormal )
190
175
return ErrIncorrectType
191
176
}
192
177
}
@@ -195,6 +180,7 @@ func (c *Command) Call(req Request, re ResponseEmitter) error {
195
180
}
196
181
197
182
return nil
183
+
198
184
}
199
185
200
186
// Resolve returns the subcommands at the given path
@@ -227,8 +213,8 @@ func (c *Command) Get(path []string) (*Command, error) {
227
213
}
228
214
229
215
// GetOptions returns the options in the given path of commands
230
- func (c * Command ) GetOptions (path []string ) (map [string ]Option , error ) {
231
- options := make ([]Option , 0 , len (c .Options ))
216
+ func (c * Command ) GetOptions (path []string ) (map [string ]cmdsutil. Option , error ) {
217
+ options := make ([]cmdsutil. Option , 0 , len (c .Options ))
232
218
233
219
cmds , err := c .Resolve (path )
234
220
if err != nil {
@@ -240,7 +226,7 @@ func (c *Command) GetOptions(path []string) (map[string]Option, error) {
240
226
options = append (options , cmd .Options ... )
241
227
}
242
228
243
- optionsMap := make (map [string ]Option )
229
+ optionsMap := make (map [string ]cmdsutil. Option )
244
230
for _ , opt := range options {
245
231
for _ , name := range opt .Names () {
246
232
if _ , found := optionsMap [name ]; found {
@@ -271,7 +257,7 @@ func (c *Command) CheckArguments(req Request) error {
271
257
// skip optional argument definitions if there aren't
272
258
// sufficient remaining values
273
259
if len (args )- valueIndex <= numRequired && ! argDef .Required ||
274
- argDef .Type == ArgFile {
260
+ argDef .Type == cmdsutil . ArgFile {
275
261
continue
276
262
}
277
263
@@ -334,7 +320,7 @@ func (c *Command) ProcessHelp() {
334
320
335
321
// checkArgValue returns an error if a given arg value is not valid for the
336
322
// given Argument
337
- func checkArgValue (v string , found bool , def Argument ) error {
323
+ func checkArgValue (v string , found bool , def cmdsutil. Argument ) error {
338
324
if def .Variadic && def .SupportsStdin {
339
325
return nil
340
326
}
@@ -347,5 +333,17 @@ func checkArgValue(v string, found bool, def Argument) error {
347
333
}
348
334
349
335
func ClientError (msg string ) error {
350
- return & Error {Code : ErrClient , Message : msg }
336
+ return & cmdsutil.Error {Code : cmdsutil .ErrClient , Message : msg }
337
+ }
338
+
339
+ // global options, added to every command
340
+ var globalOptions = []cmdsutil.Option {
341
+ cmdsutil .OptionEncodingType ,
342
+ cmdsutil .OptionStreamChannels ,
343
+ cmdsutil .OptionTimeout ,
344
+ }
345
+
346
+ // the above array of Options, wrapped in a Command
347
+ var globalCommand = & Command {
348
+ Options : globalOptions ,
351
349
}
0 commit comments