-
Notifications
You must be signed in to change notification settings - Fork 36
/
exec_error.go
45 lines (37 loc) · 1002 Bytes
/
exec_error.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
package system
import (
"fmt"
"strings"
)
const (
execErrorMsgFmt = "Running command: '%s', stdout: '%s', stderr: '%s'"
execShortErrorMaxLines = 100
)
type ExecError struct {
Command string
StdOut string
StdErr string
}
func NewExecError(cmd, stdout, stderr string) ExecError {
return ExecError{
Command: cmd,
StdOut: stdout,
StdErr: stderr,
}
}
func (e ExecError) Error() string {
return fmt.Sprintf(execErrorMsgFmt, e.Command, e.StdOut, e.StdErr)
}
// ShortError returns an error message that has stdout/stderr truncated.
func (e ExecError) ShortError() string {
outStr := e.truncateStr(e.StdOut, execShortErrorMaxLines)
errStr := e.truncateStr(e.StdErr, execShortErrorMaxLines)
return fmt.Sprintf(execErrorMsgFmt, e.Command, outStr, errStr)
}
func (e ExecError) truncateStr(in string, maxLines int) string {
outLines := strings.Split(in, "\n")
if i := len(outLines); i > maxLines {
outLines = outLines[i-maxLines:]
}
return strings.Join(outLines, "\n")
}