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