sudo pacman -Syu 2022-08-13 16:46:53 +07:00
parent 53248bff4d
commit bcc2054da6
No known key found for this signature in database
GPG Key ID: D6CB5C6C567C47B0
1 changed files with 22 additions and 20 deletions

42
main.go
View File

@ -12,6 +12,8 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
) )
const cmdName = "go-test-color"
func main() { func main() {
code := runGoTest() code := runGoTest()
os.Exit(code) os.Exit(code)
@ -24,33 +26,33 @@ func runGoTest() int {
args = append(args, os.Args[1:]...) args = append(args, os.Args[1:]...)
cmd := exec.Command("go", args...) cmd := exec.Command("go", args...)
// Output pipe // Read stdout and stderr
outReader, outWriter := io.Pipe() outReader, err := cmd.StdoutPipe()
defer outReader.Close() if err != nil {
defer outWriter.Close() log.Printf("%s failed to get stdout pipe: %s", cmdName, err)
return 1
}
// Error pipe errReader, err := cmd.StderrPipe()
errReader, errWriter := io.Pipe() if err != nil {
defer errReader.Close() log.Printf("%s failed to get stderr pipe: %s", cmdName, err)
defer errWriter.Close() return 1
}
// Redirect cmd pipes to our pipes
cmd.Stdout = outWriter
cmd.Stderr = errWriter
// See https://stackoverflow.com/questions/8875038/redirect-stdout-pipe-of-child-process-in-go // See https://stackoverflow.com/questions/8875038/redirect-stdout-pipe-of-child-process-in-go
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
log.Printf("Failed to start: %s", err) log.Printf("%s failed to start: %s", cmdName, err)
return 1 return 1
} }
defer func() {
if err := cmd.Wait(); err != nil {
log.Printf("Failed to wait: %s", err)
}
}()
go colorOutputReader(outReader) // Add color to both stdout and stderr
go colorErrorReader(errReader) colorOutputReader(outReader)
colorErrorReader(errReader)
if err := cmd.Wait(); err != nil {
log.Printf("%s failed to wait: %s", cmdName, err)
return 1
}
return 0 return 0
} }