forked from concourse/concourse
/
exec.go
61 lines (48 loc) · 1.39 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
package topgun
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/onsi/gomega/gexec"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func Start(env []string, command string, argv ...string) *gexec.Session {
TimestampedBy("running: " + command + " " + strings.Join(argv, " "))
cmd := exec.Command(command, argv...)
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
cmd.Dir = filepath.Join(cwd, "..")
cmd.Env = env
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
return session
}
func SpawnInteractive(stdin io.Reader, env []string, command string, argv ...string) *gexec.Session {
TimestampedBy("interactively running: " + command + " " + strings.Join(argv, " "))
cmd := exec.Command(command, argv...)
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
cmd.Dir = filepath.Join(cwd, "..")
cmd.Stdin = stdin
cmd.Env = env
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
return session
}
func TimestampedBy(msg string) {
By(fmt.Sprintf("[%.9f] %s", float64(time.Now().UnixNano())/1e9, msg))
}
func Wait(session *gexec.Session) {
<-session.Exited
Expect(session.ExitCode()).To(Equal(0))
}
func Run(env []string, command string, argv ...string) *gexec.Session {
session := Start(env, command, argv...)
Wait(session)
return session
}