Skip to content

Commit

Permalink
Merge pull request #164 from ipfs/merge/cmdkit
Browse files Browse the repository at this point in the history
merge in go-ipfs-cmdkit
  • Loading branch information
Stebalien authored May 11, 2019
2 parents 6a7c2ca + 5346223 commit 72ee15c
Show file tree
Hide file tree
Showing 35 changed files with 692 additions and 266 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ env:
global:
- GOTFLAGS="-race"
matrix:
- BUILD_DEPTYPE=gx
- BUILD_DEPTYPE=gomod


Expand Down
56 changes: 56 additions & 0 deletions argument.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmds

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
}
10 changes: 4 additions & 6 deletions chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"io"
"sync"

"github.com/ipfs/go-ipfs-cmdkit"
)

func NewChanResponsePair(req *Request) (ResponseEmitter, Response) {
Expand Down Expand Up @@ -63,18 +61,18 @@ func (r *chanResponse) Request() *Request {
return r.req
}

func (r *chanResponse) Error() *cmdkit.Error {
func (r *chanResponse) Error() *Error {
select {
case <-r.closeCh:
if r.err == nil || r.err == io.EOF {
return nil
}

if e, ok := r.err.(*cmdkit.Error); ok {
if e, ok := r.err.(*Error); ok {
return e
}

return &cmdkit.Error{Message: r.err.Error()}
return &Error{Message: r.err.Error()}
default:
return nil
}
Expand Down Expand Up @@ -194,7 +192,7 @@ func (re *chanResponseEmitter) closeWithError(err error) {
err = io.EOF
}

if e, ok := err.(cmdkit.Error); ok {
if e, ok := err.(Error); ok {
err = &e
}

Expand Down
9 changes: 4 additions & 5 deletions cli/helptext.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"text/template"

"github.com/ipfs/go-ipfs-cmdkit"
"github.com/ipfs/go-ipfs-cmds"
)

Expand Down Expand Up @@ -252,13 +251,13 @@ func generateSynopsis(cmd *cmds.Command, path string) string {
if len(n) > 1 {
pre = "--"
}
if opt.Type() == cmdkit.Bool && opt.Default() == true {
if opt.Type() == cmds.Bool && opt.Default() == true {
pre = "--"
sopt = fmt.Sprintf("%s%s=false", pre, n)
break
} else {
if i == 0 {
if opt.Type() == cmdkit.Bool {
if opt.Type() == cmds.Bool {
sopt = fmt.Sprintf("%s%s", pre, n)
} else {
sopt = fmt.Sprintf("%s%s=<%s>", pre, n, valopt)
Expand Down Expand Up @@ -311,7 +310,7 @@ func optionFlag(flag string) string {

func optionText(cmd ...*cmds.Command) []string {
// get a slice of the options we want to list out
options := make([]cmdkit.Option, 0)
options := make([]cmds.Option, 0)
for _, c := range cmd {
for _, opt := range c.Options {
options = append(options, opt)
Expand Down Expand Up @@ -411,7 +410,7 @@ func usageText(cmd *cmds.Command) string {
return s
}

func argUsageText(arg cmdkit.Argument) string {
func argUsageText(arg cmds.Argument) string {
s := arg.Name

if arg.Required {
Expand Down
13 changes: 6 additions & 7 deletions cli/helptext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import (
"strings"
"testing"

"github.com/ipfs/go-ipfs-cmdkit"
"github.com/ipfs/go-ipfs-cmds"
)

func TestSynopsisGenerator(t *testing.T) {
command := &cmds.Command{
Arguments: []cmdkit.Argument{
cmdkit.StringArg("required", true, false, ""),
cmdkit.StringArg("variadic", false, true, ""),
Arguments: []cmds.Argument{
cmds.StringArg("required", true, false, ""),
cmds.StringArg("variadic", false, true, ""),
},
Options: []cmdkit.Option{
cmdkit.StringOption("opt", "o", "Option"),
Options: []cmds.Option{
cmds.StringOption("opt", "o", "Option"),
},
Helptext: cmdkit.HelpText{
Helptext: cmds.HelpText{
SynopsisOptionsValues: map[string]string{
"opt": "OPTION",
},
Expand Down
24 changes: 11 additions & 13 deletions cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import (
"path/filepath"
"strings"

"github.com/ipfs/go-ipfs-cmds"

osh "github.com/Kubuxu/go-os-helper"
"github.com/ipfs/go-ipfs-cmdkit"
"github.com/ipfs/go-ipfs-cmds"
"github.com/ipfs/go-ipfs-files"
logging "github.com/ipfs/go-log"
)
Expand Down Expand Up @@ -101,7 +99,7 @@ func parse(req *cmds.Request, cmdline []string, root *cmds.Command) (err error)
var (
path = make([]string, 0, len(cmdline))
args = make([]string, 0, len(cmdline))
opts = cmdkit.OptMap{}
opts = cmds.OptMap{}
cmd = root
)

Expand Down Expand Up @@ -246,7 +244,7 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {

fillingVariadic := iArgDef+1 > len(argDefs)
switch argDef.Type {
case cmdkit.ArgString:
case cmds.ArgString:
if len(inputs) > 0 {
stringArgs, inputs = append(stringArgs, inputs[0]), inputs[1:]
} else if stdin != nil && argDef.SupportsStdin && !fillingVariadic {
Expand All @@ -258,7 +256,7 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
stdin = nil
}
}
case cmdkit.ArgFile:
case cmds.ArgFile:
if len(inputs) > 0 {
// treat stringArg values as file paths
fpath := inputs[0]
Expand Down Expand Up @@ -390,7 +388,7 @@ func splitkv(opt string) (k, v string, ok bool) {
}
}

func parseOpt(opt, value string, opts map[string]cmdkit.Option) (interface{}, error) {
func parseOpt(opt, value string, opts map[string]cmds.Option) (interface{}, error) {
optDef, ok := opts[opt]
if !ok {
return nil, fmt.Errorf("unknown option %q", opt)
Expand All @@ -408,7 +406,7 @@ type kv struct {
Value interface{}
}

func (st *parseState) parseShortOpts(optDefs map[string]cmdkit.Option) ([]kv, error) {
func (st *parseState) parseShortOpts(optDefs map[string]cmds.Option) ([]kv, error) {
k, vStr, ok := splitkv(st.cmdline[st.i][1:])
kvs := make([]kv, 0, len(k))

Expand All @@ -431,7 +429,7 @@ func (st *parseState) parseShortOpts(optDefs map[string]cmdkit.Option) ([]kv, er
case !ok:
return nil, fmt.Errorf("unknown option %q", k)

case od.Type() == cmdkit.Bool:
case od.Type() == cmds.Bool:
// single char flags for bools
kvs = append(kvs, kv{
Key: flag,
Expand Down Expand Up @@ -471,14 +469,14 @@ func (st *parseState) parseShortOpts(optDefs map[string]cmdkit.Option) ([]kv, er
return kvs, nil
}

func (st *parseState) parseLongOpt(optDefs map[string]cmdkit.Option) (string, interface{}, error) {
func (st *parseState) parseLongOpt(optDefs map[string]cmds.Option) (string, interface{}, error) {
k, v, ok := splitkv(st.peek()[2:])
if !ok {
optDef, ok := optDefs[k]
if !ok {
return "", nil, fmt.Errorf("unknown option %q", k)
}
if optDef.Type() == cmdkit.Bool {
if optDef.Type() == cmds.Bool {
return k, true, nil
} else if st.i < len(st.cmdline)-1 {
st.i++
Expand All @@ -492,7 +490,7 @@ func (st *parseState) parseLongOpt(optDefs map[string]cmdkit.Option) (string, in
return k, optval, err
}

func getArgDef(i int, argDefs []cmdkit.Argument) *cmdkit.Argument {
func getArgDef(i int, argDefs []cmds.Argument) *cmds.Argument {
if i < len(argDefs) {
// get the argument definition (usually just argDefs[i])
return &argDefs[i]
Expand All @@ -509,7 +507,7 @@ func getArgDef(i int, argDefs []cmdkit.Argument) *cmdkit.Argument {
const notRecursiveFmtStr = "'%s' is a directory, use the '-%s' flag to specify directories"
const dirNotSupportedFmtStr = "invalid path '%s', argument '%s' does not support directories"

func appendFile(fpath string, argDef *cmdkit.Argument, recursive, hidden bool) (files.Node, error) {
func appendFile(fpath string, argDef *cmds.Argument, recursive, hidden bool) (files.Node, error) {
stat, err := os.Lstat(fpath)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 72ee15c

Please sign in to comment.