Log and return stdout/stderr consistency
parent
1c46502f35
commit
b04b2ad5ee
|
@ -18,6 +18,7 @@ package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
@ -39,7 +40,7 @@ type ExecRunner struct{}
|
||||||
// RunCmd implements the Command Runner interface to run a exec.Cmd object
|
// RunCmd implements the Command Runner interface to run a exec.Cmd object
|
||||||
func (*ExecRunner) RunCmd(cmd *exec.Cmd) (*RunResult, error) {
|
func (*ExecRunner) RunCmd(cmd *exec.Cmd) (*RunResult, error) {
|
||||||
rr := &RunResult{Args: cmd.Args}
|
rr := &RunResult{Args: cmd.Args}
|
||||||
glog.Infof("(ExecRunner) Run: %v", rr.Command())
|
glog.Infof("Run: %v", rr.Command())
|
||||||
|
|
||||||
var outb, errb io.Writer
|
var outb, errb io.Writer
|
||||||
if cmd.Stdout == nil {
|
if cmd.Stdout == nil {
|
||||||
|
@ -62,19 +63,19 @@ func (*ExecRunner) RunCmd(cmd *exec.Cmd) (*RunResult, error) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
if err == nil {
|
|
||||||
// Reduce log spam
|
if exitError, ok := err.(*exec.ExitError); ok {
|
||||||
if elapsed > (1 * time.Second) {
|
rr.ExitCode = exitError.ExitCode()
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
return rr, err
|
// Decrease log spam
|
||||||
|
if elapsed > (1 * time.Second) {
|
||||||
|
glog.Infof("Exit %d for %v: (%s)", rr.Command(), elapsed)
|
||||||
|
}
|
||||||
|
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
|
// Copy copies a file and its permissions
|
||||||
|
|
Loading…
Reference in New Issue