-
Notifications
You must be signed in to change notification settings - Fork 198
/
command.go
76 lines (60 loc) · 1.7 KB
/
command.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
76
package utils
import (
"bytes"
"encoding/base64"
"fmt"
"os"
"os/exec"
"strings"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func RunCommand(dir, command string, args ...string) ([]byte, error) {
return RunCommandWithOptions(dir, command, args, RunCommandOptions{ShouldSucceed: false})
}
func RunSucceedCommand(dir, command string, args ...string) {
_, _ = RunCommandWithOptions(dir, command, args, RunCommandOptions{ShouldSucceed: true})
}
func SucceedCommandOutputString(dir, command string, args ...string) string {
res, _ := RunCommandWithOptions(dir, command, args, RunCommandOptions{ShouldSucceed: true})
return string(res)
}
type RunCommandOptions struct {
ExtraEnv []string
ToStdin string
ShouldSucceed bool
NoStderr bool
}
func RunCommandWithOptions(dir, command string, args []string, options RunCommandOptions) ([]byte, error) {
if isWerfTestBinaryPath(command) {
args = WerfBinArgs(args...)
}
cmd := exec.Command(command, args...)
if dir != "" {
cmd.Dir = dir
}
if len(options.ExtraEnv) != 0 {
cmd.Env = append(os.Environ(), options.ExtraEnv...)
}
if options.ToStdin != "" {
var b bytes.Buffer
b.Write([]byte(options.ToStdin))
cmd.Stdin = &b
}
var res []byte
var err error
if options.NoStderr {
res, err = cmd.Output()
} else {
res, err = cmd.CombinedOutput()
}
_, _ = GinkgoWriter.Write(res)
if options.ShouldSucceed {
errorDesc := fmt.Sprintf("%[2]s %[3]s (dir: %[1]s)", dir, command, strings.Join(args, " "))
Ω(err).ShouldNot(HaveOccurred(), errorDesc)
}
return res, err
}
func ShelloutPack(command string) string {
return fmt.Sprintf("eval $(echo %s | base64 -d)", base64.StdEncoding.EncodeToString([]byte(command)))
}