Skip to content
This repository was archived by the owner on Nov 14, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 30 additions & 2 deletions internal/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/fatih/color"
"golang.org/x/xerrors"
)

Expand Down Expand Up @@ -57,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:
Expand Down Expand Up @@ -113,6 +114,33 @@ 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, 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"`
Expand Down
5 changes: 5 additions & 0 deletions internal/api/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 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)
})
Expand Down
7 changes: 6 additions & 1 deletion internal/humanwriter/human.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Fprintln(w.out, printFunc(prefix), result.Summary)
return err
}
4 changes: 2 additions & 2 deletions internal/humanwriter/human_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down