generated from goyek/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add color.ReportFlow which is an extension of middleware.ReportFlow (#64
- Loading branch information
Showing
6 changed files
with
115 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Package color contains goyek features which additionally | ||
// have colors. | ||
// | ||
// Set NO_COLOR environment variable to a non-empty string | ||
// or use the NoColor function to prevent colorizing the output. | ||
package color | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
func init() { | ||
color.NoColor = os.Getenv("NO_COLOR") != "" | ||
} | ||
|
||
// NoColor prevents colorizing the output. | ||
func NoColor() { | ||
color.NoColor = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package color | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/fatih/color" | ||
"github.com/goyek/goyek/v2" | ||
) | ||
|
||
// ReportStatus is a middleware which reports the flow execution status with colors. | ||
// | ||
// The format is based on the reports provided by the Go test runner. | ||
func ReportFlow(next goyek.Executor) goyek.Executor { | ||
return func(in goyek.ExecuteInput) error { | ||
out := in.Output | ||
c := color.New(color.Bold) | ||
|
||
from := time.Now() | ||
if err := next(in); err != nil { | ||
c = c.Add(color.FgRed) | ||
c.Fprintf(out, "%v\t%.3fs\n", err, time.Since(from).Seconds()) | ||
return err | ||
} | ||
|
||
c = c.Add(color.FgGreen) | ||
c.Fprintf(out, "ok\t%.3fs\n", time.Since(from).Seconds()) | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package color | ||
|
||
import ( | ||
"io" | ||
"time" | ||
|
||
"github.com/fatih/color" | ||
"github.com/goyek/goyek/v2" | ||
) | ||
|
||
// ReportStatus is a middleware which reports the task run status with colors. | ||
// | ||
// The format is based on the reports provided by the Go test runner. | ||
func ReportStatus(next goyek.Runner) goyek.Runner { | ||
return func(in goyek.Input) goyek.Result { | ||
c := color.New(color.FgBlue) | ||
|
||
// report start task | ||
c.Fprintf(in.Output, "===== TASK %s\n", in.TaskName) | ||
start := time.Now() | ||
|
||
// run | ||
res := next(in) | ||
|
||
// report task end | ||
c = color.New(color.FgGreen) | ||
status := "PASS" | ||
switch res.Status { | ||
case goyek.StatusFailed: | ||
c = color.New(color.FgRed) | ||
status = "FAIL" | ||
case goyek.StatusSkipped: | ||
c = color.New(color.FgYellow) | ||
status = "SKIP" | ||
case goyek.StatusNotRun: | ||
status = "NOOP" | ||
} | ||
c.Fprintf(in.Output, "----- %s: %s (%.2fs)\n", status, in.TaskName, time.Since(start).Seconds()) | ||
|
||
// report panic if happened | ||
if res.PanicStack != nil { | ||
if res.PanicValue != nil { | ||
c.Fprintf(in.Output, "panic: %v", res.PanicValue) | ||
} else { | ||
c.Fprint(in.Output, "panic(nil) or runtime.Goexit() called") | ||
} | ||
io.WriteString(in.Output, "\n\n") //nolint:errcheck // not checking errors when writing to output | ||
c.Fprintf(in.Output, "%s", res.PanicStack) | ||
} | ||
|
||
return res | ||
} | ||
} |