Skip to content

Commit 4a716d1

Browse files
jlenispf13
authored andcommitted
Extending redirection to stdout, stderr, stdin
1 parent 5f23f55 commit 4a716d1

File tree

1 file changed

+61
-8
lines changed

1 file changed

+61
-8
lines changed

command.go

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,6 @@ type Command struct {
177177
// that we can use on every pflag set and children commands
178178
globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName
179179

180-
// output is an output writer defined by user.
181-
output io.Writer
182180
// usageFunc is usage func defined by user.
183181
usageFunc func(*Command) error
184182
// usageTemplate is usage template defined by user.
@@ -195,6 +193,13 @@ type Command struct {
195193
helpCommand *Command
196194
// versionTemplate is the version template defined by user.
197195
versionTemplate string
196+
197+
// inReader is a reader defined by the user that replaces stdin
198+
inReader io.Reader
199+
// outWriter is a writer defined by the user that replaces stdout
200+
outWriter io.Writer
201+
// errWriter is a writer defined by the user that replaces stderr
202+
errWriter io.Writer
198203
}
199204

200205
// SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden
@@ -206,7 +211,25 @@ func (c *Command) SetArgs(a []string) {
206211
// SetOutput sets the destination for usage and error messages.
207212
// If output is nil, os.Stderr is used.
208213
func (c *Command) SetOutput(output io.Writer) {
209-
c.output = output
214+
c.outWriter = output
215+
}
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 = newOut
221+
}
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 = newErr
227+
}
228+
229+
// SetOut sets the source for input data
230+
// If newIn is nil, os.Stdin is used.
231+
func (c *Command) SetIn(newIn io.Reader) {
232+
c.inReader = newIn
210233
}
211234

212235
// SetUsageFunc sets usage function. Usage can be defined by application.
@@ -267,16 +290,46 @@ func (c *Command) OutOrStderr() io.Writer {
267290
return c.getOut(os.Stderr)
268291
}
269292

293+
// ErrOrStderr returns output to stderr
294+
func (c *Command) ErrOrStderr() io.Writer {
295+
return c.getErr(os.Stderr)
296+
}
297+
298+
// ErrOrStderr returns output to stderr
299+
func (c *Command) InOrStdin() io.Reader {
300+
return c.getIn(os.Stdin)
301+
}
302+
270303
func (c *Command) getOut(def io.Writer) io.Writer {
271-
if c.output != nil {
272-
return c.output
304+
if c.outWriter != nil {
305+
return c.outWriter
273306
}
274307
if c.HasParent() {
275308
return c.parent.getOut(def)
276309
}
277310
return def
278311
}
279312

313+
func (c *Command) getErr(def io.Writer) io.Writer {
314+
if c.errWriter != nil {
315+
return c.errWriter
316+
}
317+
if c.HasParent() {
318+
return c.parent.getErr(def)
319+
}
320+
return def
321+
}
322+
323+
func (c *Command) getIn(def io.Reader) io.Reader {
324+
if c.inReader != nil {
325+
return c.inReader
326+
}
327+
if c.HasParent() {
328+
return c.parent.getIn(def)
329+
}
330+
return def
331+
}
332+
280333
// UsageFunc returns either the function set by SetUsageFunc for this command
281334
// or a parent, or it returns a default usage function.
282335
func (c *Command) UsageFunc() (f func(*Command) error) {
@@ -331,11 +384,11 @@ func (c *Command) Help() error {
331384

332385
// UsageString return usage string.
333386
func (c *Command) UsageString() string {
334-
tmpOutput := c.output
387+
tmpOutput := c.outWriter
335388
bb := new(bytes.Buffer)
336-
c.SetOutput(bb)
389+
c.outWriter = bb
337390
c.Usage()
338-
c.output = tmpOutput
391+
c.outWriter = tmpOutput
339392
return bb.String()
340393
}
341394

0 commit comments

Comments
 (0)