diff --git a/CHANGELOG.md b/CHANGELOG.md index 3397d59..65c7e95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this library adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) as well as to [Module version numbering](https://go.dev/doc/modules/version-numbers). +## [Unreleased](https://github.com/goyek/x/compare/v0.1.7...HEAD) + +### Added + +- Add `color.ReportFlow` which is an extension of `middleware.ReportFlow`. + +### Changed + +- Bump `github.com/goyek/goyek` to `2.2.0`. +- Bump `github.com/fatih/color` to `1.17.0`. + ## [0.1.7](https://github.com/goyek/x/releases/tag/v0.1.7) - 2024-01-17 ### Added diff --git a/boot/boot.go b/boot/boot.go index dd59dfa..d6b8fd7 100644 --- a/boot/boot.go +++ b/boot/boot.go @@ -35,7 +35,7 @@ func Main() { *v = true // needed to report the task status } - goyek.UseExecutor(middleware.ReportFlow) + goyek.UseExecutor(color.ReportFlow) if *dryRun { goyek.Use(middleware.DryRun) diff --git a/color/global.go b/color/global.go new file mode 100644 index 0000000..a3b357e --- /dev/null +++ b/color/global.go @@ -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 +} diff --git a/color/color.go b/color/logger.go similarity index 78% rename from color/color.go rename to color/logger.go index 9659075..07ff210 100644 --- a/color/color.go +++ b/color/logger.go @@ -1,76 +1,15 @@ -// 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 ( "fmt" "io" - "os" "runtime" "strings" "sync" - "time" "github.com/fatih/color" - "github.com/goyek/goyek/v2" ) -func init() { - color.NoColor = os.Getenv("NO_COLOR") != "" -} - -// NoColor prevents colorizing the output. -func NoColor() { - color.NoColor = true -} - -// 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, color.Bold) - 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 - } -} - // CodeLineLogger decorates the log with code line information, identation and colors. type CodeLineLogger struct { mu sync.Mutex diff --git a/color/reportflow.go b/color/reportflow.go new file mode 100644 index 0000000..b3ce4cd --- /dev/null +++ b/color/reportflow.go @@ -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 + } +} diff --git a/color/reportstatus.go b/color/reportstatus.go new file mode 100644 index 0000000..43ca411 --- /dev/null +++ b/color/reportstatus.go @@ -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 + } +}