Log and return stdout/stderr consistency

pull/5917/head
Thomas Stromberg 2019-11-14 20:02:59 -08:00
parent 1c46502f35
commit b04b2ad5ee
1 changed files with 14 additions and 13 deletions

View File

@ -18,6 +18,7 @@ package command
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
@ -39,7 +40,7 @@ type ExecRunner struct{}
// RunCmd implements the Command Runner interface to run a exec.Cmd object
func (*ExecRunner) RunCmd(cmd *exec.Cmd) (*RunResult, error) {
rr := &RunResult{Args: cmd.Args}
glog.Infof("(ExecRunner) Run: %v", rr.Command())
glog.Infof("Run: %v", rr.Command())
var outb, errb io.Writer
if cmd.Stdout == nil {
@ -62,19 +63,19 @@ func (*ExecRunner) RunCmd(cmd *exec.Cmd) (*RunResult, error) {
start := time.Now()
err := cmd.Run()
elapsed := time.Since(start)
if err == nil {
// Reduce log spam
if elapsed > (1 * time.Second) {
glog.Infof("(ExecRunner) Done: %v: (%s)", rr.Command(), elapsed)
}
} else {
if exitError, ok := err.(*exec.ExitError); ok {
rr.ExitCode = exitError.ExitCode()
}
glog.Infof("(ExecRunner) Non-zero exit: %v: %v (%s)\n%s", rr.Command(), err, elapsed, rr.Output())
err = errors.Wrapf(err, "command failed: %s\nstdout: %s\nstderr: %s", rr.Command(), rr.Stdout.String(), rr.Stderr.String())
// Decrease log spam
if elapsed > (1 * time.Second) {
glog.Infof("Exit %d for %v: (%s)", rr.Command(), elapsed)
}
return rr, err
if err == nil {
return rr, nil
}
return rr, fmt.Errorf("%s: %v\nstdout: %s\nstderr: %s", rr.Command(), err, rr.Stdout.String(), rr.Stderr.String())
}
// Copy copies a file and its permissions