diff --git a/.travis.yml b/.travis.yml index 4cfe98c..923835b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,6 @@ env: global: - GOTFLAGS="-race" matrix: - - BUILD_DEPTYPE=gx - BUILD_DEPTYPE=gomod diff --git a/Makefile b/Makefile deleted file mode 100644 index 5415256..0000000 --- a/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -export IPFS_API ?= v04x.ipfs.io - -gx: - go get -u github.com/whyrusleeping/gx - go get -u github.com/whyrusleeping/gx-go - -deps: gx - gx --verbose install --global - gx-go rewrite diff --git a/README.md b/README.md index 0b1edc3..cc7517a 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,8 @@ # go-ipfs-cmdkit -[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) -[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/) -[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) -[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme) +**[DEPRECATED]** -> tools used by go-ipfs-cmds and go-ipfs/commands - -cmdkit offers some types, functions and values that are shared between go-ipfs/commands and its rewrite go-ipfs-cmds. - -## Documentation - -https://godoc.org/github.com/ipfs/go-ipfs-cmdkit - -## Contribute - -Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/NAME/issues)! - -This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md). - -### Want to hack on IPFS? - -[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/contributing.md) +These types have been merged into [go-ipfs-cmds](https://github.com/ipfs/go-ipfs-cmds) and are now simply re-exports. ## License diff --git a/argument.go b/argument.go deleted file mode 100644 index 593a162..0000000 --- a/argument.go +++ /dev/null @@ -1,56 +0,0 @@ -package cmdkit - -type ArgumentType int - -const ( - ArgString ArgumentType = iota - ArgFile -) - -type Argument struct { - Name string - Type ArgumentType - Required bool // error if no value is specified - Variadic bool // unlimited values can be specfied - SupportsStdin bool // can accept stdin as a value - Recursive bool // supports recursive file adding (with '-r' flag) - Description string -} - -func StringArg(name string, required, variadic bool, description string) Argument { - return Argument{ - Name: name, - Type: ArgString, - Required: required, - Variadic: variadic, - Description: description, - } -} - -func FileArg(name string, required, variadic bool, description string) Argument { - return Argument{ - Name: name, - Type: ArgFile, - Required: required, - Variadic: variadic, - Description: description, - } -} - -// TODO: modifiers might need a different API? -// e.g. passing enum values into arg constructors variadically -// (`FileArg("file", ArgRequired, ArgStdin, ArgRecursive)`) - -func (a Argument) EnableStdin() Argument { - a.SupportsStdin = true - return a -} - -func (a Argument) EnableRecursive() Argument { - if a.Type != ArgFile { - panic("Only FileArgs can enable recursive") - } - - a.Recursive = true - return a -} diff --git a/ci/Jenkinsfile b/ci/Jenkinsfile deleted file mode 100644 index b2067e6..0000000 --- a/ci/Jenkinsfile +++ /dev/null @@ -1 +0,0 @@ -golang() diff --git a/error.go b/error.go deleted file mode 100644 index 162fadc..0000000 --- a/error.go +++ /dev/null @@ -1,72 +0,0 @@ -package cmdkit - -import ( - "encoding/json" - "errors" - "fmt" -) - -// ErrorType signfies a category of errors -type ErrorType uint - -// ErrorTypes convey what category of error ocurred -const ( - ErrNormal ErrorType = iota // general errors - ErrClient // error was caused by the client, (e.g. invalid CLI usage) - ErrImplementation // programmer error in the server - ErrNotFound // == HTTP 404 - ErrFatal // abort instantly - // TODO: add more types of errors for better error-specific handling -) - -// Error is a struct for marshalling errors -type Error struct { - Message string - Code ErrorType -} - -// Errorf returns an Error with the given code and format specification -func Errorf(code ErrorType, format string, args ...interface{}) Error { - return Error{ - Code: code, - Message: fmt.Sprintf(format, args...), - } -} - -func (e Error) Error() string { - return e.Message -} - -func (e Error) MarshalJSON() ([]byte, error) { - return json.Marshal(struct { - Message string - Code ErrorType - Type string - }{ - Message: e.Message, - Code: e.Code, - Type: "error", - }) -} - -func (e *Error) UnmarshalJSON(data []byte) error { - var w struct { - Message string - Code ErrorType - Type string - } - - err := json.Unmarshal(data, &w) - if err != nil { - return err - } - - if w.Type != "error" { - return errors.New("not of type error") - } - - e.Message = w.Message - e.Code = w.Code - - return nil -} diff --git a/facade.go b/facade.go new file mode 100644 index 0000000..83acb7e --- /dev/null +++ b/facade.go @@ -0,0 +1,51 @@ +package cmdkit + +import ( + "reflect" + + "github.com/ipfs/go-ipfs-cmds" +) + +type ( + Argument = cmds.Argument + ArgumentType = cmds.ArgumentType + Error = cmds.Error + ErrorType = cmds.ErrorType + HelpText = cmds.HelpText + OptMap = cmds.OptMap + Option = cmds.Option +) + +const ( + Invalid = cmds.Invalid + Bool = cmds.Bool + Int = cmds.Int + Uint = cmds.Uint + Int64 = cmds.Int64 + Uint64 = cmds.Uint64 + Float = cmds.Float + String = cmds.String + ArgString = cmds.ArgString + ArgFile = cmds.ArgFile +) + +func BoolOption(names ...string) Option { return cmds.BoolOption(names...) } +func FloatOption(names ...string) Option { return cmds.FloatOption(names...) } +func Int64Option(names ...string) Option { return cmds.Int64Option(names...) } +func IntOption(names ...string) Option { return cmds.IntOption(names...) } +func NewOption(kind reflect.Kind, names ...string) Option { return cmds.NewOption(kind, names...) } +func StringOption(names ...string) Option { return cmds.StringOption(names...) } +func Uint64Option(names ...string) Option { return cmds.Uint64Option(names...) } +func UintOption(names ...string) Option { return cmds.UintOption(names...) } + +func FileArg(name string, required, variadic bool, description string) Argument { + return cmds.FileArg(name, required, variadic, description) +} + +func StringArg(name string, required, variadic bool, description string) Argument { + return cmds.StringArg(name, required, variadic, description) +} + +func Errorf(code ErrorType, format string, args ...interface{}) Error { + return cmds.Errorf(code, format, args...) +} diff --git a/go.mod b/go.mod index 3852b93..04a8b6f 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,5 @@ module github.com/ipfs/go-ipfs-cmdkit + +go 1.12 + +require github.com/ipfs/go-ipfs-cmds v0.0.7 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f3df082 --- /dev/null +++ b/go.sum @@ -0,0 +1,34 @@ +github.com/Kubuxu/go-os-helper v0.0.1/go.mod h1:N8B+I7vPCT80IcP58r50u4+gEEcsZETFUpAzWW2ep1Y= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE= +github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= +github.com/ipfs/go-ipfs-cmds v0.0.7 h1:0N2NXxYAZn1kHpHrZMHZYRcVGJSxQogDD89oKc0GZMg= +github.com/ipfs/go-ipfs-cmds v0.0.7/go.mod h1:E5ou2OpwkAtR8LdneNdq4w1vPcrTWvh/6WPhjxGaX/Y= +github.com/ipfs/go-ipfs-files v0.0.2 h1:fEEjF4H+1t8SFOHqUGp0KqcwgIRlbD2bu8CAS2sIggE= +github.com/ipfs/go-ipfs-files v0.0.2/go.mod h1:INEFm0LL2LWXBhNJ2PMIIb2w45hpXgPjNoE7yA8Y1d4= +github.com/ipfs/go-log v0.0.1 h1:9XTUN/rW64BCG1YhPK9Hoy3q8nr4gOmHHBpgFdfw6Lc= +github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM= +github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg= +github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= +github.com/mattn/go-isatty v0.0.5 h1:tHXDdz1cpzGaovsTB+TVB8q90WEokoVmfMqoVcrLUgw= +github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg= +github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/texttheater/golang-levenshtein v0.0.0-20180516184445-d188e65d659e/go.mod h1:XDKHRm5ThF8YJjx001LtgelzsoaEcvnA7lVWz9EeX3g= +github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc h1:9lDbC6Rz4bwmou+oE6Dt4Cb2BGMur5eR/GYptkKUVHo= +github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= +golang.org/x/net v0.0.0-20190227160552-c95aed5357e7 h1:C2F/nMkR/9sfUTpvR3QrjBuTdvMUC/cFajkphs1YLQo= +golang.org/x/net v0.0.0-20190227160552-c95aed5357e7/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190302025703-b6889370fb10 h1:xQJI9OEiErEQ++DoXOHqEpzsGMrAv2Q2jyCpi7DmfpQ= +golang.org/x/sys v0.0.0-20190302025703-b6889370fb10/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/helptext.go b/helptext.go deleted file mode 100644 index 8f4db8a..0000000 --- a/helptext.go +++ /dev/null @@ -1,18 +0,0 @@ -package cmdkit - -// HelpText is a set of strings used to generate command help text. The help -// text follows formats similar to man pages, but not exactly the same. -type HelpText struct { - // required - Tagline string // used in - ShortDescription string // used in DESCRIPTION - SynopsisOptionsValues map[string]string // mappings for synopsis generator - - // optional - whole section overrides - Usage string // overrides USAGE section - LongDescription string // overrides DESCRIPTION section - Options string // overrides OPTIONS section - Arguments string // overrides ARGUMENTS section - Subcommands string // overrides SUBCOMMANDS section - Synopsis string // overrides SYNOPSIS field -} diff --git a/json_test.go b/json_test.go deleted file mode 100644 index e9863a7..0000000 --- a/json_test.go +++ /dev/null @@ -1,95 +0,0 @@ -package cmdkit - -import ( - "encoding/json" - "testing" -) - -func TestMarshal(t *testing.T) { - type testcase struct { - msg string - code ErrorType - } - - tcs := []testcase{ - {msg: "error msg", code: 0}, - {msg: "error msg", code: 1}, - {msg: "some other error msg", code: 1}, - } - - for _, tc := range tcs { - e := Error{ - Message: tc.msg, - Code: tc.code, - } - - buf, err := json.Marshal(e) - if err != nil { - t.Fatal(err) - } - - m := make(map[string]interface{}) - - err = json.Unmarshal(buf, &m) - if err != nil { - t.Fatal(err) - } - - if len(m) != 3 { - t.Errorf("expected three map elements, got %d", len(m)) - } - - if m["Message"].(string) != tc.msg { - t.Errorf(`expected m["Message"] to be %q, got %q`, tc.msg, m["Message"]) - } - - icode := ErrorType(m["Code"].(float64)) - if icode != tc.code { - t.Errorf(`expected m["Code"] to be %v, got %v`, tc.code, icode) - } - - if m["Type"].(string) != "error" { - t.Errorf(`expected m["Type"] to be %q, got %q`, "error", m["Type"]) - } - } -} - -func TestUnmarshal(t *testing.T) { - type testcase struct { - json string - msg string - code ErrorType - - err string - } - - tcs := []testcase{ - {json: `{"Message":"error msg","Code":0}`, msg: "error msg", err: "not of type error"}, - {json: `{"Message":"error msg","Code":0,"Type":"error"}`, msg: "error msg"}, - {json: `{"Message":"error msg","Code":1,"Type":"error"}`, msg: "error msg", code: 1}, - {json: `{"Message":"some other error msg","Code":1,"Type":"error"}`, msg: "some other error msg", code: 1}, - } - - for i, tc := range tcs { - t.Log("at test case", i) - var e Error - err := json.Unmarshal([]byte(tc.json), &e) - if err != nil && err.Error() != tc.err { - t.Errorf("expected parse error %q but got %q", tc.err, err) - } else if err == nil && tc.err != "" { - t.Errorf("expected parse error %q but got %q", tc.err, err) - } - - if err != nil { - continue - } - - if e.Message != tc.msg { - t.Errorf("expected e.Message to be %q, got %q", tc.msg, e.Message) - } - - if e.Code != tc.code { - t.Errorf("expected e.Code to be %q, got %q", tc.code, e.Code) - } - } -} diff --git a/option.go b/option.go deleted file mode 100644 index 0e2165d..0000000 --- a/option.go +++ /dev/null @@ -1,276 +0,0 @@ -package cmdkit - -import ( - "fmt" - "reflect" - "strconv" - "strings" -) - -// Types of Command options -const ( - Invalid = reflect.Invalid - Bool = reflect.Bool - Int = reflect.Int - Uint = reflect.Uint - Int64 = reflect.Int64 - Uint64 = reflect.Uint64 - Float = reflect.Float64 - String = reflect.String -) - -type OptMap map[string]interface{} - -// Option is used to specify a field that will be provided by a consumer -type Option interface { - Name() string // the main name of the option - Names() []string // a list of unique names matched with user-provided flags - - Type() reflect.Kind // value must be this type - Description() string // a short string that describes this option - - WithDefault(interface{}) Option // sets the default value of the option - Default() interface{} - - Parse(str string) (interface{}, error) -} - -type option struct { - names []string - kind reflect.Kind - description string - defaultVal interface{} -} - -func (o *option) Name() string { - return o.names[0] -} - -func (o *option) Names() []string { - return o.names -} - -func (o *option) Type() reflect.Kind { - return o.kind -} - -func (o *option) Description() string { - if len(o.description) == 0 { - return "" - } - if !strings.HasSuffix(o.description, ".") { - o.description += "." - } - if o.defaultVal != nil { - if strings.Contains(o.description, "<>") { - return strings.Replace(o.description, "<>", - fmt.Sprintf("Default: %v.", o.defaultVal), -1) - } else { - return fmt.Sprintf("%s Default: %v.", o.description, o.defaultVal) - } - } - return o.description -} - -type converter func(string) (interface{}, error) - -var converters = map[reflect.Kind]converter{ - Bool: func(v string) (interface{}, error) { - if v == "" { - return true, nil - } - v = strings.ToLower(v) - - 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 uint(val), err - }, - Int64: func(v string) (interface{}, error) { - val, err := strconv.ParseInt(v, 0, 64) - if err != nil { - return nil, err - } - return val, err - }, - Uint64: func(v string) (interface{}, error) { - val, err := strconv.ParseUint(v, 0, 64) - if err != nil { - return nil, err - } - return val, err - }, - Float: func(v string) (interface{}, error) { - return strconv.ParseFloat(v, 64) - }, - String: func(v string) (interface{}, error) { - return v, nil - }, -} - -func (o *option) Parse(v string) (interface{}, error) { - conv, ok := converters[o.Type()] - if !ok { - return nil, fmt.Errorf("option %q takes %s arguments, but was passed %q", o.Name(), o.Type(), v) - } - - return conv(v) -} - -// constructor helper functions -func NewOption(kind reflect.Kind, names ...string) Option { - var desc string - - if len(names) >= 2 { - desc = names[len(names)-1] - names = names[:len(names)-1] - } - - return &option{ - names: names, - kind: kind, - description: desc, - } -} - -func (o *option) WithDefault(v interface{}) Option { - o.defaultVal = v - return o -} - -func (o *option) Default() interface{} { - return o.defaultVal -} - -// TODO handle description separately. this will take care of the panic case in -// NewOption - -// For all func {Type}Option(...string) functions, the last variadic argument -// is treated as the description field. - -func BoolOption(names ...string) Option { - return NewOption(Bool, names...) -} -func IntOption(names ...string) Option { - return NewOption(Int, names...) -} -func UintOption(names ...string) Option { - return NewOption(Uint, names...) -} -func Int64Option(names ...string) Option { - return NewOption(Int64, names...) -} -func Uint64Option(names ...string) Option { - return NewOption(Uint64, names...) -} -func FloatOption(names ...string) Option { - return NewOption(Float, names...) -} -func StringOption(names ...string) Option { - return NewOption(String, names...) -} - -type OptionValue struct { - Value interface{} - ValueFound bool - Def Option -} - -// Found returns true if the option value was provided by the user (not a default value) -func (ov *OptionValue) Found() bool { - return ov.ValueFound -} - -// Definition returns the option definition for the provided value -func (ov *OptionValue) Definition() Option { - return ov.Def -} - -// value accessor methods, gets the value as a certain type -func (ov *OptionValue) Bool() (value bool, found bool, err error) { - if ov == nil || !ov.ValueFound && ov.Value == nil { - return false, false, nil - } - val, ok := ov.Value.(bool) - if !ok { - err = fmt.Errorf("expected type %T, got %T", val, ov.Value) - } - return val, ov.ValueFound, err -} - -func (ov *OptionValue) Int() (value int, found bool, err error) { - if ov == nil || !ov.ValueFound && ov.Value == nil { - return 0, false, nil - } - val, ok := ov.Value.(int) - if !ok { - err = fmt.Errorf("expected type %T, got %T", val, ov.Value) - } - return val, ov.ValueFound, err -} - -func (ov *OptionValue) Uint() (value uint, found bool, err error) { - if ov == nil || !ov.ValueFound && ov.Value == nil { - return 0, false, nil - } - val, ok := ov.Value.(uint) - if !ok { - err = fmt.Errorf("expected type %T, got %T", val, ov.Value) - } - return val, ov.ValueFound, err -} - -func (ov *OptionValue) Int64() (value int64, found bool, err error) { - if ov == nil || !ov.ValueFound && ov.Value == nil { - return 0, false, nil - } - val, ok := ov.Value.(int64) - if !ok { - err = fmt.Errorf("expected type %T, got %T", val, ov.Value) - } - return val, ov.ValueFound, err -} - -func (ov *OptionValue) Uint64() (value uint64, found bool, err error) { - if ov == nil || !ov.ValueFound && ov.Value == nil { - return 0, false, nil - } - val, ok := ov.Value.(uint64) - if !ok { - err = fmt.Errorf("expected type %T, got %T", val, ov.Value) - } - return val, ov.ValueFound, err -} - -func (ov *OptionValue) Float() (value float64, found bool, err error) { - if ov == nil || !ov.ValueFound && ov.Value == nil { - return 0, false, nil - } - val, ok := ov.Value.(float64) - if !ok { - err = fmt.Errorf("expected type %T, got %T", val, ov.Value) - } - return val, ov.ValueFound, err -} - -func (ov *OptionValue) String() (value string, found bool, err error) { - if ov == nil || !ov.ValueFound && ov.Value == nil { - return "", false, nil - } - val, ok := ov.Value.(string) - if !ok { - err = fmt.Errorf("expected type %T, got %T", val, ov.Value) - } - return val, ov.ValueFound, err -} diff --git a/option_test.go b/option_test.go deleted file mode 100644 index ed7729c..0000000 --- a/option_test.go +++ /dev/null @@ -1,124 +0,0 @@ -package cmdkit - -import ( - "math" - "reflect" - "strings" - "testing" -) - -func TestOptionValueExtractBoolNotFound(t *testing.T) { - t.Log("ensure that no error is returned when value is not found") - optval := &OptionValue{ValueFound: false} - _, _, err := optval.Bool() - if err != nil { - t.Fatal("Found was false. Err should have been nil") - } -} - -func TestOptionValueExtractWrongType(t *testing.T) { - - t.Log("ensure that error is returned when value if of wrong type") - - optval := &OptionValue{Value: "wrong type: a string", ValueFound: true} - _, _, err := optval.Bool() - if err == nil { - t.Fatal("No error returned. Failure.") - } - - optval = &OptionValue{Value: "wrong type: a string", ValueFound: true} - _, _, err = optval.Int() - if err == nil { - t.Fatal("No error returned. Failure.") - } -} - -func TestLackOfDescriptionOfOptionDoesNotPanic(t *testing.T) { - opt := BoolOption("a", "") - opt.Description() -} - -func TestDotIsAddedInDescripton(t *testing.T) { - opt := BoolOption("a", "desc without dot") - dest := opt.Description() - if !strings.HasSuffix(dest, ".") { - t.Fatal("dot should have been added at the end of description") - } -} - -func TestOptionName(t *testing.T) { - exp := map[string]interface{}{ - "Name()": "main", - "Names()": []string{"main", "m", "alias"}, - } - - assert := func(name string, value interface{}) { - if !reflect.DeepEqual(value, exp[name]) { - t.Errorf(`expected %s to return %q, got %q`, name, exp[name], value) - } - } - - opt := StringOption("main", "m", "alias", `an option with main name "main" and "m" and "alias" as aliases`) - assert("Name()", opt.Name()) - assert("Names()", opt.Names()) -} - -func TestParse(t *testing.T) { - type testcase struct { - opt Option - str string - v interface{} - err string - } - - tcs := []testcase{ - {opt: StringOption("str"), str: "i'm a string!", v: "i'm a string!"}, - {opt: IntOption("int1"), str: "42", v: 42}, - {opt: IntOption("int1"), str: "fourtytwo", err: `strconv.ParseInt: parsing "fourtytwo": invalid syntax`}, - {opt: IntOption("int2"), str: "-42", v: -42}, - {opt: UintOption("uint1"), str: "23", v: uint(23)}, - {opt: UintOption("uint2"), str: "-23", err: `strconv.ParseUint: parsing "-23": invalid syntax`}, - {opt: Int64Option("int3"), str: "100001", v: int64(100001)}, - {opt: Int64Option("int3"), str: "2147483648", v: int64(math.MaxInt32 + 1)}, - {opt: Int64Option("int3"), str: "fly", err: `strconv.ParseInt: parsing "fly": invalid syntax`}, - {opt: Uint64Option("uint3"), str: "23", v: uint64(23)}, - {opt: Uint64Option("uint3"), str: "-23", err: `strconv.ParseUint: parsing "-23": invalid syntax`}, - {opt: BoolOption("true"), str: "true", v: true}, - {opt: BoolOption("true"), str: "", v: true}, - {opt: BoolOption("false"), str: "false", v: false}, - {opt: FloatOption("float"), str: "2.718281828459045", v: 2.718281828459045}, - } - - for _, tc := range tcs { - v, err := tc.opt.Parse(tc.str) - if err != nil && err.Error() != tc.err { - t.Errorf("unexpected error: %s", err) - } else if err == nil && tc.err != "" { - t.Errorf("expected error %q but got nil", tc.err) - } - - if v != tc.v { - t.Errorf("expected %v but got %v", tc.v, v) - } - } -} - -func TestDescription(t *testing.T) { - type testcase struct { - opt Option - desc string - } - - tcs := []testcase{ - {opt: StringOption("str", "some random option"), desc: "some random option."}, - {opt: StringOption("str", "some random option (<>)"), desc: "some random option (<>)."}, - {opt: StringOption("str", "some random option (<>)").WithDefault("random=4"), desc: "some random option (Default: random=4.)."}, - {opt: StringOption("str", "some random option").WithDefault("random=4"), desc: "some random option. Default: random=4."}, - } - - for _, tc := range tcs { - if desc := tc.opt.Description(); desc != tc.desc { - t.Errorf("expected\n%q\nbut got\n%q", tc.desc, desc) - } - } -} diff --git a/package.json b/package.json deleted file mode 100644 index 9565c7b..0000000 --- a/package.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "bugs": { - "url": "https://github.com/ipfs/go-ipfs-cmdkit" - }, - "gx": { - "dvcsimport": "github.com/ipfs/go-ipfs-cmdkit", - "goversion": "1.7" - }, - "gxDependencies": [ - { - "author": "The Go Authors", - "hash": "QmVGjyM9i2msKvLXwh9VosCTgP4mL91kC7hDmqnwTTx6Hu", - "name": "sys", - "version": "0.2.0" - } - ], - "gxVersion": "0.10.0", - "language": "go", - "license": "MIT", - "name": "go-ipfs-cmdkit", - "releaseCmd": "git commit -a -m \"gx publish $VERSION\"", - "version": "1.2.0" -} -