Extending redirection to stdout, stderr, stdin
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Juan Leni · committed by Steve Francia · parent 5f23f55 · Browse files · 1 file·+61 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+61 added−8 removed
- other:+61 added−8 removed
Provenance
- Commit
4a716d1- Tree
415f797- Author
Juan Leni <lenijuan@gmail.com>- Committer
Steve Francia <steve.francia@gmail.com>- Parent
5f23f55
1 file changed+61 added−8 removed
modified:command.go+61 added-8 removed
175 unmodified lines176 // globNormFunc is the global normalization function177 // that we can use on every pflag set and children commands178 globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName179 180- // output is an output writer defined by user.181- output io.Writer180 // usageFunc is usage func defined by user.181 usageFunc func(*Command) error182 // usageTemplate is usage template defined by user.183 usageTemplate string8 unmodified lines192 // cobra uses default help command.193 helpCommand *Command194 // versionTemplate is the version template defined by user.195 versionTemplate string196+197+ // inReader is a reader defined by the user that replaces stdin198+ inReader io.Reader199+ // outWriter is a writer defined by the user that replaces stdout200+ outWriter io.Writer201+ // errWriter is a writer defined by the user that replaces stderr202+ errWriter io.Writer203 }204 205 // SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden206 // particularly useful when testing.3 unmodified lines210 211 // SetOutput sets the destination for usage and error messages.212 // If output is nil, os.Stderr is used.213 func (c *Command) SetOutput(output io.Writer) {209- c.output = output214+ c.outWriter = output215 }216 217+// SetOut sets the destination for usage messages.218+// If newOut is nil, os.Stdout is used.219+func (c *Command) SetOut(newOut io.Writer) {220+ c.outWriter = newOut221+}222+223+// SetErr sets the destination for error messages.224+// If newErr is nil, os.Stderr is used.225+func (c *Command) SetErr(newErr io.Writer) {226+ c.errWriter = newErr227+}228+229+// SetOut sets the source for input data230+// If newIn is nil, os.Stdin is used.231+func (c *Command) SetIn(newIn io.Reader) {232+ c.inReader = newIn233+}234+235 // SetUsageFunc sets usage function. Usage can be defined by application.236 func (c *Command) SetUsageFunc(f func(*Command) error) {237 c.usageFunc = f238 }50 unmodified lines289 func (c *Command) OutOrStderr() io.Writer {290 return c.getOut(os.Stderr)291 }292 293+// ErrOrStderr returns output to stderr294+func (c *Command) ErrOrStderr() io.Writer {295+ return c.getErr(os.Stderr)296+}297+298+// ErrOrStderr returns output to stderr299+func (c *Command) InOrStdin() io.Reader {300+ return c.getIn(os.Stdin)301+}302+303 func (c *Command) getOut(def io.Writer) io.Writer {271- if c.output != nil {272- return c.output304+ if c.outWriter != nil {305+ return c.outWriter306 }307 if c.HasParent() {308 return c.parent.getOut(def)309 }310 return def311 }312 313+func (c *Command) getErr(def io.Writer) io.Writer {314+ if c.errWriter != nil {315+ return c.errWriter316+ }317+ if c.HasParent() {318+ return c.parent.getErr(def)319+ }320+ return def321+}322+323+func (c *Command) getIn(def io.Reader) io.Reader {324+ if c.inReader != nil {325+ return c.inReader326+ }327+ if c.HasParent() {328+ return c.parent.getIn(def)329+ }330+ return def331+}332+333 // UsageFunc returns either the function set by SetUsageFunc for this command334 // or a parent, or it returns a default usage function.335 func (c *Command) UsageFunc() (f func(*Command) error) {336 if c.usageFunc != nil {46 unmodified lines383 }384 385 // UsageString return usage string.386 func (c *Command) UsageString() string {334- tmpOutput := c.output387+ tmpOutput := c.outWriter388 bb := new(bytes.Buffer)336- c.SetOutput(bb)389+ c.outWriter = bb390 c.Usage()338- c.output = tmpOutput391+ c.outWriter = tmpOutput392 return bb.String()393 }394 395 // FlagErrorFunc returns either the function set by SetFlagErrorFunc for this1173 unmodified lines