-
Notifications
You must be signed in to change notification settings - Fork 170
/
runresult.go
75 lines (63 loc) · 1.71 KB
/
runresult.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package exec
import (
"fmt"
"os/exec"
)
// RunResult is the result of running a command.
type RunResult struct {
// The exit code of the command.
ExitCode int
// The stdout output captured from running the command.
Stdout string
// The stderr output captured from running the command.
Stderr string
}
func NewRunResult(code int, stdout, stderr string) RunResult {
return RunResult{
ExitCode: code,
Stdout: stdout,
Stderr: stderr,
}
}
// ExitError is the error returned when a command unsuccessfully exits.
type ExitError struct {
// The path or name of the command being invoked.
Cmd string
// The exit code of the command.
ExitCode int
stdOut string
stdErr string
outputAvailable bool
// The underlying exec.ExitError.
err exec.ExitError
}
func NewExitError(
exitErr exec.ExitError,
cmd string,
stdOut string,
stdErr string,
outputAvailable bool) error {
return &ExitError{
ExitCode: exitErr.ExitCode(),
Cmd: cmd,
err: exitErr,
stdOut: stdOut,
stdErr: stdErr,
outputAvailable: outputAvailable,
}
}
// Error augments the underlying exec.ExitError's Error with the stdout and stderr output of the command, if available.
func (e *ExitError) Error() string {
var errorPrefix string
// Handle the case where the underlying error represents an exit code error. In this case we'd rather use "exit code"
// and not "exit status" as the error message, to make it easier to find in logs.
if e.err.Exited() {
errorPrefix = fmt.Sprintf("exit code: %d", e.err.ExitCode())
} else {
errorPrefix = e.err.Error()
}
if !e.outputAvailable {
return errorPrefix
}
return fmt.Sprintf("%s, stdout: %s, stderr: %s", errorPrefix, e.stdOut, e.stdErr)
}