Update module spf13/cobra to v0.0.7 (#271)
Update module spf13/cobra to v0.0.7 Reviewed-on: https://kolaente.dev/vikunja/api/pulls/271
This commit is contained in:
144
vendor/github.com/spf13/cobra/command.go
generated
vendored
144
vendor/github.com/spf13/cobra/command.go
generated
vendored
@ -17,6 +17,7 @@ package cobra
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@ -80,7 +81,8 @@ type Command struct {
|
||||
|
||||
// Version defines the version for this command. If this value is non-empty and the command does not
|
||||
// define a "version" flag, a "version" boolean flag will be added to the command and, if specified,
|
||||
// will print content of the "Version" variable.
|
||||
// will print content of the "Version" variable. A shorthand "v" flag will also be added if the
|
||||
// command does not define one.
|
||||
Version string
|
||||
|
||||
// The *Run functions are executed in the following order:
|
||||
@ -140,9 +142,11 @@ type Command struct {
|
||||
// TraverseChildren parses flags on all parents before executing child command.
|
||||
TraverseChildren bool
|
||||
|
||||
//FParseErrWhitelist flag parse errors to be ignored
|
||||
// FParseErrWhitelist flag parse errors to be ignored
|
||||
FParseErrWhitelist FParseErrWhitelist
|
||||
|
||||
ctx context.Context
|
||||
|
||||
// commands is the list of commands supported by this program.
|
||||
commands []*Command
|
||||
// parent is a parent command for this command.
|
||||
@ -177,8 +181,6 @@ type Command struct {
|
||||
// that we can use on every pflag set and children commands
|
||||
globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName
|
||||
|
||||
// output is an output writer defined by user.
|
||||
output io.Writer
|
||||
// usageFunc is usage func defined by user.
|
||||
usageFunc func(*Command) error
|
||||
// usageTemplate is usage template defined by user.
|
||||
@ -195,6 +197,19 @@ type Command struct {
|
||||
helpCommand *Command
|
||||
// versionTemplate is the version template defined by user.
|
||||
versionTemplate string
|
||||
|
||||
// inReader is a reader defined by the user that replaces stdin
|
||||
inReader io.Reader
|
||||
// outWriter is a writer defined by the user that replaces stdout
|
||||
outWriter io.Writer
|
||||
// errWriter is a writer defined by the user that replaces stderr
|
||||
errWriter io.Writer
|
||||
}
|
||||
|
||||
// Context returns underlying command context. If command wasn't
|
||||
// executed with ExecuteContext Context returns Background context.
|
||||
func (c *Command) Context() context.Context {
|
||||
return c.ctx
|
||||
}
|
||||
|
||||
// SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden
|
||||
@ -205,8 +220,28 @@ func (c *Command) SetArgs(a []string) {
|
||||
|
||||
// SetOutput sets the destination for usage and error messages.
|
||||
// If output is nil, os.Stderr is used.
|
||||
// Deprecated: Use SetOut and/or SetErr instead
|
||||
func (c *Command) SetOutput(output io.Writer) {
|
||||
c.output = output
|
||||
c.outWriter = output
|
||||
c.errWriter = output
|
||||
}
|
||||
|
||||
// SetOut sets the destination for usage messages.
|
||||
// If newOut is nil, os.Stdout is used.
|
||||
func (c *Command) SetOut(newOut io.Writer) {
|
||||
c.outWriter = newOut
|
||||
}
|
||||
|
||||
// SetErr sets the destination for error messages.
|
||||
// If newErr is nil, os.Stderr is used.
|
||||
func (c *Command) SetErr(newErr io.Writer) {
|
||||
c.errWriter = newErr
|
||||
}
|
||||
|
||||
// SetIn sets the source for input data
|
||||
// If newIn is nil, os.Stdin is used.
|
||||
func (c *Command) SetIn(newIn io.Reader) {
|
||||
c.inReader = newIn
|
||||
}
|
||||
|
||||
// SetUsageFunc sets usage function. Usage can be defined by application.
|
||||
@ -267,9 +302,19 @@ func (c *Command) OutOrStderr() io.Writer {
|
||||
return c.getOut(os.Stderr)
|
||||
}
|
||||
|
||||
// ErrOrStderr returns output to stderr
|
||||
func (c *Command) ErrOrStderr() io.Writer {
|
||||
return c.getErr(os.Stderr)
|
||||
}
|
||||
|
||||
// InOrStdin returns input to stdin
|
||||
func (c *Command) InOrStdin() io.Reader {
|
||||
return c.getIn(os.Stdin)
|
||||
}
|
||||
|
||||
func (c *Command) getOut(def io.Writer) io.Writer {
|
||||
if c.output != nil {
|
||||
return c.output
|
||||
if c.outWriter != nil {
|
||||
return c.outWriter
|
||||
}
|
||||
if c.HasParent() {
|
||||
return c.parent.getOut(def)
|
||||
@ -277,6 +322,26 @@ func (c *Command) getOut(def io.Writer) io.Writer {
|
||||
return def
|
||||
}
|
||||
|
||||
func (c *Command) getErr(def io.Writer) io.Writer {
|
||||
if c.errWriter != nil {
|
||||
return c.errWriter
|
||||
}
|
||||
if c.HasParent() {
|
||||
return c.parent.getErr(def)
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func (c *Command) getIn(def io.Reader) io.Reader {
|
||||
if c.inReader != nil {
|
||||
return c.inReader
|
||||
}
|
||||
if c.HasParent() {
|
||||
return c.parent.getIn(def)
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
// UsageFunc returns either the function set by SetUsageFunc for this command
|
||||
// or a parent, or it returns a default usage function.
|
||||
func (c *Command) UsageFunc() (f func(*Command) error) {
|
||||
@ -314,6 +379,8 @@ func (c *Command) HelpFunc() func(*Command, []string) {
|
||||
}
|
||||
return func(c *Command, a []string) {
|
||||
c.mergePersistentFlags()
|
||||
// The help should be sent to stdout
|
||||
// See https://github.com/spf13/cobra/issues/1002
|
||||
err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)
|
||||
if err != nil {
|
||||
c.Println(err)
|
||||
@ -329,13 +396,22 @@ func (c *Command) Help() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// UsageString return usage string.
|
||||
// UsageString returns usage string.
|
||||
func (c *Command) UsageString() string {
|
||||
tmpOutput := c.output
|
||||
// Storing normal writers
|
||||
tmpOutput := c.outWriter
|
||||
tmpErr := c.errWriter
|
||||
|
||||
bb := new(bytes.Buffer)
|
||||
c.SetOutput(bb)
|
||||
c.outWriter = bb
|
||||
c.errWriter = bb
|
||||
|
||||
c.Usage()
|
||||
c.output = tmpOutput
|
||||
|
||||
// Setting things back to normal
|
||||
c.outWriter = tmpOutput
|
||||
c.errWriter = tmpErr
|
||||
|
||||
return bb.String()
|
||||
}
|
||||
|
||||
@ -793,6 +869,13 @@ func (c *Command) preRun() {
|
||||
}
|
||||
}
|
||||
|
||||
// ExecuteContext is the same as Execute(), but sets the ctx on the command.
|
||||
// Retrieve ctx by calling cmd.Context() inside your *Run lifecycle functions.
|
||||
func (c *Command) ExecuteContext(ctx context.Context) error {
|
||||
c.ctx = ctx
|
||||
return c.Execute()
|
||||
}
|
||||
|
||||
// Execute uses the args (os.Args[1:] by default)
|
||||
// and run through the command tree finding appropriate matches
|
||||
// for commands and then corresponding flags.
|
||||
@ -803,6 +886,10 @@ func (c *Command) Execute() error {
|
||||
|
||||
// ExecuteC executes the command.
|
||||
func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||
if c.ctx == nil {
|
||||
c.ctx = context.Background()
|
||||
}
|
||||
|
||||
// Regardless of what command execute is called on, run on Root only
|
||||
if c.HasParent() {
|
||||
return c.Root().ExecuteC()
|
||||
@ -817,13 +904,11 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||
// overriding
|
||||
c.InitDefaultHelpCmd()
|
||||
|
||||
var args []string
|
||||
args := c.args
|
||||
|
||||
// Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155
|
||||
if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" {
|
||||
args = os.Args[1:]
|
||||
} else {
|
||||
args = c.args
|
||||
}
|
||||
|
||||
var flags []string
|
||||
@ -849,6 +934,12 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||
cmd.commandCalledAs.name = cmd.Name()
|
||||
}
|
||||
|
||||
// We have to pass global context to children command
|
||||
// if context is present on the parent command.
|
||||
if cmd.ctx == nil {
|
||||
cmd.ctx = c.ctx
|
||||
}
|
||||
|
||||
err = cmd.execute(flags)
|
||||
if err != nil {
|
||||
// Always show help if requested, even if SilenceErrors is in
|
||||
@ -932,7 +1023,11 @@ func (c *Command) InitDefaultVersionFlag() {
|
||||
} else {
|
||||
usage += c.Name()
|
||||
}
|
||||
c.Flags().Bool("version", false, usage)
|
||||
if c.Flags().ShorthandLookup("v") == nil {
|
||||
c.Flags().BoolP("version", "v", false, usage)
|
||||
} else {
|
||||
c.Flags().Bool("version", false, usage)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1070,6 +1165,21 @@ func (c *Command) Printf(format string, i ...interface{}) {
|
||||
c.Print(fmt.Sprintf(format, i...))
|
||||
}
|
||||
|
||||
// PrintErr is a convenience method to Print to the defined Err output, fallback to Stderr if not set.
|
||||
func (c *Command) PrintErr(i ...interface{}) {
|
||||
fmt.Fprint(c.ErrOrStderr(), i...)
|
||||
}
|
||||
|
||||
// PrintErrln is a convenience method to Println to the defined Err output, fallback to Stderr if not set.
|
||||
func (c *Command) PrintErrln(i ...interface{}) {
|
||||
c.Print(fmt.Sprintln(i...))
|
||||
}
|
||||
|
||||
// PrintErrf is a convenience method to Printf to the defined Err output, fallback to Stderr if not set.
|
||||
func (c *Command) PrintErrf(format string, i ...interface{}) {
|
||||
c.Print(fmt.Sprintf(format, i...))
|
||||
}
|
||||
|
||||
// CommandPath returns the full path to this command.
|
||||
func (c *Command) CommandPath() string {
|
||||
if c.HasParent() {
|
||||
@ -1335,7 +1445,7 @@ func (c *Command) LocalFlags() *flag.FlagSet {
|
||||
return c.lflags
|
||||
}
|
||||
|
||||
// InheritedFlags returns all flags which were inherited from parents commands.
|
||||
// InheritedFlags returns all flags which were inherited from parent commands.
|
||||
func (c *Command) InheritedFlags() *flag.FlagSet {
|
||||
c.mergePersistentFlags()
|
||||
|
||||
@ -1470,7 +1580,7 @@ func (c *Command) ParseFlags(args []string) error {
|
||||
beforeErrorBufLen := c.flagErrorBuf.Len()
|
||||
c.mergePersistentFlags()
|
||||
|
||||
//do it here after merging all flags and just before parse
|
||||
// do it here after merging all flags and just before parse
|
||||
c.Flags().ParseErrorsWhitelist = flag.ParseErrorsWhitelist(c.FParseErrWhitelist)
|
||||
|
||||
err := c.Flags().Parse(args)
|
||||
|
Reference in New Issue
Block a user