Skip to content

Commit

Permalink
Add color.ReportFlow which is an extension of middleware.ReportFlow (#64
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pellared authored Aug 8, 2024
1 parent c7600fc commit 4fd664e
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 62 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion boot/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions color/global.go
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
}
61 changes: 0 additions & 61 deletions color/color.go → color/logger.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 29 additions & 0 deletions color/reportflow.go
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
}
}
53 changes: 53 additions & 0 deletions color/reportstatus.go
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
}
}

0 comments on commit 4fd664e

Please sign in to comment.