fix: allows to capture cmd out/err

OutOrStderr() introduces an inconsistent behavior.
This commit adds a feature to get the right behavior, without breaking changes

closes: https://github.com/spf13/cobra/issues/1708

Co-authored-by: Mislav Marohnić <git@mislav.net>
This commit is contained in:
Tiago Carreira 2023-07-22 22:59:47 +01:00
parent bd4d1655f6
commit 5a0bfd38d5
No known key found for this signature in database
GPG Key ID: 3F13CBCC0395F78C
1 changed files with 44 additions and 2 deletions

View File

@ -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
@ -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) {
@ -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
@ -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)
}
@ -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