github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/topgun/exec.go (about) 1 package topgun 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "os/exec" 8 "path/filepath" 9 "strings" 10 "time" 11 12 "github.com/onsi/gomega/gexec" 13 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 func Start(env []string, command string, argv ...string) *gexec.Session { 19 TimestampedBy("running: " + command + " " + strings.Join(argv, " ")) 20 21 cmd := exec.Command(command, argv...) 22 cwd, err := os.Getwd() 23 Expect(err).ToNot(HaveOccurred()) 24 cmd.Dir = filepath.Join(cwd, "..") 25 cmd.Env = env 26 27 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) 28 Expect(err).ToNot(HaveOccurred()) 29 30 return session 31 } 32 33 func SpawnInteractive(stdin io.Reader, env []string, command string, argv ...string) *gexec.Session { 34 TimestampedBy("interactively running: " + command + " " + strings.Join(argv, " ")) 35 36 cmd := exec.Command(command, argv...) 37 cwd, err := os.Getwd() 38 Expect(err).ToNot(HaveOccurred()) 39 cmd.Dir = filepath.Join(cwd, "..") 40 cmd.Stdin = stdin 41 cmd.Env = env 42 43 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) 44 Expect(err).ToNot(HaveOccurred()) 45 return session 46 } 47 48 func TimestampedBy(msg string) { 49 By(fmt.Sprintf("[%.9f] %s", float64(time.Now().UnixNano())/1e9, msg)) 50 } 51 52 func Wait(session *gexec.Session) { 53 <-session.Exited 54 Expect(session.ExitCode()).To(Equal(0)) 55 } 56 57 func Run(env []string, command string, argv ...string) *gexec.Session { 58 session := Start(env, command, argv...) 59 Wait(session) 60 return session 61 }