move to smarter log() and quiet spew()

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-10 00:24:25 +00:00
parent f1d7a816be
commit 8a5c9922e4
1 changed files with 28 additions and 8 deletions

View File

@ -1,7 +1,6 @@
package shell package shell
import "fmt" // import "log"
import "log"
import "strings" import "strings"
import "time" import "time"
import "os" import "os"
@ -10,22 +9,37 @@ import "bufio"
import "github.com/davecgh/go-spew/spew" import "github.com/davecgh/go-spew/spew"
import "github.com/svent/go-nbreader" import "github.com/svent/go-nbreader"
import log "github.com/sirupsen/logrus"
// import "github.com/wercker/journalhook"
var shellStdout *os.File var shellStdout *os.File
var shellStderr *os.File var shellStderr *os.File
var spewOn bool = false
func Script(cmds string) int { func Script(cmds string) int {
// split on new lines (while we are at it, handle stupid windows text files // 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") lines := strings.Split(strings.Replace(cmds, "\r\n", "\n", -1), "\n")
for _, line := range lines { for _, line := range lines {
line = strings.TrimSpace(line) // this is like 'chomp' in perl line = strings.TrimSpace(line) // this is like 'chomp' in perl
fmt.Println("LINE:", line) log.Println("LINE:", line)
time.Sleep(1) time.Sleep(1)
Run(line) Run(line)
} }
return 0 return 0
} }
/*
func UseJournalctl() {
journalhook.Enable()
}
*/
func SpewOn() {
spewOn = true
}
func SetStdout(newout *os.File) { func SetStdout(newout *os.File) {
shellStdout = newout shellStdout = newout
} }
@ -56,7 +70,9 @@ func Run(cmdline string) int {
pstdout, _ := process.StdoutPipe() pstdout, _ := process.StdoutPipe()
pstderr, _ := process.StderrPipe() pstderr, _ := process.StderrPipe()
spew.Dump(pstdout) if (spewOn) {
spew.Dump(pstdout)
}
process.Start() process.Start()
@ -120,8 +136,10 @@ func Run(cmdline string) int {
// spew.Dump(process.ProcessState) // spew.Dump(process.ProcessState)
err := process.Wait() err := process.Wait()
if err != nil { if err != nil {
spew.Dump(err.(*exec.ExitError)) if (spewOn) {
spew.Dump(process.ProcessState) spew.Dump(err.(*exec.ExitError))
spew.Dump(process.ProcessState)
}
stuff := err.(*exec.ExitError) stuff := err.(*exec.ExitError)
log.Println("ERROR ", stuff) log.Println("ERROR ", stuff)
log.Println("END ", cmdline) log.Println("END ", cmdline)
@ -140,8 +158,10 @@ func Run(cmdline string) int {
err := process.Wait() err := process.Wait()
if err != nil { if err != nil {
spew.Dump(err.(*exec.ExitError)) if (spewOn) {
spew.Dump(process.ProcessState) spew.Dump(err.(*exec.ExitError))
spew.Dump(process.ProcessState)
}
stuff := err.(*exec.ExitError) stuff := err.(*exec.ExitError)
log.Println("ERROR ", stuff) log.Println("ERROR ", stuff)
log.Println("END ", cmdline) log.Println("END ", cmdline)