Split stdout/stderr in runCommand()

Sometimes git emits things on stderr that are not errors but not part of
the programmatic output either.  Don't combine the output.
This commit is contained in:
Tim Hockin 2020-03-09 20:29:23 -07:00
parent 95a1690e6f
commit 82cd91958b
1 changed files with 12 additions and 32 deletions

View File

@ -23,7 +23,6 @@ import (
"context" "context"
"flag" "flag"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
@ -719,22 +718,7 @@ func cmdForLog(command string, args ...string) string {
} }
func runCommand(ctx context.Context, cwd, command string, args ...string) (string, error) { func runCommand(ctx context.Context, cwd, command string, args ...string) (string, error) {
log.V(5).Info("running command", "cwd", cwd, "cmd", cmdForLog(command, args...)) return runCommandWithStdin(ctx, cwd, "", command, args...)
cmd := exec.CommandContext(ctx, command, args...)
if cwd != "" {
cmd.Dir = cwd
}
output, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded {
return "", fmt.Errorf("command timed out: %v: %q", err, string(output))
}
if err != nil {
return "", fmt.Errorf("error running command: %v: %q", err, string(output))
}
return string(output), nil
} }
func runCommandWithStdin(ctx context.Context, cwd, stdin, command string, args ...string) (string, error) { func runCommandWithStdin(ctx context.Context, cwd, stdin, command string, args ...string) (string, error) {
@ -744,27 +728,23 @@ func runCommandWithStdin(ctx context.Context, cwd, stdin, command string, args .
if cwd != "" { if cwd != "" {
cmd.Dir = cwd cmd.Dir = cwd
} }
outbuf := bytes.NewBuffer(nil)
errbuf := bytes.NewBuffer(nil)
cmd.Stdout = outbuf
cmd.Stderr = errbuf
cmd.Stdin = bytes.NewBufferString(stdin)
in, err := cmd.StdinPipe() err := cmd.Run()
if err != nil { stdout := outbuf.String()
return "", err stderr := errbuf.String()
}
if _, err := io.Copy(in, bytes.NewBufferString(stdin)); err != nil {
return "", err
}
if err := in.Close(); err != nil {
return "", err
}
output, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded { if ctx.Err() == context.DeadlineExceeded {
return "", fmt.Errorf("command timed out: %v: %q", err, string(output)) return "", fmt.Errorf("command timed out: %v: { stdout: %q, stderr: %q }", err, stdout, stderr)
} }
if err != nil { if err != nil {
return "", fmt.Errorf("error running command: %v: %q", err, string(output)) return "", fmt.Errorf("error running command: %v: { stdout: %q, stderr: %q }", err, stdout, stderr)
} }
return string(output), nil return stdout, nil
} }
func setupGitAuth(ctx context.Context, username, password, gitURL string) error { func setupGitAuth(ctx context.Context, username, password, gitURL string) error {