diff --git a/common/io.go b/common/io.go index 0c33b0756..378c19fc6 100644 --- a/common/io.go +++ b/common/io.go @@ -1,6 +1,8 @@ package common import ( + "bytes" + "errors" "io" ) @@ -22,3 +24,52 @@ func (pr *PrefixedReader) Read(p []byte) (n int, err error) { return pr.reader.Read(p) } } + +// NOTE: Not goroutine safe +type BufferCloser struct { + bytes.Buffer + Closed bool +} + +func NewBufferCloser(buf []byte) *BufferCloser { + return &BufferCloser{ + *bytes.NewBuffer(buf), + false, + } +} + +func (bc *BufferCloser) Close() error { + if bc.Closed { + return errors.New("BufferCloser already closed") + } + bc.Closed = true + return nil +} + +func (bc *BufferCloser) Write(p []byte) (n int, err error) { + if bc.Closed { + return 0, errors.New("Cannot write to closed BufferCloser") + } + return bc.Buffer.Write(p) +} + +func (bc *BufferCloser) WriteByte(c byte) error { + if bc.Closed { + return errors.New("Cannot write to closed BufferCloser") + } + return bc.Buffer.WriteByte(c) +} + +func (bc *BufferCloser) WriteRune(r rune) (n int, err error) { + if bc.Closed { + return 0, errors.New("Cannot write to closed BufferCloser") + } + return bc.Buffer.WriteRune(r) +} + +func (bc *BufferCloser) WriteString(s string) (n int, err error) { + if bc.Closed { + return 0, errors.New("Cannot write to closed BufferCloser") + } + return bc.Buffer.WriteString(s) +} diff --git a/process/process.go b/process/process.go index 03b0a46c1..5c27f14eb 100644 --- a/process/process.go +++ b/process/process.go @@ -52,6 +52,7 @@ func Create(label string, execPath string, args []string, inFile io.Reader, outF proc.ExitState = exitError.ProcessState } } + proc.ExitState = proc.Cmd.ProcessState proc.EndTime = time.Now() // TODO make this goroutine-safe err = proc.OutputFile.Close() if err != nil {