-
Notifications
You must be signed in to change notification settings - Fork 6
/
cmd.go
90 lines (81 loc) · 1.87 KB
/
cmd.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package test_utils
import (
"fmt"
"os"
"os/exec"
"path/filepath"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
type Command struct {
cmd string
args []string
dir string
timeout string
pollInterval string
env map[string]string
}
func Cmd(cmd string, args ...string) Command {
return Command{
cmd: cmd,
args: args,
timeout: "30s",
pollInterval: "1s",
env: map[string]string{},
}
}
func (c Command) WithDir(dir string) Command {
return Command{
cmd: c.cmd,
args: c.args,
dir: dir,
timeout: c.timeout,
pollInterval: c.pollInterval,
env: c.env,
}
}
func (c Command) WithTimeout(timeout string) Command {
return Command{
cmd: c.cmd,
args: c.args,
dir: c.dir,
timeout: timeout,
pollInterval: c.pollInterval,
env: c.env,
}
}
func (c Command) WithEnv(key, val string) Command {
newEnv := map[string]string{}
for k, v := range c.env {
newEnv[k] = v
}
newEnv[key] = val
return Command{
cmd: c.cmd,
args: c.args,
dir: c.dir,
timeout: c.timeout,
pollInterval: c.pollInterval,
env: newEnv,
}
}
func (c Command) Run() *gexec.Session {
session, err := gexec.Start(c.build(), GinkgoWriter, GinkgoWriter)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
EventuallyWithOffset(1, session, c.timeout, c.pollInterval).Should(gexec.Exit())
return session
}
func (c Command) build() *exec.Cmd {
command := exec.Command(c.cmd, c.args...)
command.Env = os.Environ()
for k, v := range c.env {
command.Env = append(command.Env, fmt.Sprintf("%s=%s", k, v))
}
if c.dir != "" {
cwd, err := os.Getwd()
ExpectWithOffset(2, err).NotTo(HaveOccurred())
command.Dir = filepath.Join(cwd, c.dir)
}
return command
}