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 Sep 11, 2023
1 parent 0c72800 commit e35bf96
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,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 @@ -282,16 +287,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 @@ -371,9 +390,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 @@ -390,6 +410,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 @@ -400,12 +423,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 e35bf96

Please sign in to comment.