Expose OutOrStdout and OutOrStderr and don't make assumptions if output is not set
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Fabiano Franz · committed by Fabiano Franz · parent a272c3c · Browse files · 2 files·+10 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+10 added−10 removed
- other:+10 added−10 removed
Provenance
- Commit
20217d8- Tree
bd35716- Author
Fabiano Franz <ffranz@redhat.com>- Committer
Fabiano Franz <ffranz@redhat.com>- Parent
a272c3c
2 files changed+10 added−10 removed
modified:cobra_test.go+1 added-1 removed
644 unmodified lines645 646 second := &Command{647 Use: "second",648 Run: func(cmd *Command, args []string) {649- fmt.Fprintf(cmd.getOutOrStdout(), "%v", args)649+ fmt.Fprintf(cmd.OutOrStdout(), "%v", args)650 },651 }652 first.AddCommand(second)653 535 unmodified linesmodified:command.go+9 added-9 removed
108 unmodified lines109 110 flagErrorBuf *bytes.Buffer111 112 args []string // actual args parsed from flags113- output *io.Writer // nil means stderr; use Out() method instead113+ output *io.Writer // out writer if set in SetOutput(w)114 usageFunc func(*Command) error // Usage can be defined by application115 usageTemplate string // Can be defined by Application116 helpTemplate string // Can be defined by Application117 helpFunc func(*Command, []string) // Help can be defined by application57 unmodified lines175 command.SetGlobalNormalizationFunc(n)176 }177 }178 179-func (c *Command) getOutOrStdout() io.Writer {179+func (c *Command) OutOrStdout() io.Writer {180 return c.getOut(os.Stdout)181 }182 183-func (c *Command) getOutOrStderr() io.Writer {183+func (c *Command) OutOrStderr() io.Writer {184 return c.getOut(os.Stderr)185 }186 187 func (c *Command) getOut(def io.Writer) io.Writer {47 unmodified lines235 // Used when a user provides invalid input236 // Can be defined by user by overriding UsageFunc237 func (c *Command) Usage() error {238 c.mergePersistentFlags()239- err := tmpl(c.getOutOrStderr(), c.UsageTemplate(), c)239+ err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)240 return err241 }242 243 // Output the help for the command244 // Used when a user calls help [command]245 // by the default HelpFunc in the commander246 func (c *Command) Help() error {247 c.mergePersistentFlags()248- err := tmpl(c.getOutOrStdout(), c.HelpTemplate(), c)248+ err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)249 return err250 }251 252 func (c *Command) UsageString() string {579 unmodified lines832 }833 }834 }835 836-// Print is a convenience method to Print to the defined output836+// Print is a convenience method to Print to the defined output, fallback to Stderr if not set837 func (c *Command) Print(i ...interface{}) {838- fmt.Fprint(c.getOutOrStderr(), i...)838+ fmt.Fprint(c.OutOrStderr(), i...)839 }840 841-// Println is a convenience method to Println to the defined output841+// Println is a convenience method to Println to the defined output, fallback to Stderr if not set842 func (c *Command) Println(i ...interface{}) {843 str := fmt.Sprintln(i...)844 c.Print(str)845 }846 847-// Printf is a convenience method to Printf to the defined output847+// Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set848 func (c *Command) Printf(format string, i ...interface{}) {849 str := fmt.Sprintf(format, i...)850 c.Print(str)851 }407 unmodified lines