refactor: replace cmd.Run with cmd.Start and cmd.Wait
See https://stackoverflow.com/questions/8875038/redirect-stdout-pipe-of-child-process-in-gomain
parent
31724133f1
commit
e488efb295
26
main.go
26
main.go
|
@ -17,34 +17,40 @@ func main() {
|
||||||
os.Exit(code)
|
os.Exit(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run go test with args
|
||||||
func runGoTest() int {
|
func runGoTest() int {
|
||||||
// Run go test
|
// Pass all args
|
||||||
|
|
||||||
args := []string{"test"}
|
args := []string{"test"}
|
||||||
args = append(args, os.Args[1:]...)
|
args = append(args, os.Args[1:]...)
|
||||||
|
|
||||||
cmd := exec.Command("go", args...)
|
cmd := exec.Command("go", args...)
|
||||||
cmd.Env = os.Environ()
|
|
||||||
|
|
||||||
// Output pipe and error pipe
|
|
||||||
|
|
||||||
|
// Output pipe
|
||||||
outReader, outWriter := io.Pipe()
|
outReader, outWriter := io.Pipe()
|
||||||
defer outReader.Close()
|
defer outReader.Close()
|
||||||
defer outWriter.Close()
|
defer outWriter.Close()
|
||||||
|
|
||||||
|
// Error pipe
|
||||||
errReader, errWriter := io.Pipe()
|
errReader, errWriter := io.Pipe()
|
||||||
defer errReader.Close()
|
defer errReader.Close()
|
||||||
defer errWriter.Close()
|
defer errWriter.Close()
|
||||||
|
|
||||||
|
// Redirect cmd pipes to our pipes
|
||||||
cmd.Stdout = outWriter
|
cmd.Stdout = outWriter
|
||||||
cmd.Stderr = errWriter
|
cmd.Stderr = errWriter
|
||||||
|
|
||||||
go colorOutputReader(outReader)
|
// See https://stackoverflow.com/questions/8875038/redirect-stdout-pipe-of-child-process-in-go
|
||||||
go colorErrorReader(errReader)
|
if err := cmd.Start(); err != nil {
|
||||||
|
log.Printf("Failed to start: %s", err)
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := cmd.Wait(); err != nil {
|
||||||
|
log.Printf("Failed to wait: %s", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go colorOutputReader(outReader)
|
||||||
|
go colorErrorReader(errReader)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue