Skip to content

Commit 3a00a23

Browse files
committed
extract opts, args and helptext to cmdsutils subpackage
this way we can share the code with the legace commands package and don't have to convert everything
1 parent 2422ada commit 3a00a23

19 files changed

+151
-125
lines changed

chan.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cmds
33
import (
44
"fmt"
55
"io"
6+
7+
"github.com/ipfs/go-ipfs-cmds/cmdsutil"
68
)
79

810
func NewChanResponsePair(req *Request) (ResponseEmitter, Response) {
@@ -25,7 +27,7 @@ func NewChanResponsePair(req *Request) (ResponseEmitter, Response) {
2527
type chanResponse struct {
2628
req *Request
2729

28-
err *Error
30+
err *cmdsutil.Error
2931
length uint64
3032

3133
ch <-chan interface{}
@@ -35,7 +37,7 @@ func (r *chanResponse) Request() *Request {
3537
return r.req
3638
}
3739

38-
func (r *chanResponse) Error() *Error {
40+
func (r *chanResponse) Error() *cmdsutil.Error {
3941
return r.err
4042
}
4143

@@ -56,18 +58,18 @@ type chanResponseEmitter struct {
5658
ch chan<- interface{}
5759

5860
length *uint64
59-
err **Error
61+
err **cmdsutil.Error
6062

6163
emitted bool
6264
}
6365

64-
func (re *chanResponseEmitter) SetError(err interface{}, t ErrorType) {
66+
func (re *chanResponseEmitter) SetError(err interface{}, t cmdsutil.ErrorType) {
6567
// don't change value after emitting
6668
if re.emitted {
6769
return
6870
}
6971

70-
*re.err = &Error{Message: fmt.Sprint(err), Code: t}
72+
*re.err = &cmdsutil.Error{Message: fmt.Sprint(err), Code: t}
7173
}
7274

7375
func (re *chanResponseEmitter) SetLength(l uint64) {

channelmarshaler.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cmds
22

3+
/*
34
import "io"
45
56
type ChannelMarshaler struct {
@@ -37,3 +38,4 @@ func (cr *ChannelMarshaler) Read(p []byte) (int, error) {
3738
}
3839
return n, nil
3940
}
41+
*/

argument.go renamed to cmdsutil/argument.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmds
1+
package cmdsutil
22

33
type ArgumentType int
44

error.go renamed to cmdsutil/error.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmds
1+
package cmdsutil
22

33
// ErrorType signfies a category of errors
44
type ErrorType uint

cmdsutil/helptext.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cmdsutil
2+
3+
// HelpText is a set of strings used to generate command help text. The help
4+
// text follows formats similar to man pages, but not exactly the same.
5+
type HelpText struct {
6+
// required
7+
Tagline string // used in <cmd usage>
8+
ShortDescription string // used in DESCRIPTION
9+
SynopsisOptionsValues map[string]string // mappings for synopsis generator
10+
11+
// optional - whole section overrides
12+
Usage string // overrides USAGE section
13+
LongDescription string // overrides DESCRIPTION section
14+
Options string // overrides OPTIONS section
15+
Arguments string // overrides ARGUMENTS section
16+
Subcommands string // overrides SUBCOMMANDS section
17+
Synopsis string // overrides SYNOPSIS field
18+
}

option.go renamed to cmdsutil/option.go

+23-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmds
1+
package cmdsutil
22

33
import (
44
"fmt"
@@ -18,6 +18,8 @@ const (
1818
String = reflect.String
1919
)
2020

21+
type OptMap map[string]interface{}
22+
2123
// Option is used to specify a field that will be provided by a consumer
2224
type Option interface {
2325
Names() []string // a list of unique names matched with user-provided flags
@@ -109,75 +111,75 @@ func StringOption(names ...string) Option {
109111
}
110112

111113
type OptionValue struct {
112-
value interface{}
113-
found bool
114-
def Option
114+
Value interface{}
115+
ValueFound bool
116+
Def Option
115117
}
116118

117119
// Found returns true if the option value was provided by the user (not a default value)
118120
func (ov OptionValue) Found() bool {
119-
return ov.found
121+
return ov.ValueFound
120122
}
121123

122124
// Definition returns the option definition for the provided value
123125
func (ov OptionValue) Definition() Option {
124-
return ov.def
126+
return ov.Def
125127
}
126128

127129
// value accessor methods, gets the value as a certain type
128130
func (ov OptionValue) Bool() (value bool, found bool, err error) {
129-
if !ov.found && ov.value == nil {
131+
if !ov.ValueFound && ov.Value == nil {
130132
return false, false, nil
131133
}
132-
val, ok := ov.value.(bool)
134+
val, ok := ov.Value.(bool)
133135
if !ok {
134136
err = util.ErrCast()
135137
}
136-
return val, ov.found, err
138+
return val, ov.ValueFound, err
137139
}
138140

139141
func (ov OptionValue) Int() (value int, found bool, err error) {
140-
if !ov.found && ov.value == nil {
142+
if !ov.ValueFound && ov.Value == nil {
141143
return 0, false, nil
142144
}
143-
val, ok := ov.value.(int)
145+
val, ok := ov.Value.(int)
144146
if !ok {
145147
err = util.ErrCast()
146148
}
147-
return val, ov.found, err
149+
return val, ov.ValueFound, err
148150
}
149151

150152
func (ov OptionValue) Uint() (value uint, found bool, err error) {
151-
if !ov.found && ov.value == nil {
153+
if !ov.ValueFound && ov.Value == nil {
152154
return 0, false, nil
153155
}
154-
val, ok := ov.value.(uint)
156+
val, ok := ov.Value.(uint)
155157
if !ok {
156158
err = util.ErrCast()
157159
}
158-
return val, ov.found, err
160+
return val, ov.ValueFound, err
159161
}
160162

161163
func (ov OptionValue) Float() (value float64, found bool, err error) {
162-
if !ov.found && ov.value == nil {
164+
if !ov.ValueFound && ov.Value == nil {
163165
return 0, false, nil
164166
}
165-
val, ok := ov.value.(float64)
167+
val, ok := ov.Value.(float64)
166168
if !ok {
167169
err = util.ErrCast()
168170
}
169-
return val, ov.found, err
171+
return val, ov.ValueFound, err
170172
}
171173

172174
func (ov OptionValue) String() (value string, found bool, err error) {
173-
if !ov.found && ov.value == nil {
175+
if !ov.ValueFound && ov.Value == nil {
174176
return "", false, nil
175177
}
176-
val, ok := ov.value.(string)
178+
val, ok := ov.Value.(string)
177179
if !ok {
178180
err = util.ErrCast()
179181
}
180-
return val, ov.found, err
182+
return val, ov.ValueFound, err
181183
}
182184

183185
// Flag names
@@ -195,15 +197,3 @@ var OptionEncodingType = StringOption(EncLong, EncShort, "The encoding type the
195197
var OptionRecursivePath = BoolOption(RecLong, RecShort, "Add directory paths recursively").Default(false)
196198
var OptionStreamChannels = BoolOption(ChanOpt, "Stream channel output")
197199
var OptionTimeout = StringOption(TimeoutOpt, "set a global timeout on the command")
198-
199-
// global options, added to every command
200-
var globalOptions = []Option{
201-
OptionEncodingType,
202-
OptionStreamChannels,
203-
OptionTimeout,
204-
}
205-
206-
// the above array of Options, wrapped in a Command
207-
var globalCommand = &Command{
208-
Options: globalOptions,
209-
}

option_test.go renamed to cmdsutil/option_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmds
1+
package cmdsutil
22

33
import (
44
"strings"

command.go

+26-28
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"fmt"
1414
"reflect"
1515

16+
"github.com/ipfs/go-ipfs-cmds/cmdsutil"
17+
1618
oldcmds "github.com/ipfs/go-ipfs/commands"
1719
"github.com/ipfs/go-ipfs/path"
1820
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
@@ -24,28 +26,11 @@ var log = logging.Logger("command")
2426
// It reads from the Request, and writes results to the ResponseEmitter.
2527
type Function func(Request, ResponseEmitter)
2628

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-
4429
// Command is a runnable command, with input arguments and options (flags).
4530
// It can also have Subcommands, to group units of work into sets.
4631
type Command struct {
47-
Options []Option
48-
Arguments []Argument
32+
Options []cmdsutil.Option
33+
Arguments []cmdsutil.Argument
4934
PreRun func(req Request) error
5035

5136
// Run is the function that processes the request to generate a response.
@@ -55,7 +40,7 @@ type Command struct {
5540
Run interface{}
5641
PostRun interface{}
5742
Encoders map[EncodingType]Encoder
58-
Helptext HelpText
43+
Helptext cmdsutil.HelpText
5944

6045
// External denotes that a command is actually an external binary.
6146
// fewer checks and validations will be performed on such commands.
@@ -170,7 +155,7 @@ func (c *Command) Call(req Request, re ResponseEmitter) error {
170155
} else if ch, ok := output.(chan interface{}); ok {
171156
output = (<-chan interface{})(ch)
172157
} else {
173-
re.SetError(ErrIncorrectType, ErrNormal)
158+
re.SetError(ErrIncorrectType, cmdsutil.ErrNormal)
174159
return ErrIncorrectType
175160
}
176161

@@ -186,7 +171,7 @@ func (c *Command) Call(req Request, re ResponseEmitter) error {
186171
expectedType := reflect.TypeOf(cmd.Type)
187172

188173
if actualType != expectedType {
189-
re.SetError(ErrIncorrectType, ErrNormal)
174+
re.SetError(ErrIncorrectType, cmdsutil.ErrNormal)
190175
return ErrIncorrectType
191176
}
192177
}
@@ -195,6 +180,7 @@ func (c *Command) Call(req Request, re ResponseEmitter) error {
195180
}
196181

197182
return nil
183+
198184
}
199185

200186
// Resolve returns the subcommands at the given path
@@ -227,8 +213,8 @@ func (c *Command) Get(path []string) (*Command, error) {
227213
}
228214

229215
// 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))
232218

233219
cmds, err := c.Resolve(path)
234220
if err != nil {
@@ -240,7 +226,7 @@ func (c *Command) GetOptions(path []string) (map[string]Option, error) {
240226
options = append(options, cmd.Options...)
241227
}
242228

243-
optionsMap := make(map[string]Option)
229+
optionsMap := make(map[string]cmdsutil.Option)
244230
for _, opt := range options {
245231
for _, name := range opt.Names() {
246232
if _, found := optionsMap[name]; found {
@@ -271,7 +257,7 @@ func (c *Command) CheckArguments(req Request) error {
271257
// skip optional argument definitions if there aren't
272258
// sufficient remaining values
273259
if len(args)-valueIndex <= numRequired && !argDef.Required ||
274-
argDef.Type == ArgFile {
260+
argDef.Type == cmdsutil.ArgFile {
275261
continue
276262
}
277263

@@ -334,7 +320,7 @@ func (c *Command) ProcessHelp() {
334320

335321
// checkArgValue returns an error if a given arg value is not valid for the
336322
// given Argument
337-
func checkArgValue(v string, found bool, def Argument) error {
323+
func checkArgValue(v string, found bool, def cmdsutil.Argument) error {
338324
if def.Variadic && def.SupportsStdin {
339325
return nil
340326
}
@@ -347,5 +333,17 @@ func checkArgValue(v string, found bool, def Argument) error {
347333
}
348334

349335
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,
351349
}

0 commit comments

Comments
 (0)