Skip to content

Commit

Permalink
trial: a hack to intercept Stdou/Stderr by replacing the variables in…
Browse files Browse the repository at this point in the history
… os.*
  • Loading branch information
mohammed90 committed Nov 22, 2019
1 parent 25062de commit 53fe7fd
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions internal/remote/output_interceptor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package remote

import (
"bytes"
"errors"
"io"
"io/ioutil"
Expand All @@ -22,22 +23,42 @@ func NewOutputInterceptor() OutputInterceptor {
}

type outputInterceptor struct {
origStdout *os.File
origStderr *os.File
readSources [2]io.ReadCloser // stores the reader pipes form os.Pipe() for closure.
streamTarget *os.File
combinedReader io.Reader
intercepting bool
tailer io.Reader
buffer *bytes.Buffer
}

func (interceptor *outputInterceptor) StartInterceptingOutput() error {
if interceptor.intercepting {
return errors.New("Already intercepting output!")
}
interceptor.origStdout = os.Stdout
interceptor.origStderr = os.Stderr
stdoutRead, stdoutWrite, err := os.Pipe()
if err != nil {
return err
}
stderrRead, stderrWrite, err := os.Pipe()
if err != nil {
return err
}

os.Stdout = stdoutWrite
os.Stderr = stderrWrite

interceptor.intercepting = true
interceptor.combinedReader = io.MultiReader(os.Stdout, os.Stderr)

if interceptor.streamTarget != nil {
interceptor.tailer = io.TeeReader(interceptor.combinedReader, interceptor.streamTarget)
}
interceptor.readSources = [2]io.ReadCloser{stderrRead, stderrRead}
interceptor.combinedReader = io.TeeReader(io.MultiReader(stderrRead, stdoutRead), interceptor.buffer)

// if interceptor.streamTarget != nil {
// interceptor.tailer = io.TeeReader(interceptor.combinedReader, interceptor.streamTarget)
// }

return nil
}
Expand All @@ -47,14 +68,28 @@ func (interceptor *outputInterceptor) StopInterceptingAndReturnOutput() (string,
return "", errors.New("Not intercepting output!")
}

output, err := ioutil.ReadAll(interceptor.combinedReader)
output, err := ioutil.ReadAll(interceptor.buffer)

interceptor.intercepting = false
currStdout := os.Stdout
currStdout.Close()
currStderr := os.Stderr
currStderr.Close()

os.Stdout = interceptor.origStdout
os.Stderr = interceptor.origStderr

if interceptor.streamTarget != nil {
interceptor.streamTarget.Sync()
for _, r := range interceptor.readSources {
if closerErr := r.Close(); closerErr != nil {
err = closerErr
}
}

interceptor.intercepting = false

// if interceptor.streamTarget != nil {
// interceptor.streamTarget.Sync()
// }

return string(output), err
}

Expand Down

0 comments on commit 53fe7fd

Please sign in to comment.