/
exec.go
66 lines (55 loc) · 1.68 KB
/
exec.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
package tool
import (
"fmt"
"strings"
"github.com/helm/chart-testing/v3/pkg/exec"
)
type ProcessExecutorer interface {
RunProcessAndCaptureOutput(executable string, execArgs ...interface{}) (string, error)
}
type ProcessExecutor struct {
exec.ProcessExecutor
}
func NewProcessExecutor(debug bool) ProcessExecutor {
return ProcessExecutor{
exec.NewProcessExecutor(debug),
}
}
func (p ProcessExecutor) RunProcessAndCaptureOutput(executable string, execArgs ...interface{}) (string, error) {
return p.RunProcessInDirAndCaptureOutput("", executable, execArgs...)
}
// RunProcessInDirAndCaptureOutput overrides exec.ProcessExecutor's and inject the command line and any streamed content
// to either Stdout or Stderr into the returned error, if any.
func (p ProcessExecutor) RunProcessInDirAndCaptureOutput(
workingDirectory string,
executable string,
execArgs ...interface{},
) (string, error) {
cmd, err := p.CreateProcess(executable, execArgs...)
if err != nil {
return "", err
}
cmd.Dir = workingDirectory
bytes, err := cmd.CombinedOutput()
capturedOutput := strings.TrimSpace(string(bytes))
execArgsCopy := toStringArray(execArgs)
execArgsStr := strings.Join(execArgsCopy, " ")
if err != nil {
if len(capturedOutput) == 0 {
return "", fmt.Errorf(
"error running process: executing %s with args %q: %w",
executable, execArgsStr, err)
}
return capturedOutput, fmt.Errorf(
"error running process: executing %s with args %q: %w\n---\n%s",
executable, execArgsStr, err, capturedOutput)
}
return capturedOutput, nil
}
func toStringArray(args []interface{}) []string {
cpy := make([]string, len(args))
for i, a := range args {
cpy[i] = fmt.Sprint(a)
}
return cpy
}