feat: split stdout and stderr reader, writer

main
hau 2020-12-01 17:54:36 +07:00
parent 64ebde9ef8
commit 421efc5422
1 changed files with 24 additions and 6 deletions

30
main.go
View File

@ -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
}