diff options
| author | Jeff Carr <[email protected]> | 2019-05-09 22:20:26 +0000 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2019-05-09 22:20:26 +0000 |
| commit | 026c59bc5c23630c1f918167e5237f315dc5895b (patch) | |
| tree | 3dd4ba947c50e7f07eeb1788394348655df188c6 /shell.go | |
| parent | 7048640de6155694190b548d47a19e639384c903 (diff) | |
allow stdout redirection
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'shell.go')
| -rw-r--r-- | shell.go | 28 |
1 files changed, 23 insertions, 5 deletions
@@ -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 + } + + f := bufio.NewWriter(shellStdout) - newreader := bufio.NewReader(stdout) + newreader := bufio.NewReader(pstdout) nbr := nbreader.NewNBReader(newreader, 1024) - newerrreader := bufio.NewReader(stderr) + newerrreader := bufio.NewReader(pstderr) nbrerr := nbreader.NewNBReader(newerrreader, 1024) for { |
