Skip to content

Commit

Permalink
fix: allows to capture cmd out/err
Browse files Browse the repository at this point in the history
OutOrStderr() introduces an inconsistent behavior.
This commit adds a feature to get the right behavior, without breaking changes

closes: #1708

Co-authored-by: Mislav Marohnić <git@mislav.net>
  • Loading branch information
tcarreira and mislav committed Jul 22, 2023
1 parent 60d056d commit d0b15ff
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions command.go
Expand Up @@ -188,6 +188,11 @@ type Command struct {
// errWriter is a writer defined by the user that replaces stderr
errWriter io.Writer

// outFallbackWriter is a writer defined by the user that is used if outWriter is nil
outFallbackWriter io.Writer
// errFallbackWriter is a writer defined by the user that is used if errWriter is nil
errFallbackWriter io.Writer

// FParseErrWhitelist flag parse errors to be ignored
FParseErrWhitelist FParseErrWhitelist

Expand Down Expand Up @@ -279,16 +284,30 @@ func (c *Command) SetOutput(output io.Writer) {

// SetOut sets the destination for usage messages.
// If newOut is nil, os.Stdout is used.
// Deprecated: Use SetOutFallback and/or SetErrFallback instead (see https://github.com/spf13/cobra/issues/1708)
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.
// Deprecated: Use SetOutFallback and/or SetErrFallback instead (see https://github.com/spf13/cobra/issues/1708)
func (c *Command) SetErr(newErr io.Writer) {
c.errWriter = newErr
}

// SetOutFallback sets the destination for usage messages when SetOut() was not used.
// If newOut is nil, os.Stdout is used.
func (c *Command) SetOutFallback(newOut io.Writer) {
c.outFallbackWriter = newOut
}

// SetErrFallback sets the destination for error messages when SetErr() was not used.
// If newErr is nil, os.Stderr is used.
func (c *Command) SetErrFallback(newErr io.Writer) {
c.errFallbackWriter = newErr
}

// SetIn sets the source for input data
// If newIn is nil, os.Stdin is used.
func (c *Command) SetIn(newIn io.Reader) {
Expand Down Expand Up @@ -363,9 +382,10 @@ func (c *Command) OutOrStdout() io.Writer {
return c.getOut(os.Stdout)
}

// OutOrStderr returns output to stderr
// OutOrStderr returns output to stderr.
// Deprecated: Use OutOrStdout or ErrOrStderr instead
func (c *Command) OutOrStderr() io.Writer {
return c.getOut(os.Stderr)
return c.getOutFallbackToErr(os.Stderr)
}

// ErrOrStderr returns output to stderr
Expand All @@ -382,6 +402,9 @@ func (c *Command) getOut(def io.Writer) io.Writer {
if c.outWriter != nil {
return c.outWriter
}
if c.outFallbackWriter != nil {
return c.outFallbackWriter
}
if c.HasParent() {
return c.parent.getOut(def)
}
Expand All @@ -392,12 +415,31 @@ func (c *Command) getErr(def io.Writer) io.Writer {
if c.errWriter != nil {
return c.errWriter
}
if c.errFallbackWriter != nil {
return c.errFallbackWriter
}
if c.HasParent() {
return c.parent.getErr(def)
}
return def
}

// getOutFallbackToErr should only be used inside OutOrStderr.
// Deprecated: this function exists to allow for backwards compatibility only
// (see https://github.com/spf13/cobra/issues/1708)
func (c *Command) getOutFallbackToErr(def io.Writer) io.Writer {
if c.outWriter != nil {
return c.outWriter
}
if c.errFallbackWriter != nil {
return c.errFallbackWriter
}
if c.HasParent() {
return c.parent.getOutFallbackToErr(def)
}
return def
}

func (c *Command) getIn(def io.Reader) io.Reader {
if c.inReader != nil {
return c.inReader
Expand Down

0 comments on commit d0b15ff

Please sign in to comment.