From c937e971af1914c098b092959bd938e53b2c44e9 Mon Sep 17 00:00:00 2001 From: Marty Schoch Date: Wed, 15 Feb 2017 15:36:47 -0500 Subject: [PATCH 1/2] bump manifest to latest cobra and pflag --- .../spf13/cobra/bash_completions.go | 8 +- .../vendor/github.com/spf13/cobra/cobra.go | 3 +- .../vendor/github.com/spf13/cobra/command.go | 116 +++++++++---- .../vendor/github.com/spf13/pflag/bool.go | 7 +- .../github.com/spf13/pflag/bool_slice.go | 147 +++++++++++++++++ .../vendor/github.com/spf13/pflag/count.go | 7 +- .../vendor/github.com/spf13/pflag/flag.go | 152 +++++++++++++++--- .../vendor/github.com/spf13/pflag/float32.go | 7 +- .../vendor/github.com/spf13/pflag/float64.go | 7 +- .../github.com/spf13/pflag/golangflag.go | 3 - .../vendor/github.com/spf13/pflag/int.go | 7 +- .../vendor/github.com/spf13/pflag/int32.go | 7 +- .../vendor/github.com/spf13/pflag/int64.go | 7 +- .../vendor/github.com/spf13/pflag/int8.go | 7 +- cmd/bleve/vendor/github.com/spf13/pflag/ip.go | 2 - .../vendor/github.com/spf13/pflag/ip_slice.go | 148 +++++++++++++++++ .../vendor/github.com/spf13/pflag/ipnet.go | 2 - .../vendor/github.com/spf13/pflag/string.go | 4 +- .../github.com/spf13/pflag/string_array.go | 9 +- .../github.com/spf13/pflag/string_slice.go | 7 +- .../vendor/github.com/spf13/pflag/uint.go | 7 +- .../vendor/github.com/spf13/pflag/uint16.go | 9 +- .../vendor/github.com/spf13/pflag/uint32.go | 11 +- .../vendor/github.com/spf13/pflag/uint64.go | 7 +- .../vendor/github.com/spf13/pflag/uint8.go | 7 +- .../github.com/spf13/pflag/uint_slice.go | 126 +++++++++++++++ cmd/bleve/vendor/manifest | 6 +- 27 files changed, 685 insertions(+), 145 deletions(-) create mode 100644 cmd/bleve/vendor/github.com/spf13/pflag/bool_slice.go create mode 100644 cmd/bleve/vendor/github.com/spf13/pflag/ip_slice.go create mode 100644 cmd/bleve/vendor/github.com/spf13/pflag/uint_slice.go diff --git a/cmd/bleve/vendor/github.com/spf13/cobra/bash_completions.go b/cmd/bleve/vendor/github.com/spf13/cobra/bash_completions.go index 7a5bd4d7..8820ba8f 100644 --- a/cmd/bleve/vendor/github.com/spf13/cobra/bash_completions.go +++ b/cmd/bleve/vendor/github.com/spf13/cobra/bash_completions.go @@ -10,6 +10,7 @@ import ( "github.com/spf13/pflag" ) +// Annotations for Bash completion. const ( BashCompFilenameExt = "cobra_annotation_bash_completion_filename_extensions" BashCompCustom = "cobra_annotation_bash_completion_custom" @@ -22,7 +23,7 @@ func preamble(out io.Writer, name string) error { if err != nil { return err } - _, err = fmt.Fprint(out, ` + preamStr := ` __debug() { if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then @@ -246,7 +247,8 @@ __handle_word() __handle_word } -`) +` + _, err = fmt.Fprint(out, preamStr) return err } @@ -566,6 +568,7 @@ func gen(cmd *Command, w io.Writer) error { return nil } +// GenBashCompletion generates bash completion file and writes to the passed writer. func (cmd *Command) GenBashCompletion(w io.Writer) error { if err := preamble(w, cmd.Name()); err != nil { return err @@ -585,6 +588,7 @@ func nonCompletableFlag(flag *pflag.Flag) bool { return flag.Hidden || len(flag.Deprecated) > 0 } +// GenBashCompletionFile generates bash completion file. func (cmd *Command) GenBashCompletionFile(filename string) error { outFile, err := os.Create(filename) if err != nil { diff --git a/cmd/bleve/vendor/github.com/spf13/cobra/cobra.go b/cmd/bleve/vendor/github.com/spf13/cobra/cobra.go index b39c715a..9605b984 100644 --- a/cmd/bleve/vendor/github.com/spf13/cobra/cobra.go +++ b/cmd/bleve/vendor/github.com/spf13/cobra/cobra.go @@ -37,7 +37,8 @@ var templateFuncs = template.FuncMap{ var initializers []func() -// Automatic prefix matching can be a dangerous thing to automatically enable in CLI tools. +// EnablePrefixMatching allows to set automatic prefix matching. Automatic prefix matching can be a dangerous thing +// to automatically enable in CLI tools. // Set this to true to enable it. var EnablePrefixMatching = false diff --git a/cmd/bleve/vendor/github.com/spf13/cobra/command.go b/cmd/bleve/vendor/github.com/spf13/cobra/command.go index 9ae98369..5c1c3a09 100644 --- a/cmd/bleve/vendor/github.com/spf13/cobra/command.go +++ b/cmd/bleve/vendor/github.com/spf13/cobra/command.go @@ -57,6 +57,9 @@ type Command struct { Deprecated string // Is this command hidden and should NOT show up in the list of available commands? Hidden bool + // Annotations are key/value pairs that can be used by applications to identify or + // group commands + Annotations map[string]string // Full set of flags flags *flag.FlagSet // Set of flags childrens of this command will inherit @@ -109,10 +112,11 @@ type Command struct { flagErrorBuf *bytes.Buffer - args []string // actual args parsed from flags - output *io.Writer // out writer if set in SetOutput(w) - usageFunc func(*Command) error // Usage can be defined by application - usageTemplate string // Can be defined by Application + args []string // actual args parsed from flags + output *io.Writer // out writer if set in SetOutput(w) + usageFunc func(*Command) error // Usage can be defined by application + usageTemplate string // Can be defined by Application + flagErrorFunc func(*Command, error) error helpTemplate string // Can be defined by Application helpFunc func(*Command, []string) // Help can be defined by application helpCommand *Command // The help command @@ -128,7 +132,7 @@ type Command struct { DisableFlagParsing bool } -// os.Args[1:] by default, if desired, can be overridden +// SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden // particularly useful when testing. func (c *Command) SetArgs(a []string) { c.args = a @@ -140,26 +144,33 @@ func (c *Command) SetOutput(output io.Writer) { c.output = &output } -// Usage can be defined by application. +// SetUsageFunc sets usage function. Usage can be defined by application. func (c *Command) SetUsageFunc(f func(*Command) error) { c.usageFunc = f } -// Can be defined by Application. +// SetUsageTemplate sets usage template. Can be defined by Application. func (c *Command) SetUsageTemplate(s string) { c.usageTemplate = s } -// Can be defined by Application. +// SetFlagErrorFunc sets a function to generate an error when flag parsing +// fails +func (c *Command) SetFlagErrorFunc(f func(*Command, error) error) { + c.flagErrorFunc = f +} + +// SetHelpFunc sets help function. Can be defined by Application func (c *Command) SetHelpFunc(f func(*Command, []string)) { c.helpFunc = f } +// SetHelpCommand sets help command. func (c *Command) SetHelpCommand(cmd *Command) { c.helpCommand = cmd } -// Can be defined by Application. +// SetHelpTemplate sets help template to be used. Application can use it to set custom template. func (c *Command) SetHelpTemplate(s string) { c.helpTemplate = s } @@ -176,10 +187,12 @@ func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string } } +// OutOrStdout returns output to stdout func (c *Command) OutOrStdout() io.Writer { return c.getOut(os.Stdout) } +// OutOrStderr returns output to stderr func (c *Command) OutOrStderr() io.Writer { return c.getOut(os.Stderr) } @@ -224,12 +237,8 @@ func (c *Command) Usage() error { // HelpFunc returns either the function set by SetHelpFunc for this command // or a parent, or it returns a function with default help behavior. func (c *Command) HelpFunc() func(*Command, []string) { - cmd := c - for cmd != nil { - if cmd.helpFunc != nil { - return cmd.helpFunc - } - cmd = cmd.parent + if helpFunc := c.checkHelpFunc(); helpFunc != nil { + return helpFunc } return func(*Command, []string) { c.mergePersistentFlags() @@ -240,6 +249,20 @@ func (c *Command) HelpFunc() func(*Command, []string) { } } +// checkHelpFunc checks if there is helpFunc in ancestors of c. +func (c *Command) checkHelpFunc() func(*Command, []string) { + if c == nil { + return nil + } + if c.helpFunc != nil { + return c.helpFunc + } + if c.HasParent() { + return c.parent.checkHelpFunc() + } + return nil +} + // Help puts out the help for the command. // Used when a user calls help [command]. // Can be defined by user by overriding HelpFunc. @@ -248,6 +271,7 @@ func (c *Command) Help() error { return nil } +// UsageString return usage string. func (c *Command) UsageString() string { tmpOutput := c.output bb := new(bytes.Buffer) @@ -257,8 +281,25 @@ func (c *Command) UsageString() string { return bb.String() } +// FlagErrorFunc returns either the function set by SetFlagErrorFunc for this +// command or a parent, or it returns a function which returns the original +// error. +func (c *Command) FlagErrorFunc() (f func(*Command, error) error) { + if c.flagErrorFunc != nil { + return c.flagErrorFunc + } + + if c.HasParent() { + return c.parent.FlagErrorFunc() + } + return func(c *Command, err error) error { + return err + } +} + var minUsagePadding = 25 +// UsagePadding return padding for the usage. func (c *Command) UsagePadding() int { if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen { return minUsagePadding @@ -268,7 +309,7 @@ func (c *Command) UsagePadding() int { var minCommandPathPadding = 11 -// +// CommandPathPadding return padding for the command path. func (c *Command) CommandPathPadding() int { if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen { return minCommandPathPadding @@ -278,6 +319,7 @@ func (c *Command) CommandPathPadding() int { var minNamePadding = 11 +// NamePadding returns padding for the name. func (c *Command) NamePadding() int { if c.parent == nil || minNamePadding > c.parent.commandsMaxNameLen { return minNamePadding @@ -285,6 +327,7 @@ func (c *Command) NamePadding() int { return c.parent.commandsMaxNameLen } +// UsageTemplate returns usage template for the command. func (c *Command) UsageTemplate() string { if c.usageTemplate != "" { return c.usageTemplate @@ -320,6 +363,7 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e ` } +// HelpTemplate return help template for the command. func (c *Command) HelpTemplate() string { if c.helpTemplate != "" { return c.helpTemplate @@ -385,7 +429,7 @@ func stripFlags(args []string, c *Command) []string { case inFlag: inFlag = false case y == "": - // strip empty commands, as the go tests expect this to be ok.... + // strip empty commands, as the go tests expect this to be ok.... case !strings.HasPrefix(y, "-"): commands = append(commands, y) inFlag = false @@ -414,7 +458,7 @@ func argsMinusFirstX(args []string, x string) []string { return args } -// find the target command given the args and command tree +// Find the target command given the args and command tree // Meant to be run on the highest node. Only searches down. func (c *Command) Find(args []string) (*Command, []string, error) { if c == nil { @@ -482,6 +526,7 @@ func (c *Command) Find(args []string) (*Command, []string, error) { return commandFound, a, nil } +// SuggestionsFor provides suggestions for the typedName. func (c *Command) SuggestionsFor(typedName string) []string { suggestions := []string{} for _, cmd := range c.commands { @@ -502,6 +547,7 @@ func (c *Command) SuggestionsFor(typedName string) []string { return suggestions } +// VisitParents visits all parents of the command and invokes fn on each parent. func (c *Command) VisitParents(fn func(*Command)) { var traverse func(*Command) *Command @@ -517,6 +563,7 @@ func (c *Command) VisitParents(fn func(*Command)) { traverse(c) } +// Root finds root command. func (c *Command) Root() *Command { var findRoot func(*Command) *Command @@ -553,7 +600,7 @@ func (c *Command) execute(a []string) (err error) { err = c.ParseFlags(a) if err != nil { - return err + return c.FlagErrorFunc()(c, err) } // If help is called, regardless of other flags, return we want help // Also say we need help if the command isn't runnable. @@ -641,7 +688,7 @@ func (c *Command) errorMsgFromParse() string { return "" } -// Call execute to use the args (os.Args[1:] by default) +// Execute Call execute to use the args (os.Args[1:] by default) // and run through the command tree finding appropriate matches // for commands and then corresponding flags. func (c *Command) Execute() error { @@ -649,6 +696,7 @@ func (c *Command) Execute() error { return err } +// ExecuteC executes the command. func (c *Command) ExecuteC() (cmd *Command, err error) { // Regardless of what command execute is called on, run on Root only @@ -712,6 +760,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) { } func (c *Command) initHelpFlag() { + c.mergePersistentFlags() if c.Flags().Lookup("help") == nil { c.Flags().BoolP("help", "h", false, "help for "+c.Name()) } @@ -745,7 +794,7 @@ func (c *Command) initHelpCmd() { c.AddCommand(c.helpCommand) } -// Used for testing. +// ResetCommands used for testing. func (c *Command) ResetCommands() { c.commands = nil c.helpCommand = nil @@ -868,7 +917,7 @@ func (c *Command) UseLine() string { return str + c.Use } -// For use in determining which flags have been assigned to which commands +// DebugFlags used to determine which flags have been assigned to which commands // and which persist. func (c *Command) DebugFlags() { c.Println("DebugFlags called on", c.Name()) @@ -923,7 +972,8 @@ func (c *Command) Name() string { if i >= 0 { name = name[:i] } - return name + c.name = name + return c.name } // HasAlias determines if a given string is an alias of the command. @@ -936,10 +986,12 @@ func (c *Command) HasAlias(s string) bool { return false } +// NameAndAliases returns string containing name and all aliases func (c *Command) NameAndAliases() string { return strings.Join(append([]string{c.Name()}, c.Aliases...), ", ") } +// HasExample determines if the command has example. func (c *Command) HasExample() bool { return len(c.Example) > 0 } @@ -1036,7 +1088,7 @@ func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) f return c.globNormFunc } -// Flage returns the complete FlagSet that applies +// Flags returns the complete FlagSet that applies // to this command (local and persistent declared here and by all parents). func (c *Command) Flags() *flag.FlagSet { if c.flags == nil { @@ -1136,44 +1188,44 @@ func (c *Command) ResetFlags() { c.pflags.SetOutput(c.flagErrorBuf) } -// Does the command contain any flags (local plus persistent from the entire structure). +// HasFlags checks if the command contains any flags (local plus persistent from the entire structure). func (c *Command) HasFlags() bool { return c.Flags().HasFlags() } -// Does the command contain persistent flags. +// HasPersistentFlags checks if the command contains persistent flags. func (c *Command) HasPersistentFlags() bool { return c.PersistentFlags().HasFlags() } -// Does the command has flags specifically declared locally. +// HasLocalFlags checks if the command has flags specifically declared locally. func (c *Command) HasLocalFlags() bool { return c.LocalFlags().HasFlags() } -// Does the command have flags inherited from its parent command. +// HasInheritedFlags checks if the command has flags inherited from its parent command. func (c *Command) HasInheritedFlags() bool { return c.InheritedFlags().HasFlags() } -// Does the command contain any flags (local plus persistent from the entire +// HasAvailableFlags checks if the command contains any flags (local plus persistent from the entire // structure) which are not hidden or deprecated. func (c *Command) HasAvailableFlags() bool { return c.Flags().HasAvailableFlags() } -// Does the command contain persistent flags which are not hidden or deprecated. +// HasAvailablePersistentFlags checks if the command contains persistent flags which are not hidden or deprecated. func (c *Command) HasAvailablePersistentFlags() bool { return c.PersistentFlags().HasAvailableFlags() } -// Does the command has flags specifically declared locally which are not hidden +// HasAvailableLocalFlags checks if the command has flags specifically declared locally which are not hidden // or deprecated. func (c *Command) HasAvailableLocalFlags() bool { return c.LocalFlags().HasAvailableFlags() } -// Does the command have flags inherited from its parent command which are +// HasAvailableInheritedFlags checks if the command has flags inherited from its parent command which are // not hidden or deprecated. func (c *Command) HasAvailableInheritedFlags() bool { return c.InheritedFlags().HasAvailableFlags() diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/bool.go b/cmd/bleve/vendor/github.com/spf13/pflag/bool.go index d272e40b..c4c5c0bf 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/bool.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/bool.go @@ -1,9 +1,6 @@ package pflag -import ( - "fmt" - "strconv" -) +import "strconv" // optional interface to indicate boolean flags that can be // supplied without "=value" text @@ -30,7 +27,7 @@ func (b *boolValue) Type() string { return "bool" } -func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) } +func (b *boolValue) String() string { return strconv.FormatBool(bool(*b)) } func (b *boolValue) IsBoolFlag() bool { return true } diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/bool_slice.go b/cmd/bleve/vendor/github.com/spf13/pflag/bool_slice.go new file mode 100644 index 00000000..5af02f1a --- /dev/null +++ b/cmd/bleve/vendor/github.com/spf13/pflag/bool_slice.go @@ -0,0 +1,147 @@ +package pflag + +import ( + "io" + "strconv" + "strings" +) + +// -- boolSlice Value +type boolSliceValue struct { + value *[]bool + changed bool +} + +func newBoolSliceValue(val []bool, p *[]bool) *boolSliceValue { + bsv := new(boolSliceValue) + bsv.value = p + *bsv.value = val + return bsv +} + +// Set converts, and assigns, the comma-separated boolean argument string representation as the []bool value of this flag. +// If Set is called on a flag that already has a []bool assigned, the newly converted values will be appended. +func (s *boolSliceValue) Set(val string) error { + + // remove all quote characters + rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "") + + // read flag arguments with CSV parser + boolStrSlice, err := readAsCSV(rmQuote.Replace(val)) + if err != nil && err != io.EOF { + return err + } + + // parse boolean values into slice + out := make([]bool, 0, len(boolStrSlice)) + for _, boolStr := range boolStrSlice { + b, err := strconv.ParseBool(strings.TrimSpace(boolStr)) + if err != nil { + return err + } + out = append(out, b) + } + + if !s.changed { + *s.value = out + } else { + *s.value = append(*s.value, out...) + } + + s.changed = true + + return nil +} + +// Type returns a string that uniquely represents this flag's type. +func (s *boolSliceValue) Type() string { + return "boolSlice" +} + +// String defines a "native" format for this boolean slice flag value. +func (s *boolSliceValue) String() string { + + boolStrSlice := make([]string, len(*s.value)) + for i, b := range *s.value { + boolStrSlice[i] = strconv.FormatBool(b) + } + + out, _ := writeAsCSV(boolStrSlice) + + return "[" + out + "]" +} + +func boolSliceConv(val string) (interface{}, error) { + val = strings.Trim(val, "[]") + // Empty string would cause a slice with one (empty) entry + if len(val) == 0 { + return []bool{}, nil + } + ss := strings.Split(val, ",") + out := make([]bool, len(ss)) + for i, t := range ss { + var err error + out[i], err = strconv.ParseBool(t) + if err != nil { + return nil, err + } + } + return out, nil +} + +// GetBoolSlice returns the []bool value of a flag with the given name. +func (f *FlagSet) GetBoolSlice(name string) ([]bool, error) { + val, err := f.getFlagType(name, "boolSlice", boolSliceConv) + if err != nil { + return []bool{}, err + } + return val.([]bool), nil +} + +// BoolSliceVar defines a boolSlice flag with specified name, default value, and usage string. +// The argument p points to a []bool variable in which to store the value of the flag. +func (f *FlagSet) BoolSliceVar(p *[]bool, name string, value []bool, usage string) { + f.VarP(newBoolSliceValue(value, p), name, "", usage) +} + +// BoolSliceVarP is like BoolSliceVar, but accepts a shorthand letter that can be used after a single dash. +func (f *FlagSet) BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string) { + f.VarP(newBoolSliceValue(value, p), name, shorthand, usage) +} + +// BoolSliceVar defines a []bool flag with specified name, default value, and usage string. +// The argument p points to a []bool variable in which to store the value of the flag. +func BoolSliceVar(p *[]bool, name string, value []bool, usage string) { + CommandLine.VarP(newBoolSliceValue(value, p), name, "", usage) +} + +// BoolSliceVarP is like BoolSliceVar, but accepts a shorthand letter that can be used after a single dash. +func BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string) { + CommandLine.VarP(newBoolSliceValue(value, p), name, shorthand, usage) +} + +// BoolSlice defines a []bool flag with specified name, default value, and usage string. +// The return value is the address of a []bool variable that stores the value of the flag. +func (f *FlagSet) BoolSlice(name string, value []bool, usage string) *[]bool { + p := []bool{} + f.BoolSliceVarP(&p, name, "", value, usage) + return &p +} + +// BoolSliceP is like BoolSlice, but accepts a shorthand letter that can be used after a single dash. +func (f *FlagSet) BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool { + p := []bool{} + f.BoolSliceVarP(&p, name, shorthand, value, usage) + return &p +} + +// BoolSlice defines a []bool flag with specified name, default value, and usage string. +// The return value is the address of a []bool variable that stores the value of the flag. +func BoolSlice(name string, value []bool, usage string) *[]bool { + return CommandLine.BoolSliceP(name, "", value, usage) +} + +// BoolSliceP is like BoolSlice, but accepts a shorthand letter that can be used after a single dash. +func BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool { + return CommandLine.BoolSliceP(name, shorthand, value, usage) +} diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/count.go b/cmd/bleve/vendor/github.com/spf13/pflag/count.go index 7b1f142e..d22be41f 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/count.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/count.go @@ -1,9 +1,6 @@ package pflag -import ( - "fmt" - "strconv" -) +import "strconv" // -- count Value type countValue int @@ -28,7 +25,7 @@ func (i *countValue) Type() string { return "count" } -func (i *countValue) String() string { return fmt.Sprintf("%v", *i) } +func (i *countValue) String() string { return strconv.Itoa(int(*i)) } func countConv(sval string) (interface{}, error) { i, err := strconv.Atoi(sval) diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/flag.go b/cmd/bleve/vendor/github.com/spf13/pflag/flag.go index b0b0d46f..746af632 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/flag.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/flag.go @@ -416,7 +416,7 @@ func Set(name, value string) error { // otherwise, the default values of all defined flags in the set. func (f *FlagSet) PrintDefaults() { usages := f.FlagUsages() - fmt.Fprintf(f.out(), "%s", usages) + fmt.Fprint(f.out(), usages) } // defaultIsZeroValue returns true if the default value for this flag represents @@ -487,9 +487,76 @@ func UnquoteUsage(flag *Flag) (name string, usage string) { return } -// FlagUsages Returns a string containing the usage information for all flags in -// the FlagSet -func (f *FlagSet) FlagUsages() string { +// Splits the string `s` on whitespace into an initial substring up to +// `i` runes in length and the remainder. Will go `slop` over `i` if +// that encompasses the entire string (which allows the caller to +// avoid short orphan words on the final line). +func wrapN(i, slop int, s string) (string, string) { + if i+slop > len(s) { + return s, "" + } + + w := strings.LastIndexAny(s[:i], " \t") + if w <= 0 { + return s, "" + } + + return s[:w], s[w+1:] +} + +// Wraps the string `s` to a maximum width `w` with leading indent +// `i`. The first line is not indented (this is assumed to be done by +// caller). Pass `w` == 0 to do no wrapping +func wrap(i, w int, s string) string { + if w == 0 { + return s + } + + // space between indent i and end of line width w into which + // we should wrap the text. + wrap := w - i + + var r, l string + + // Not enough space for sensible wrapping. Wrap as a block on + // the next line instead. + if wrap < 24 { + i = 16 + wrap = w - i + r += "\n" + strings.Repeat(" ", i) + } + // If still not enough space then don't even try to wrap. + if wrap < 24 { + return s + } + + // Try to avoid short orphan words on the final line, by + // allowing wrapN to go a bit over if that would fit in the + // remainder of the line. + slop := 5 + wrap = wrap - slop + + // Handle first line, which is indented by the caller (or the + // special case above) + l, s = wrapN(wrap, slop, s) + r = r + l + + // Now wrap the rest + for s != "" { + var t string + + t, s = wrapN(wrap, slop, s) + r = r + "\n" + strings.Repeat(" ", i) + t + } + + return r + +} + +// FlagUsagesWrapped returns a string containing the usage information +// for all flags in the FlagSet. Wrapped to `cols` columns (0 for no +// wrapping) +func (f *FlagSet) FlagUsagesWrapped(cols int) string { x := new(bytes.Buffer) lines := make([]string, 0, len(f.formal)) @@ -514,7 +581,7 @@ func (f *FlagSet) FlagUsages() string { if len(flag.NoOptDefVal) > 0 { switch flag.Value.Type() { case "string": - line += fmt.Sprintf("[=%q]", flag.NoOptDefVal) + line += fmt.Sprintf("[=\"%s\"]", flag.NoOptDefVal) case "bool": if flag.NoOptDefVal != "true" { line += fmt.Sprintf("[=%s]", flag.NoOptDefVal) @@ -534,7 +601,7 @@ func (f *FlagSet) FlagUsages() string { line += usage if !flag.defaultIsZeroValue() { if flag.Value.Type() == "string" { - line += fmt.Sprintf(" (default %q)", flag.DefValue) + line += fmt.Sprintf(" (default \"%s\")", flag.DefValue) } else { line += fmt.Sprintf(" (default %s)", flag.DefValue) } @@ -546,12 +613,19 @@ func (f *FlagSet) FlagUsages() string { for _, line := range lines { sidx := strings.Index(line, "\x00") spacing := strings.Repeat(" ", maxlen-sidx) - fmt.Fprintln(x, line[:sidx], spacing, line[sidx+1:]) + // maxlen + 2 comes from + 1 for the \x00 and + 1 for the (deliberate) off-by-one in maxlen-sidx + fmt.Fprintln(x, line[:sidx], spacing, wrap(maxlen+2, cols, line[sidx+1:])) } return x.String() } +// FlagUsages returns a string containing the usage information for all flags in +// the FlagSet +func (f *FlagSet) FlagUsages() string { + return f.FlagUsagesWrapped(0) +} + // PrintDefaults prints to standard error the default values of all defined command-line flags. func PrintDefaults() { CommandLine.PrintDefaults() @@ -635,7 +709,7 @@ func (f *FlagSet) VarPF(value Value, name, shorthand, usage string) *Flag { // VarP is like Var, but accepts a shorthand letter that can be used after a single dash. func (f *FlagSet) VarP(value Value, name, shorthand, usage string) { - _ = f.VarPF(value, name, shorthand, usage) + f.VarPF(value, name, shorthand, usage) } // AddFlag will add the flag to the FlagSet @@ -752,7 +826,7 @@ func containsShorthand(arg, shorthand string) bool { return strings.Contains(arg, shorthand) } -func (f *FlagSet) parseLongArg(s string, args []string) (a []string, err error) { +func (f *FlagSet) parseLongArg(s string, args []string, fn parseFunc) (a []string, err error) { a = args name := s[2:] if len(name) == 0 || name[0] == '-' || name[0] == '=' { @@ -786,11 +860,11 @@ func (f *FlagSet) parseLongArg(s string, args []string) (a []string, err error) err = f.failf("flag needs an argument: %s", s) return } - err = f.setFlag(flag, value, s) + err = fn(flag, value, s) return } -func (f *FlagSet) parseSingleShortArg(shorthands string, args []string) (outShorts string, outArgs []string, err error) { +func (f *FlagSet) parseSingleShortArg(shorthands string, args []string, fn parseFunc) (outShorts string, outArgs []string, err error) { if strings.HasPrefix(shorthands, "test.") { return } @@ -825,16 +899,16 @@ func (f *FlagSet) parseSingleShortArg(shorthands string, args []string) (outShor err = f.failf("flag needs an argument: %q in -%s", c, shorthands) return } - err = f.setFlag(flag, value, shorthands) + err = fn(flag, value, shorthands) return } -func (f *FlagSet) parseShortArg(s string, args []string) (a []string, err error) { +func (f *FlagSet) parseShortArg(s string, args []string, fn parseFunc) (a []string, err error) { a = args shorthands := s[1:] for len(shorthands) > 0 { - shorthands, a, err = f.parseSingleShortArg(shorthands, args) + shorthands, a, err = f.parseSingleShortArg(shorthands, args, fn) if err != nil { return } @@ -843,7 +917,7 @@ func (f *FlagSet) parseShortArg(s string, args []string) (a []string, err error) return } -func (f *FlagSet) parseArgs(args []string) (err error) { +func (f *FlagSet) parseArgs(args []string, fn parseFunc) (err error) { for len(args) > 0 { s := args[0] args = args[1:] @@ -863,9 +937,9 @@ func (f *FlagSet) parseArgs(args []string) (err error) { f.args = append(f.args, args...) break } - args, err = f.parseLongArg(s, args) + args, err = f.parseLongArg(s, args, fn) } else { - args, err = f.parseShortArg(s, args) + args, err = f.parseShortArg(s, args, fn) } if err != nil { return @@ -881,7 +955,41 @@ func (f *FlagSet) parseArgs(args []string) (err error) { func (f *FlagSet) Parse(arguments []string) error { f.parsed = true f.args = make([]string, 0, len(arguments)) - err := f.parseArgs(arguments) + + assign := func(flag *Flag, value, origArg string) error { + return f.setFlag(flag, value, origArg) + } + + err := f.parseArgs(arguments, assign) + if err != nil { + switch f.errorHandling { + case ContinueOnError: + return err + case ExitOnError: + os.Exit(2) + case PanicOnError: + panic(err) + } + } + return nil +} + +type parseFunc func(flag *Flag, value, origArg string) error + +// ParseAll parses flag definitions from the argument list, which should not +// include the command name. The arguments for fn are flag and value. Must be +// called after all flags in the FlagSet are defined and before flags are +// accessed by the program. The return value will be ErrHelp if -help was set +// but not defined. +func (f *FlagSet) ParseAll(arguments []string, fn func(flag *Flag, value string) error) error { + f.parsed = true + f.args = make([]string, 0, len(arguments)) + + assign := func(flag *Flag, value, origArg string) error { + return fn(flag, value) + } + + err := f.parseArgs(arguments, assign) if err != nil { switch f.errorHandling { case ContinueOnError: @@ -907,6 +1015,14 @@ func Parse() { CommandLine.Parse(os.Args[1:]) } +// ParseAll parses the command-line flags from os.Args[1:] and called fn for each. +// The arguments for fn are flag and value. Must be called after all flags are +// defined and before flags are accessed by the program. +func ParseAll(fn func(flag *Flag, value string) error) { + // Ignore errors; CommandLine is set for ExitOnError. + CommandLine.ParseAll(os.Args[1:], fn) +} + // SetInterspersed sets whether to support interspersed option/non-option arguments. func SetInterspersed(interspersed bool) { CommandLine.SetInterspersed(interspersed) diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/float32.go b/cmd/bleve/vendor/github.com/spf13/pflag/float32.go index 7683fae1..a243f81f 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/float32.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/float32.go @@ -1,9 +1,6 @@ package pflag -import ( - "fmt" - "strconv" -) +import "strconv" // -- float32 Value type float32Value float32 @@ -23,7 +20,7 @@ func (f *float32Value) Type() string { return "float32" } -func (f *float32Value) String() string { return fmt.Sprintf("%v", *f) } +func (f *float32Value) String() string { return strconv.FormatFloat(float64(*f), 'g', -1, 32) } func float32Conv(sval string) (interface{}, error) { v, err := strconv.ParseFloat(sval, 32) diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/float64.go b/cmd/bleve/vendor/github.com/spf13/pflag/float64.go index 50fbf8cc..04b5492a 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/float64.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/float64.go @@ -1,9 +1,6 @@ package pflag -import ( - "fmt" - "strconv" -) +import "strconv" // -- float64 Value type float64Value float64 @@ -23,7 +20,7 @@ func (f *float64Value) Type() string { return "float64" } -func (f *float64Value) String() string { return fmt.Sprintf("%v", *f) } +func (f *float64Value) String() string { return strconv.FormatFloat(float64(*f), 'g', -1, 64) } func float64Conv(sval string) (interface{}, error) { return strconv.ParseFloat(sval, 64) diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/golangflag.go b/cmd/bleve/vendor/github.com/spf13/pflag/golangflag.go index b056147f..c4f47ebe 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/golangflag.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/golangflag.go @@ -6,13 +6,10 @@ package pflag import ( goflag "flag" - "fmt" "reflect" "strings" ) -var _ = fmt.Print - // flagValueWrapper implements pflag.Value around a flag.Value. The main // difference here is the addition of the Type method that returns a string // name of the type. As this is generally unknown, we approximate that with diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/int.go b/cmd/bleve/vendor/github.com/spf13/pflag/int.go index b6560368..1474b89d 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/int.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/int.go @@ -1,9 +1,6 @@ package pflag -import ( - "fmt" - "strconv" -) +import "strconv" // -- int Value type intValue int @@ -23,7 +20,7 @@ func (i *intValue) Type() string { return "int" } -func (i *intValue) String() string { return fmt.Sprintf("%v", *i) } +func (i *intValue) String() string { return strconv.Itoa(int(*i)) } func intConv(sval string) (interface{}, error) { return strconv.Atoi(sval) diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/int32.go b/cmd/bleve/vendor/github.com/spf13/pflag/int32.go index 41659a9a..9b95944f 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/int32.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/int32.go @@ -1,9 +1,6 @@ package pflag -import ( - "fmt" - "strconv" -) +import "strconv" // -- int32 Value type int32Value int32 @@ -23,7 +20,7 @@ func (i *int32Value) Type() string { return "int32" } -func (i *int32Value) String() string { return fmt.Sprintf("%v", *i) } +func (i *int32Value) String() string { return strconv.FormatInt(int64(*i), 10) } func int32Conv(sval string) (interface{}, error) { v, err := strconv.ParseInt(sval, 0, 32) diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/int64.go b/cmd/bleve/vendor/github.com/spf13/pflag/int64.go index 6e67e380..0026d781 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/int64.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/int64.go @@ -1,9 +1,6 @@ package pflag -import ( - "fmt" - "strconv" -) +import "strconv" // -- int64 Value type int64Value int64 @@ -23,7 +20,7 @@ func (i *int64Value) Type() string { return "int64" } -func (i *int64Value) String() string { return fmt.Sprintf("%v", *i) } +func (i *int64Value) String() string { return strconv.FormatInt(int64(*i), 10) } func int64Conv(sval string) (interface{}, error) { return strconv.ParseInt(sval, 0, 64) diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/int8.go b/cmd/bleve/vendor/github.com/spf13/pflag/int8.go index 400db21f..4da92228 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/int8.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/int8.go @@ -1,9 +1,6 @@ package pflag -import ( - "fmt" - "strconv" -) +import "strconv" // -- int8 Value type int8Value int8 @@ -23,7 +20,7 @@ func (i *int8Value) Type() string { return "int8" } -func (i *int8Value) String() string { return fmt.Sprintf("%v", *i) } +func (i *int8Value) String() string { return strconv.FormatInt(int64(*i), 10) } func int8Conv(sval string) (interface{}, error) { v, err := strconv.ParseInt(sval, 0, 8) diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/ip.go b/cmd/bleve/vendor/github.com/spf13/pflag/ip.go index 88a17430..3d414ba6 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/ip.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/ip.go @@ -6,8 +6,6 @@ import ( "strings" ) -var _ = strings.TrimSpace - // -- net.IP value type ipValue net.IP diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/ip_slice.go b/cmd/bleve/vendor/github.com/spf13/pflag/ip_slice.go new file mode 100644 index 00000000..7dd196fe --- /dev/null +++ b/cmd/bleve/vendor/github.com/spf13/pflag/ip_slice.go @@ -0,0 +1,148 @@ +package pflag + +import ( + "fmt" + "io" + "net" + "strings" +) + +// -- ipSlice Value +type ipSliceValue struct { + value *[]net.IP + changed bool +} + +func newIPSliceValue(val []net.IP, p *[]net.IP) *ipSliceValue { + ipsv := new(ipSliceValue) + ipsv.value = p + *ipsv.value = val + return ipsv +} + +// Set converts, and assigns, the comma-separated IP argument string representation as the []net.IP value of this flag. +// If Set is called on a flag that already has a []net.IP assigned, the newly converted values will be appended. +func (s *ipSliceValue) Set(val string) error { + + // remove all quote characters + rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "") + + // read flag arguments with CSV parser + ipStrSlice, err := readAsCSV(rmQuote.Replace(val)) + if err != nil && err != io.EOF { + return err + } + + // parse ip values into slice + out := make([]net.IP, 0, len(ipStrSlice)) + for _, ipStr := range ipStrSlice { + ip := net.ParseIP(strings.TrimSpace(ipStr)) + if ip == nil { + return fmt.Errorf("invalid string being converted to IP address: %s", ipStr) + } + out = append(out, ip) + } + + if !s.changed { + *s.value = out + } else { + *s.value = append(*s.value, out...) + } + + s.changed = true + + return nil +} + +// Type returns a string that uniquely represents this flag's type. +func (s *ipSliceValue) Type() string { + return "ipSlice" +} + +// String defines a "native" format for this net.IP slice flag value. +func (s *ipSliceValue) String() string { + + ipStrSlice := make([]string, len(*s.value)) + for i, ip := range *s.value { + ipStrSlice[i] = ip.String() + } + + out, _ := writeAsCSV(ipStrSlice) + + return "[" + out + "]" +} + +func ipSliceConv(val string) (interface{}, error) { + val = strings.Trim(val, "[]") + // Emtpy string would cause a slice with one (empty) entry + if len(val) == 0 { + return []net.IP{}, nil + } + ss := strings.Split(val, ",") + out := make([]net.IP, len(ss)) + for i, sval := range ss { + ip := net.ParseIP(strings.TrimSpace(sval)) + if ip == nil { + return nil, fmt.Errorf("invalid string being converted to IP address: %s", sval) + } + out[i] = ip + } + return out, nil +} + +// GetIPSlice returns the []net.IP value of a flag with the given name +func (f *FlagSet) GetIPSlice(name string) ([]net.IP, error) { + val, err := f.getFlagType(name, "ipSlice", ipSliceConv) + if err != nil { + return []net.IP{}, err + } + return val.([]net.IP), nil +} + +// IPSliceVar defines a ipSlice flag with specified name, default value, and usage string. +// The argument p points to a []net.IP variable in which to store the value of the flag. +func (f *FlagSet) IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string) { + f.VarP(newIPSliceValue(value, p), name, "", usage) +} + +// IPSliceVarP is like IPSliceVar, but accepts a shorthand letter that can be used after a single dash. +func (f *FlagSet) IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string) { + f.VarP(newIPSliceValue(value, p), name, shorthand, usage) +} + +// IPSliceVar defines a []net.IP flag with specified name, default value, and usage string. +// The argument p points to a []net.IP variable in which to store the value of the flag. +func IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string) { + CommandLine.VarP(newIPSliceValue(value, p), name, "", usage) +} + +// IPSliceVarP is like IPSliceVar, but accepts a shorthand letter that can be used after a single dash. +func IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string) { + CommandLine.VarP(newIPSliceValue(value, p), name, shorthand, usage) +} + +// IPSlice defines a []net.IP flag with specified name, default value, and usage string. +// The return value is the address of a []net.IP variable that stores the value of that flag. +func (f *FlagSet) IPSlice(name string, value []net.IP, usage string) *[]net.IP { + p := []net.IP{} + f.IPSliceVarP(&p, name, "", value, usage) + return &p +} + +// IPSliceP is like IPSlice, but accepts a shorthand letter that can be used after a single dash. +func (f *FlagSet) IPSliceP(name, shorthand string, value []net.IP, usage string) *[]net.IP { + p := []net.IP{} + f.IPSliceVarP(&p, name, shorthand, value, usage) + return &p +} + +// IPSlice defines a []net.IP flag with specified name, default value, and usage string. +// The return value is the address of a []net.IP variable that stores the value of the flag. +func IPSlice(name string, value []net.IP, usage string) *[]net.IP { + return CommandLine.IPSliceP(name, "", value, usage) +} + +// IPSliceP is like IPSlice, but accepts a shorthand letter that can be used after a single dash. +func IPSliceP(name, shorthand string, value []net.IP, usage string) *[]net.IP { + return CommandLine.IPSliceP(name, shorthand, value, usage) +} diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/ipnet.go b/cmd/bleve/vendor/github.com/spf13/pflag/ipnet.go index 149b764b..e2c1b8bc 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/ipnet.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/ipnet.go @@ -27,8 +27,6 @@ func (*ipNetValue) Type() string { return "ipNet" } -var _ = strings.TrimSpace - func newIPNetValue(val net.IPNet, p *net.IPNet) *ipNetValue { *p = val return (*ipNetValue)(p) diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/string.go b/cmd/bleve/vendor/github.com/spf13/pflag/string.go index e296136e..04e0a26f 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/string.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/string.go @@ -1,7 +1,5 @@ package pflag -import "fmt" - // -- string Value type stringValue string @@ -18,7 +16,7 @@ func (s *stringValue) Type() string { return "string" } -func (s *stringValue) String() string { return fmt.Sprintf("%s", *s) } +func (s *stringValue) String() string { return string(*s) } func stringConv(sval string) (interface{}, error) { return sval, nil diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/string_array.go b/cmd/bleve/vendor/github.com/spf13/pflag/string_array.go index f320f2ec..276b7ed4 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/string_array.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/string_array.go @@ -1,12 +1,5 @@ package pflag -import ( - "fmt" - "strings" -) - -var _ = fmt.Fprint - // -- stringArray Value type stringArrayValue struct { value *[]string @@ -40,7 +33,7 @@ func (s *stringArrayValue) String() string { } func stringArrayConv(sval string) (interface{}, error) { - sval = strings.Trim(sval, "[]") + sval = sval[1 : len(sval)-1] // An empty string would cause a array with one (empty) string if len(sval) == 0 { return []string{}, nil diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/string_slice.go b/cmd/bleve/vendor/github.com/spf13/pflag/string_slice.go index 51e3c5d2..05eee754 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/string_slice.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/string_slice.go @@ -3,12 +3,9 @@ package pflag import ( "bytes" "encoding/csv" - "fmt" "strings" ) -var _ = fmt.Fprint - // -- stringSlice Value type stringSliceValue struct { value *[]string @@ -39,7 +36,7 @@ func writeAsCSV(vals []string) (string, error) { return "", err } w.Flush() - return strings.TrimSuffix(b.String(), fmt.Sprintln()), nil + return strings.TrimSuffix(b.String(), "\n"), nil } func (s *stringSliceValue) Set(val string) error { @@ -66,7 +63,7 @@ func (s *stringSliceValue) String() string { } func stringSliceConv(sval string) (interface{}, error) { - sval = strings.Trim(sval, "[]") + sval = sval[1 : len(sval)-1] // An empty string would cause a slice with one (empty) string if len(sval) == 0 { return []string{}, nil diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/uint.go b/cmd/bleve/vendor/github.com/spf13/pflag/uint.go index e142b499..dcbc2b75 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/uint.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/uint.go @@ -1,9 +1,6 @@ package pflag -import ( - "fmt" - "strconv" -) +import "strconv" // -- uint Value type uintValue uint @@ -23,7 +20,7 @@ func (i *uintValue) Type() string { return "uint" } -func (i *uintValue) String() string { return fmt.Sprintf("%v", *i) } +func (i *uintValue) String() string { return strconv.FormatUint(uint64(*i), 10) } func uintConv(sval string) (interface{}, error) { v, err := strconv.ParseUint(sval, 0, 0) diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/uint16.go b/cmd/bleve/vendor/github.com/spf13/pflag/uint16.go index 5c96c19d..7e9914ed 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/uint16.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/uint16.go @@ -1,9 +1,6 @@ package pflag -import ( - "fmt" - "strconv" -) +import "strconv" // -- uint16 value type uint16Value uint16 @@ -12,7 +9,7 @@ func newUint16Value(val uint16, p *uint16) *uint16Value { *p = val return (*uint16Value)(p) } -func (i *uint16Value) String() string { return fmt.Sprintf("%d", *i) } + func (i *uint16Value) Set(s string) error { v, err := strconv.ParseUint(s, 0, 16) *i = uint16Value(v) @@ -23,6 +20,8 @@ func (i *uint16Value) Type() string { return "uint16" } +func (i *uint16Value) String() string { return strconv.FormatUint(uint64(*i), 10) } + func uint16Conv(sval string) (interface{}, error) { v, err := strconv.ParseUint(sval, 0, 16) if err != nil { diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/uint32.go b/cmd/bleve/vendor/github.com/spf13/pflag/uint32.go index 294fcaa3..d8024539 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/uint32.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/uint32.go @@ -1,18 +1,15 @@ package pflag -import ( - "fmt" - "strconv" -) +import "strconv" -// -- uint16 value +// -- uint32 value type uint32Value uint32 func newUint32Value(val uint32, p *uint32) *uint32Value { *p = val return (*uint32Value)(p) } -func (i *uint32Value) String() string { return fmt.Sprintf("%d", *i) } + func (i *uint32Value) Set(s string) error { v, err := strconv.ParseUint(s, 0, 32) *i = uint32Value(v) @@ -23,6 +20,8 @@ func (i *uint32Value) Type() string { return "uint32" } +func (i *uint32Value) String() string { return strconv.FormatUint(uint64(*i), 10) } + func uint32Conv(sval string) (interface{}, error) { v, err := strconv.ParseUint(sval, 0, 32) if err != nil { diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/uint64.go b/cmd/bleve/vendor/github.com/spf13/pflag/uint64.go index c6818850..f62240f2 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/uint64.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/uint64.go @@ -1,9 +1,6 @@ package pflag -import ( - "fmt" - "strconv" -) +import "strconv" // -- uint64 Value type uint64Value uint64 @@ -23,7 +20,7 @@ func (i *uint64Value) Type() string { return "uint64" } -func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i) } +func (i *uint64Value) String() string { return strconv.FormatUint(uint64(*i), 10) } func uint64Conv(sval string) (interface{}, error) { v, err := strconv.ParseUint(sval, 0, 64) diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/uint8.go b/cmd/bleve/vendor/github.com/spf13/pflag/uint8.go index 26db418a..bb0e83c1 100644 --- a/cmd/bleve/vendor/github.com/spf13/pflag/uint8.go +++ b/cmd/bleve/vendor/github.com/spf13/pflag/uint8.go @@ -1,9 +1,6 @@ package pflag -import ( - "fmt" - "strconv" -) +import "strconv" // -- uint8 Value type uint8Value uint8 @@ -23,7 +20,7 @@ func (i *uint8Value) Type() string { return "uint8" } -func (i *uint8Value) String() string { return fmt.Sprintf("%v", *i) } +func (i *uint8Value) String() string { return strconv.FormatUint(uint64(*i), 10) } func uint8Conv(sval string) (interface{}, error) { v, err := strconv.ParseUint(sval, 0, 8) diff --git a/cmd/bleve/vendor/github.com/spf13/pflag/uint_slice.go b/cmd/bleve/vendor/github.com/spf13/pflag/uint_slice.go new file mode 100644 index 00000000..edd94c60 --- /dev/null +++ b/cmd/bleve/vendor/github.com/spf13/pflag/uint_slice.go @@ -0,0 +1,126 @@ +package pflag + +import ( + "fmt" + "strconv" + "strings" +) + +// -- uintSlice Value +type uintSliceValue struct { + value *[]uint + changed bool +} + +func newUintSliceValue(val []uint, p *[]uint) *uintSliceValue { + uisv := new(uintSliceValue) + uisv.value = p + *uisv.value = val + return uisv +} + +func (s *uintSliceValue) Set(val string) error { + ss := strings.Split(val, ",") + out := make([]uint, len(ss)) + for i, d := range ss { + u, err := strconv.ParseUint(d, 10, 0) + if err != nil { + return err + } + out[i] = uint(u) + } + if !s.changed { + *s.value = out + } else { + *s.value = append(*s.value, out...) + } + s.changed = true + return nil +} + +func (s *uintSliceValue) Type() string { + return "uintSlice" +} + +func (s *uintSliceValue) String() string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = fmt.Sprintf("%d", d) + } + return "[" + strings.Join(out, ",") + "]" +} + +func uintSliceConv(val string) (interface{}, error) { + val = strings.Trim(val, "[]") + // Empty string would cause a slice with one (empty) entry + if len(val) == 0 { + return []uint{}, nil + } + ss := strings.Split(val, ",") + out := make([]uint, len(ss)) + for i, d := range ss { + u, err := strconv.ParseUint(d, 10, 0) + if err != nil { + return nil, err + } + out[i] = uint(u) + } + return out, nil +} + +// GetUintSlice returns the []uint value of a flag with the given name. +func (f *FlagSet) GetUintSlice(name string) ([]uint, error) { + val, err := f.getFlagType(name, "uintSlice", uintSliceConv) + if err != nil { + return []uint{}, err + } + return val.([]uint), nil +} + +// UintSliceVar defines a uintSlice flag with specified name, default value, and usage string. +// The argument p points to a []uint variable in which to store the value of the flag. +func (f *FlagSet) UintSliceVar(p *[]uint, name string, value []uint, usage string) { + f.VarP(newUintSliceValue(value, p), name, "", usage) +} + +// UintSliceVarP is like UintSliceVar, but accepts a shorthand letter that can be used after a single dash. +func (f *FlagSet) UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string) { + f.VarP(newUintSliceValue(value, p), name, shorthand, usage) +} + +// UintSliceVar defines a uint[] flag with specified name, default value, and usage string. +// The argument p points to a uint[] variable in which to store the value of the flag. +func UintSliceVar(p *[]uint, name string, value []uint, usage string) { + CommandLine.VarP(newUintSliceValue(value, p), name, "", usage) +} + +// UintSliceVarP is like the UintSliceVar, but accepts a shorthand letter that can be used after a single dash. +func UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string) { + CommandLine.VarP(newUintSliceValue(value, p), name, shorthand, usage) +} + +// UintSlice defines a []uint flag with specified name, default value, and usage string. +// The return value is the address of a []uint variable that stores the value of the flag. +func (f *FlagSet) UintSlice(name string, value []uint, usage string) *[]uint { + p := []uint{} + f.UintSliceVarP(&p, name, "", value, usage) + return &p +} + +// UintSliceP is like UintSlice, but accepts a shorthand letter that can be used after a single dash. +func (f *FlagSet) UintSliceP(name, shorthand string, value []uint, usage string) *[]uint { + p := []uint{} + f.UintSliceVarP(&p, name, shorthand, value, usage) + return &p +} + +// UintSlice defines a []uint flag with specified name, default value, and usage string. +// The return value is the address of a []uint variable that stores the value of the flag. +func UintSlice(name string, value []uint, usage string) *[]uint { + return CommandLine.UintSliceP(name, "", value, usage) +} + +// UintSliceP is like UintSlice, but accepts a shorthand letter that can be used after a single dash. +func UintSliceP(name, shorthand string, value []uint, usage string) *[]uint { + return CommandLine.UintSliceP(name, shorthand, value, usage) +} diff --git a/cmd/bleve/vendor/manifest b/cmd/bleve/vendor/manifest index 8b7c9bc2..719bd7b0 100644 --- a/cmd/bleve/vendor/manifest +++ b/cmd/bleve/vendor/manifest @@ -13,7 +13,7 @@ "importpath": "github.com/spf13/cobra", "repository": "https://github.com/spf13/cobra", "vcs": "git", - "revision": "9c28e4bbd74e5c3ed7aacbc552b2cab7cfdfe744", + "revision": "b5d8e8f46a2f829f755b6e33b454e25c61c935e1", "branch": "master", "notests": true }, @@ -21,9 +21,9 @@ "importpath": "github.com/spf13/pflag", "repository": "https://github.com/spf13/pflag", "vcs": "git", - "revision": "7b17cc4658ef5ca157b986ea5c0b43af7938532b", + "revision": "9ff6c6923cfffbcd502984b8e0c80539a94968b7", "branch": "master", "notests": true } ] -} \ No newline at end of file +} From 9ca14d29f1475458e397585d92d6b3bbdd898f25 Mon Sep 17 00:00:00 2001 From: Marty Schoch Date: Wed, 15 Feb 2017 15:37:26 -0500 Subject: [PATCH 2/2] use cobra command Annotations to identify mutating commands some downstream applications need to know which sub-commands may alter the index. this new function allows them to be identified, while not prescribing any particular behavior. --- cmd/bleve/cmd/bulk.go | 3 +++ cmd/bleve/cmd/create.go | 3 +++ cmd/bleve/cmd/index.go | 3 +++ cmd/bleve/cmd/root.go | 17 +++++++++++++++++ 4 files changed, 26 insertions(+) diff --git a/cmd/bleve/cmd/bulk.go b/cmd/bleve/cmd/bulk.go index b526e7db..c3c90e73 100644 --- a/cmd/bleve/cmd/bulk.go +++ b/cmd/bleve/cmd/bulk.go @@ -31,6 +31,9 @@ var bulkCmd = &cobra.Command{ Use: "bulk [index path] [data paths ...]", Short: "bulk loads from newline delimited JSON files", Long: `The bulk command will perform batch loading of documents in one or more newline delimited JSON files.`, + Annotations: map[string]string{ + canMutateBleveIndex: "true", + }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) < 2 { return fmt.Errorf("must specify at least one path") diff --git a/cmd/bleve/cmd/create.go b/cmd/bleve/cmd/create.go index 696fc1ec..a5ef3d5b 100644 --- a/cmd/bleve/cmd/create.go +++ b/cmd/bleve/cmd/create.go @@ -31,6 +31,9 @@ var createCmd = &cobra.Command{ Use: "create [index path]", Short: "creates a new index", Long: `The create command will create a new empty index.`, + Annotations: map[string]string{ + canMutateBleveIndex: "true", + }, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { // override RootCmd version which opens existing index if len(args) < 1 { diff --git a/cmd/bleve/cmd/index.go b/cmd/bleve/cmd/index.go index 36b9c8c8..63ad5f3b 100644 --- a/cmd/bleve/cmd/index.go +++ b/cmd/bleve/cmd/index.go @@ -32,6 +32,9 @@ var indexCmd = &cobra.Command{ Use: "index [index path] [data paths ...]", Short: "adds the files to the index", Long: `The index command adds the specified files to the index.`, + Annotations: map[string]string{ + canMutateBleveIndex: "true", + }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) < 2 { return fmt.Errorf("must specify at least one path") diff --git a/cmd/bleve/cmd/root.go b/cmd/bleve/cmd/root.go index 09bf342b..f0bea29f 100644 --- a/cmd/bleve/cmd/root.go +++ b/cmd/bleve/cmd/root.go @@ -17,6 +17,7 @@ package cmd import ( "fmt" "os" + "strconv" "github.com/blevesearch/bleve" "github.com/spf13/cobra" @@ -26,6 +27,22 @@ var cfgFile string var idx bleve.Index +const canMutateBleveIndex = "canMutateBleveIndex" + +// CanMutateBleveIndex returns true if the command is capable +// of mutating the bleve index, or false if its operation is +// read-only +func CanMutateBleveIndex(c *cobra.Command) bool { + for k, v := range c.Annotations { + if k == canMutateBleveIndex { + if b, err := strconv.ParseBool(v); err == nil && b { + return true + } + } + } + return false +} + // RootCmd represents the base command when called without any subcommands var RootCmd = &cobra.Command{ Use: "bleve",