feat: split stdout and stderr reader, writer
parent
64ebde9ef8
commit
421efc5422
30
main.go
30
main.go
|
@ -24,16 +24,19 @@ func runGoTest() int {
|
|||
cmd := exec.Command("go", args...)
|
||||
cmd.Env = os.Environ()
|
||||
|
||||
reader, writer := io.Pipe()
|
||||
defer writer.Close()
|
||||
outReader, outWriter := io.Pipe()
|
||||
defer outWriter.Close()
|
||||
|
||||
cmd.Stdout = writer
|
||||
cmd.Stderr = writer
|
||||
errReader, errWriter := io.Pipe()
|
||||
defer errWriter.Close()
|
||||
|
||||
cmd.Stdout = outWriter
|
||||
cmd.Stderr = errWriter
|
||||
|
||||
go func() {
|
||||
defer reader.Close()
|
||||
defer outReader.Close()
|
||||
|
||||
scanner := bufio.NewScanner(reader)
|
||||
scanner := bufio.NewScanner(outReader)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
@ -60,6 +63,21 @@ func runGoTest() int {
|
|||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer errReader.Close()
|
||||
|
||||
scanner := bufio.NewScanner(errReader)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
color.Red("%s\n", line)
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Printf("scanner error: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
return 1
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue