From 026c59bc5c23630c1f918167e5237f315dc5895b Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 9 May 2019 22:20:26 +0000 Subject: [PATCH] allow stdout redirection Signed-off-by: Jeff Carr --- shell.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/shell.go b/shell.go index 037787a..caed428 100644 --- a/shell.go +++ b/shell.go @@ -10,6 +10,9 @@ import "bufio" import "github.com/davecgh/go-spew/spew" import "github.com/svent/go-nbreader" +var shellStdout *os.File +var shellStderr *os.File + func Script(cmds string) int { // split on new lines (while we are at it, handle stupid windows text files lines := strings.Split(strings.Replace(cmds, "\r\n", "\n", -1), "\n") @@ -23,6 +26,14 @@ func Script(cmds string) int { return 0 } +func SetStdout(newout *os.File) { + shellStdout = newout +} + +func SetStderr(newerr *os.File) { + shellStderr = newerr +} + func Run(cmdline string) int { log.Println("START " + cmdline) @@ -42,16 +53,23 @@ func Run(cmdline string) int { } process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...) - stdout, _ := process.StdoutPipe() - stderr, _ := process.StderrPipe() + pstdout, _ := process.StdoutPipe() + pstderr, _ := process.StderrPipe() + + spew.Dump(pstdout) + process.Start() - f := bufio.NewWriter(os.Stdout) + if (shellStdout == nil) { + shellStdout = os.Stdout + } - newreader := bufio.NewReader(stdout) + f := bufio.NewWriter(shellStdout) + + newreader := bufio.NewReader(pstdout) nbr := nbreader.NewNBReader(newreader, 1024) - newerrreader := bufio.NewReader(stderr) + newerrreader := bufio.NewReader(pstderr) nbrerr := nbreader.NewNBReader(newerrreader, 1024) for {