refactor: split color func from runGoTest

main
hau 2020-12-18 17:21:21 +07:00
parent 4467751b1a
commit c72f25c2db
1 changed files with 47 additions and 44 deletions

31
main.go
View File

@ -33,10 +33,20 @@ func runGoTest() int {
cmd.Stdout = outWriter cmd.Stdout = outWriter
cmd.Stderr = errWriter cmd.Stderr = errWriter
go func() { go colorOutputReader(outReader)
defer outReader.Close() go colorErrorReader(errReader)
scanner := bufio.NewScanner(outReader) if err := cmd.Run(); err != nil {
return 1
}
return 0
}
func colorOutputReader(reader io.ReadCloser) {
defer reader.Close()
scanner := bufio.NewScanner(reader)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
@ -61,12 +71,12 @@ func runGoTest() int {
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
log.Printf("scanner error: %s", err) log.Printf("scanner error: %s", err)
} }
}() }
go func() { func colorErrorReader(reader io.ReadCloser) {
defer errReader.Close() defer reader.Close()
scanner := bufio.NewScanner(errReader) scanner := bufio.NewScanner(reader)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
@ -76,11 +86,4 @@ func runGoTest() int {
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
log.Printf("scanner error: %s", err) log.Printf("scanner error: %s", err)
} }
}()
if err := cmd.Run(); err != nil {
return 1
}
return 0
} }