diff --git a/executable/executable.go b/executable/executable.go index 25ae3f5..23634e8 100644 --- a/executable/executable.go +++ b/executable/executable.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" "os" "path/filepath" "time" @@ -25,7 +24,9 @@ type Executable struct { // WorkingDir can be set before calling Start or Run to customize the working directory of the executable. WorkingDir string - StdinPipe io.WriteCloser + StdinPipe io.WriteCloser + StdoutBuffer *bytes.Buffer + StderrBuffer *bytes.Buffer // These are set & removed together atleastOneReadDone bool @@ -34,8 +35,6 @@ type Executable struct { stderrPipe io.ReadCloser stdoutBytes []byte stderrBytes []byte - stdoutBuffer *bytes.Buffer - stderrBuffer *bytes.Buffer stdoutLineWriter *linewriter.LineWriter stderrLineWriter *linewriter.LineWriter readDone chan bool @@ -64,7 +63,6 @@ func (w *loggerWriter) Write(bytes []byte) (n int, err error) { } func nullLogger(msg string) { - return } func (e *Executable) Clone() *Executable { @@ -91,7 +89,7 @@ func (e *Executable) isRunning() bool { } func (e *Executable) HasExited() bool { - return e.atleastOneReadDone == true + return e.atleastOneReadDone } // Start starts the specified command but does not wait for it to complete. @@ -138,7 +136,7 @@ func (e *Executable) Start(args ...string) error { return err } e.stdoutBytes = []byte{} - e.stdoutBuffer = bytes.NewBuffer(e.stdoutBytes) + e.StdoutBuffer = bytes.NewBuffer(e.stdoutBytes) e.stdoutLineWriter = linewriter.New(newLoggerWriter(e.loggerFunc), 500*time.Millisecond) // Setup stderr relay @@ -147,7 +145,7 @@ func (e *Executable) Start(args ...string) error { return err } e.stderrBytes = []byte{} - e.stderrBuffer = bytes.NewBuffer(e.stderrBytes) + e.StderrBuffer = bytes.NewBuffer(e.stderrBytes) e.stderrLineWriter = linewriter.New(newLoggerWriter(e.loggerFunc), 500*time.Millisecond) e.StdinPipe, err = cmd.StdinPipe() @@ -161,8 +159,8 @@ func (e *Executable) Start(args ...string) error { // At this point, it is safe to set e.cmd as cmd, if any of the above steps fail, we don't want to leave e.cmd in an inconsistent state e.cmd = cmd - e.setupIORelay(e.stdoutPipe, e.stdoutBuffer, e.stdoutLineWriter) - e.setupIORelay(e.stderrPipe, e.stderrBuffer, e.stderrLineWriter) + e.setupIORelay(e.stdoutPipe, e.StdoutBuffer, e.stdoutLineWriter) + e.setupIORelay(e.stderrPipe, e.StderrBuffer, e.stderrLineWriter) return nil } @@ -181,7 +179,7 @@ func (e *Executable) setupIORelay(source io.Reader, destination1 io.Writer, dest e.atleastOneReadDone = true e.readDone <- true - io.Copy(ioutil.Discard, source) // Let's drain the pipe in case any content is leftover + io.Copy(io.Discard, source) // Let's drain the pipe in case any content is leftover }() } @@ -218,8 +216,8 @@ func (e *Executable) Wait() (ExecutableResult, error) { e.cmd = nil e.stdoutPipe = nil e.stderrPipe = nil - e.stdoutBuffer = nil - e.stderrBuffer = nil + e.StdoutBuffer = nil + e.StderrBuffer = nil e.stdoutBytes = nil e.stderrBytes = nil e.stdoutLineWriter = nil @@ -245,8 +243,8 @@ func (e *Executable) Wait() (ExecutableResult, error) { e.stdoutLineWriter.Flush() e.stderrLineWriter.Flush() - stdout := e.stdoutBuffer.Bytes() - stderr := e.stderrBuffer.Bytes() + stdout := e.StdoutBuffer.Bytes() + stderr := e.StderrBuffer.Bytes() return ExecutableResult{ Stdout: stdout,