github.com/argoproj/argo-events@v1.9.1/test/e2e/fixtures/util.go (about) 1 package fixtures 2 3 import ( 4 "bytes" 5 "fmt" 6 "os" 7 "os/exec" 8 "strings" 9 "time" 10 ) 11 12 func Exec(name string, args ...string) (string, error) { 13 cmd := exec.Command(name, args...) 14 cmd.Env = os.Environ() 15 println(cmd.String()) 16 output, err := runWithTimeout(cmd) 17 // Command completed before timeout. Print output and error if it exists. 18 if err != nil { 19 _, _ = fmt.Fprint(os.Stderr, err) 20 } 21 for _, s := range strings.Split(output, "\n") { 22 println(s) 23 } 24 return output, err 25 } 26 27 func runWithTimeout(cmd *exec.Cmd) (string, error) { 28 var buf bytes.Buffer 29 cmd.Stdout = &buf 30 cmd.Stderr = &buf 31 err := cmd.Start() 32 if err != nil { 33 return "", err 34 } 35 done := make(chan error) 36 go func() { done <- cmd.Wait() }() 37 timeout := time.After(60 * time.Second) 38 select { 39 case <-timeout: 40 _ = cmd.Process.Kill() 41 return buf.String(), fmt.Errorf("timeout") 42 case err := <-done: 43 return buf.String(), err 44 } 45 }