refactor: rename out/err stream vars/funcs

This commit is contained in:
Tiago Carreira 2023-10-02 01:37:31 +01:00
parent 5a0bfd38d5
commit 2f70b7caa3
No known key found for this signature in database
GPG key ID: 3F13CBCC0395F78C

View file

@ -186,15 +186,17 @@ type Command struct {
// inReader is a reader defined by the user that replaces stdin // inReader is a reader defined by the user that replaces stdin
inReader io.Reader inReader io.Reader
// outWriter is a writer defined by the user that replaces stdout // legacyOutWriter is a writer defined by the user that replaces stdout.
outWriter io.Writer // Deprecated: use outStreamWriter instead (see https://github.com/spf13/cobra/issues/1708)
// errWriter is a writer defined by the user that replaces stderr legacyOutWriter io.Writer
errWriter io.Writer // legacyErrWriter is a writer defined by the user that replaces stderr.
// Deprecated: use errStreamWriter instead (see https://github.com/spf13/cobra/issues/1708)
legacyErrWriter io.Writer
// outFallbackWriter is a writer defined by the user that is used if outWriter is nil // outStreamWriter is a writer defined by the user that replaces stdout
outFallbackWriter io.Writer outStreamWriter io.Writer
// errFallbackWriter is a writer defined by the user that is used if errWriter is nil // errStreamWriter is a writer defined by the user that replaces stderr
errFallbackWriter io.Writer errStreamWriter io.Writer
// FParseErrWhitelist flag parse errors to be ignored // FParseErrWhitelist flag parse errors to be ignored
FParseErrWhitelist FParseErrWhitelist FParseErrWhitelist FParseErrWhitelist
@ -281,34 +283,36 @@ func (c *Command) SetArgs(a []string) {
// If output is nil, os.Stderr is used. // If output is nil, os.Stderr is used.
// Deprecated: Use SetOut and/or SetErr instead // Deprecated: Use SetOut and/or SetErr instead
func (c *Command) SetOutput(output io.Writer) { func (c *Command) SetOutput(output io.Writer) {
c.outWriter = output c.legacyOutWriter = output
c.errWriter = output c.legacyErrWriter = output
} }
// SetOut sets the destination for usage messages. // SetOut sets the destination for usage messages.
// If newOut is nil, os.Stdout is used. // If newOut is nil, os.Stdout is used.
// Deprecated: Use SetOutFallback and/or SetErrFallback instead (see https://github.com/spf13/cobra/issues/1708) // Deprecated: Use SetOutFallback and/or SetErrFallback instead (see https://github.com/spf13/cobra/issues/1708)
func (c *Command) SetOut(newOut io.Writer) { func (c *Command) SetOut(newOut io.Writer) {
c.outWriter = newOut c.legacyOutWriter = newOut
} }
// SetErr sets the destination for error messages. // SetErr sets the destination for error messages.
// If newErr is nil, os.Stderr is used. // If newErr is nil, os.Stderr is used.
// Deprecated: Use SetOutFallback and/or SetErrFallback instead (see https://github.com/spf13/cobra/issues/1708) // Deprecated: Use SetOutFallback and/or SetErrFallback instead (see https://github.com/spf13/cobra/issues/1708)
func (c *Command) SetErr(newErr io.Writer) { func (c *Command) SetErr(newErr io.Writer) {
c.errWriter = newErr c.legacyErrWriter = newErr
} }
// SetOutFallback sets the destination for usage messages when SetOut() was not used. // SetOutStream sets the destination for usage messages.
// It includes (at least): --help, --version, completion.
// If newOut is nil, os.Stdout is used. // If newOut is nil, os.Stdout is used.
func (c *Command) SetOutFallback(newOut io.Writer) { func (c *Command) SetOutStream(newOut io.Writer) {
c.outFallbackWriter = newOut c.outStreamWriter = newOut
} }
// SetErrFallback sets the destination for error messages when SetErr() was not used. // SetErrStream sets the destination for error messages.
// It includes (at least): errors, usage, deprecations msgs, unknowns cmds/flags/topics msgs, DebugFlags.
// If newErr is nil, os.Stderr is used. // If newErr is nil, os.Stderr is used.
func (c *Command) SetErrFallback(newErr io.Writer) { func (c *Command) SetErrStream(newErr io.Writer) {
c.errFallbackWriter = newErr c.errStreamWriter = newErr
} }
// SetIn sets the source for input data // SetIn sets the source for input data
@ -407,11 +411,11 @@ func (c *Command) InOrStdin() io.Reader {
} }
func (c *Command) getOut(def io.Writer) io.Writer { func (c *Command) getOut(def io.Writer) io.Writer {
if c.outWriter != nil { if c.legacyOutWriter != nil {
return c.outWriter return c.legacyOutWriter
} }
if c.outFallbackWriter != nil { if c.outStreamWriter != nil {
return c.outFallbackWriter return c.outStreamWriter
} }
if c.HasParent() { if c.HasParent() {
return c.parent.getOut(def) return c.parent.getOut(def)
@ -420,11 +424,11 @@ func (c *Command) getOut(def io.Writer) io.Writer {
} }
func (c *Command) getErr(def io.Writer) io.Writer { func (c *Command) getErr(def io.Writer) io.Writer {
if c.errWriter != nil { if c.legacyErrWriter != nil {
return c.errWriter return c.legacyErrWriter
} }
if c.errFallbackWriter != nil { if c.errStreamWriter != nil {
return c.errFallbackWriter return c.errStreamWriter
} }
if c.HasParent() { if c.HasParent() {
return c.parent.getErr(def) return c.parent.getErr(def)
@ -436,11 +440,11 @@ func (c *Command) getErr(def io.Writer) io.Writer {
// Deprecated: this function exists to allow for backwards compatibility only // Deprecated: this function exists to allow for backwards compatibility only
// (see https://github.com/spf13/cobra/issues/1708) // (see https://github.com/spf13/cobra/issues/1708)
func (c *Command) getOutFallbackToErr(def io.Writer) io.Writer { func (c *Command) getOutFallbackToErr(def io.Writer) io.Writer {
if c.outWriter != nil { if c.legacyOutWriter != nil {
return c.outWriter return c.legacyOutWriter
} }
if c.errFallbackWriter != nil { if c.errStreamWriter != nil {
return c.errFallbackWriter return c.errStreamWriter
} }
if c.HasParent() { if c.HasParent() {
return c.parent.getOutFallbackToErr(def) return c.parent.getOutFallbackToErr(def)
@ -515,18 +519,18 @@ func (c *Command) Help() error {
// UsageString returns usage string. // UsageString returns usage string.
func (c *Command) UsageString() string { func (c *Command) UsageString() string {
// Storing normal writers // Storing normal writers
tmpOutput := c.outWriter tmpOutput := c.legacyOutWriter
tmpErr := c.errWriter tmpErr := c.legacyErrWriter
bb := new(bytes.Buffer) bb := new(bytes.Buffer)
c.outWriter = bb c.legacyOutWriter = bb
c.errWriter = bb c.legacyErrWriter = bb
CheckErr(c.Usage()) CheckErr(c.Usage())
// Setting things back to normal // Setting things back to normal
c.outWriter = tmpOutput c.legacyOutWriter = tmpOutput
c.errWriter = tmpErr c.legacyErrWriter = tmpErr
return bb.String() return bb.String()
} }