Skip to content
This repository was archived by the owner on Nov 14, 2024. It is now read-only.

Commit d788ecd

Browse files
johnstcnjawnsy
andauthored
feat: enable colored output (#17)
* feat: enable colored output * Update internal/api/types_test.go Co-authored-by: Jonathan Yu <jonathan@coder.com>
1 parent cdc5cfd commit d788ecd

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.16
55
require (
66
cdr.dev/slog v1.4.1
77
github.com/Masterminds/semver/v3 v3.1.1
8+
github.com/fatih/color v1.12.0
89
github.com/spf13/cobra v1.2.1
910
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
1011
k8s.io/api v0.19.14

internal/api/types.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/fatih/color"
78
"golang.org/x/xerrors"
89
)
910

@@ -57,11 +58,11 @@ func (s CheckState) MustEmoji() string {
5758
func (s CheckState) Emoji() (string, error) {
5859
switch s {
5960
case StatePassed:
60-
return "👍", nil
61+
return "", nil
6162
case StateWarning:
6263
return "⚠️", nil
6364
case StateFailed:
64-
return "👎", nil
65+
return "", nil
6566
case StateInfo:
6667
return "🔔", nil
6768
case StateSkipped:
@@ -113,6 +114,33 @@ func (s CheckState) String() string {
113114
panic(fmt.Sprintf("unknown state: %d", s))
114115
}
115116

117+
type PrintFunc func(format string, args ...interface{}) string
118+
119+
var _ PrintFunc = fmt.Sprintf
120+
121+
func (s CheckState) Color() (PrintFunc, error) {
122+
switch s {
123+
case StatePassed:
124+
return color.GreenString, nil
125+
case StateFailed:
126+
return color.RedString, nil
127+
case StateWarning:
128+
return color.YellowString, nil
129+
case StateInfo, StateSkipped:
130+
return fmt.Sprintf, nil
131+
default:
132+
return nil, xerrors.Errorf("unknown state: %d", s)
133+
}
134+
}
135+
136+
func (s CheckState) MustColor() PrintFunc {
137+
printFunc, err := s.Color()
138+
if err != nil {
139+
panic(err.Error())
140+
}
141+
return printFunc
142+
}
143+
116144
type CheckResult struct {
117145
Name string `json:"name"`
118146
State CheckState `json:"state"`

internal/api/types_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ func TestKnownStates(t *testing.T) {
3434
assert.True(t, "state.Text() is non-empty", len(text) > 0)
3535
_ = state.MustText()
3636

37+
colorFunc, err := state.Color()
38+
assert.Success(t, "state.Color() error nil", err)
39+
assert.True(t, "state.Color() is non-nil", colorFunc != nil)
40+
_ = state.MustColor()
41+
3742
str := state.String()
3843
assert.True(t, "state.String() is non-empty", len(str) > 0)
3944
})

internal/humanwriter/human.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ func (w *HumanResultWriter) WriteResult(result *api.CheckResult) error {
8282
return err
8383
}
8484

85-
_, err = fmt.Fprintf(w.out, "%s %s\n", prefix, result.Summary)
85+
var printFunc api.PrintFunc = fmt.Sprintf
86+
if w.colors {
87+
printFunc = result.State.MustColor()
88+
}
89+
90+
_, err = fmt.Fprintln(w.out, printFunc(prefix), result.Summary)
8691
return err
8792
}

internal/humanwriter/human_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ func TestHumanWriter(t *testing.T) {
5252
var expected string
5353
switch mode {
5454
case humanwriter.OutputModeEmoji:
55-
expected = "👍 human writer check test\n" +
55+
expected = " human writer check test\n" +
5656
"🔔 summary\n" +
57-
"👎 failed message\n" +
57+
" failed message\n" +
5858
"⚠️ \n" +
5959
"⏩ skipped check\n"
6060
case humanwriter.OutputModeText:

0 commit comments

Comments
 (0)