From 8a5c9922e408da922fd7889672da0cebcb6700a8 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 10 May 2019 00:24:25 +0000 Subject: [PATCH] move to smarter log() and quiet spew() Signed-off-by: Jeff Carr --- shell.go | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/shell.go b/shell.go index f6d0a84..d9ee33f 100644 --- a/shell.go +++ b/shell.go @@ -1,7 +1,6 @@ package shell -import "fmt" -import "log" +// import "log" import "strings" import "time" import "os" @@ -10,22 +9,37 @@ import "bufio" import "github.com/davecgh/go-spew/spew" import "github.com/svent/go-nbreader" +import log "github.com/sirupsen/logrus" +// import "github.com/wercker/journalhook" + var shellStdout *os.File var shellStderr *os.File +var spewOn bool = false + 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") for _, line := range lines { line = strings.TrimSpace(line) // this is like 'chomp' in perl - fmt.Println("LINE:", line) + log.Println("LINE:", line) time.Sleep(1) Run(line) } return 0 } +/* +func UseJournalctl() { + journalhook.Enable() +} +*/ + +func SpewOn() { + spewOn = true +} + func SetStdout(newout *os.File) { shellStdout = newout } @@ -56,7 +70,9 @@ func Run(cmdline string) int { pstdout, _ := process.StdoutPipe() pstderr, _ := process.StderrPipe() - spew.Dump(pstdout) + if (spewOn) { + spew.Dump(pstdout) + } process.Start() @@ -120,8 +136,10 @@ func Run(cmdline string) int { // spew.Dump(process.ProcessState) err := process.Wait() if err != nil { - spew.Dump(err.(*exec.ExitError)) - spew.Dump(process.ProcessState) + if (spewOn) { + spew.Dump(err.(*exec.ExitError)) + spew.Dump(process.ProcessState) + } stuff := err.(*exec.ExitError) log.Println("ERROR ", stuff) log.Println("END ", cmdline) @@ -140,8 +158,10 @@ func Run(cmdline string) int { err := process.Wait() if err != nil { - spew.Dump(err.(*exec.ExitError)) - spew.Dump(process.ProcessState) + if (spewOn) { + spew.Dump(err.(*exec.ExitError)) + spew.Dump(process.ProcessState) + } stuff := err.(*exec.ExitError) log.Println("ERROR ", stuff) log.Println("END ", cmdline)