From d50a66b6187e8411ef2519a5def6f0832e6e8d06 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Tue, 14 Sep 2021 10:24:20 +0000 Subject: [PATCH 1/4] feat: enable colored output --- go.mod | 1 + internal/api/types.go | 30 ++++++++++++++++++++++++++++++ internal/api/types_test.go | 5 +++++ internal/humanwriter/human.go | 7 ++++++- 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 525e0f6..a0512a5 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.16 require ( cdr.dev/slog v1.4.1 github.com/Masterminds/semver/v3 v3.1.1 + github.com/fatih/color v1.12.0 github.com/spf13/cobra v1.2.1 golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 k8s.io/api v0.19.14 diff --git a/internal/api/types.go b/internal/api/types.go index 83f3c67..402bd85 100644 --- a/internal/api/types.go +++ b/internal/api/types.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/fatih/color" "golang.org/x/xerrors" ) @@ -113,6 +114,35 @@ func (s CheckState) String() string { panic(fmt.Sprintf("unknown state: %d", s)) } +type PrintFunc func(format string, args ...interface{}) string + +var _ PrintFunc = fmt.Sprintf + +func (s CheckState) Color() (PrintFunc, error) { + switch s { + case StatePassed: + return color.GreenString, nil + case StateFailed: + return color.RedString, nil + case StateWarning: + return color.YellowString, nil + case StateInfo: + return color.CyanString, nil + case StateSkipped: + return fmt.Sprintf, nil + default: + return nil, xerrors.Errorf("unknown state: %d", s) + } +} + +func (s CheckState) MustColor() PrintFunc { + printFunc, err := s.Color() + if err != nil { + panic(err.Error()) + } + return printFunc +} + type CheckResult struct { Name string `json:"name"` State CheckState `json:"state"` diff --git a/internal/api/types_test.go b/internal/api/types_test.go index fc18021..d735d22 100644 --- a/internal/api/types_test.go +++ b/internal/api/types_test.go @@ -34,6 +34,11 @@ func TestKnownStates(t *testing.T) { assert.True(t, "state.Text() is non-empty", len(text) > 0) _ = state.MustText() + colorFunc, err := state.Color() + assert.Success(t, "state.Color() error non-nil", err) + assert.True(t, "state.Color() is non-nil", colorFunc != nil) + _ = state.MustColor() + str := state.String() assert.True(t, "state.String() is non-empty", len(str) > 0) }) diff --git a/internal/humanwriter/human.go b/internal/humanwriter/human.go index 85f24fd..51b508c 100644 --- a/internal/humanwriter/human.go +++ b/internal/humanwriter/human.go @@ -82,6 +82,11 @@ func (w *HumanResultWriter) WriteResult(result *api.CheckResult) error { return err } - _, err = fmt.Fprintf(w.out, "%s %s\n", prefix, result.Summary) + var printFunc api.PrintFunc = fmt.Sprintf + if w.colors { + printFunc = result.State.MustColor() + } + + _, err = fmt.Fprint(w.out, printFunc("%s %s\n", prefix, result.Summary)) return err } From 2c1e59543ca5c949b08e9c8832cc18b1695a71a1 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Tue, 14 Sep 2021 15:12:30 +0100 Subject: [PATCH 2/4] Update internal/api/types_test.go Co-authored-by: Jonathan Yu --- internal/api/types_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/api/types_test.go b/internal/api/types_test.go index d735d22..08df002 100644 --- a/internal/api/types_test.go +++ b/internal/api/types_test.go @@ -35,7 +35,7 @@ func TestKnownStates(t *testing.T) { _ = state.MustText() colorFunc, err := state.Color() - assert.Success(t, "state.Color() error non-nil", err) + assert.Success(t, "state.Color() error nil", err) assert.True(t, "state.Color() is non-nil", colorFunc != nil) _ = state.MustColor() From 542d16b6a5f8317356c7a87eafb114dfc1db2311 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Tue, 14 Sep 2021 14:34:01 +0000 Subject: [PATCH 3/4] make StateInfo and StateSkipped both normal-coloured --- internal/api/types.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/api/types.go b/internal/api/types.go index 402bd85..80d9caa 100644 --- a/internal/api/types.go +++ b/internal/api/types.go @@ -126,9 +126,7 @@ func (s CheckState) Color() (PrintFunc, error) { return color.RedString, nil case StateWarning: return color.YellowString, nil - case StateInfo: - return color.CyanString, nil - case StateSkipped: + case StateInfo, StateSkipped: return fmt.Sprintf, nil default: return nil, xerrors.Errorf("unknown state: %d", s) From 9b518ab7ffd2dc67ecf6f203c6368a56acd60202 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Tue, 14 Sep 2021 15:18:22 +0000 Subject: [PATCH 4/4] update emojis, only colour prefix --- internal/api/types.go | 4 ++-- internal/humanwriter/human.go | 2 +- internal/humanwriter/human_test.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/api/types.go b/internal/api/types.go index 80d9caa..0e41d8d 100644 --- a/internal/api/types.go +++ b/internal/api/types.go @@ -58,11 +58,11 @@ func (s CheckState) MustEmoji() string { func (s CheckState) Emoji() (string, error) { switch s { case StatePassed: - return "👍", nil + return "✓", nil case StateWarning: return "⚠️", nil case StateFailed: - return "👎", nil + return "✗", nil case StateInfo: return "🔔", nil case StateSkipped: diff --git a/internal/humanwriter/human.go b/internal/humanwriter/human.go index 51b508c..ed9c897 100644 --- a/internal/humanwriter/human.go +++ b/internal/humanwriter/human.go @@ -87,6 +87,6 @@ func (w *HumanResultWriter) WriteResult(result *api.CheckResult) error { printFunc = result.State.MustColor() } - _, err = fmt.Fprint(w.out, printFunc("%s %s\n", prefix, result.Summary)) + _, err = fmt.Fprintln(w.out, printFunc(prefix), result.Summary) return err } diff --git a/internal/humanwriter/human_test.go b/internal/humanwriter/human_test.go index cb15bf2..f51a114 100644 --- a/internal/humanwriter/human_test.go +++ b/internal/humanwriter/human_test.go @@ -52,9 +52,9 @@ func TestHumanWriter(t *testing.T) { var expected string switch mode { case humanwriter.OutputModeEmoji: - expected = "👍 human writer check test\n" + + expected = "✓ human writer check test\n" + "🔔 summary\n" + - "👎 failed message\n" + + "✗ failed message\n" + "⚠️ \n" + "⏩ skipped check\n" case humanwriter.OutputModeText: