Skip to content

Commit

Permalink
feat: make stdout, stderr buffers public
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-gang committed May 15, 2024
1 parent b170a07 commit a4b7ced
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions executable/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -64,7 +63,6 @@ func (w *loggerWriter) Write(bytes []byte) (n int, err error) {
}

func nullLogger(msg string) {
return
}

func (e *Executable) Clone() *Executable {
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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
}
Expand All @@ -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
}()
}

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit a4b7ced

Please sign in to comment.