Skip to content

Commit c965b17

Browse files
committed
renderer: override color support logic to add colors on GitHub Actions
1 parent 74e551f commit c965b17

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

cmd/kube-score/main.go

+44-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"path/filepath"
1313

14+
"github.com/mattn/go-isatty"
1415
flag "github.com/spf13/pflag"
1516
"golang.org/x/term"
1617

@@ -106,6 +107,7 @@ func scoreFiles(binName string, args []string) error {
106107
printHelp := fs.Bool("help", false, "Print help")
107108
outputFormat := fs.StringP("output-format", "o", "human", "Set to 'human', 'json', 'ci' or 'sarif'. If set to ci, kube-score will output the program in a format that is easier to parse by other programs. Sarif output allows for easier integration with CI platforms.")
108109
outputVersion := fs.String("output-version", "", "Changes the version of the --output-format. The 'json' format has version 'v2' (default) and 'v1' (deprecated, will be removed in v1.7.0). The 'human' and 'ci' formats has only version 'v1' (default). If not explicitly set, the default version for that particular output format will be used.")
110+
color := fs.String("color", "auto", "If the output should be colored. Set to 'always', 'never' or 'auto'. If set to 'auto', kube-score will try to detect if the current terminal / platform supports colors. If set to 'never', kube-score will not output any colors. If set to 'always', kube-score will output colors even if the current terminal / platform does not support colors.")
109111
optionalTests := fs.StringSlice("enable-optional-test", []string{}, "Enable an optional test, can be set multiple times")
110112
ignoreTests := fs.StringSlice("ignore-test", []string{}, "Disable a test, can be set multiple times")
111113
disableIgnoreChecksAnnotation := fs.Bool("disable-ignore-checks-annotations", false, "Set to true to disable the effect of the 'kube-score/ignore' annotations")
@@ -128,6 +130,16 @@ func scoreFiles(binName string, args []string) error {
128130
return fmt.Errorf("Error: --output-format must be set to: 'human', 'json', 'sarif' or 'ci'")
129131
}
130132

133+
acceptedColors := map[string]bool{
134+
"auto": true,
135+
"always": true,
136+
"never": true,
137+
}
138+
if !acceptedColors[*color] {
139+
fs.Usage()
140+
return fmt.Errorf("Error: --color must be set to: 'auto', 'always' or 'never'")
141+
}
142+
131143
filesToRead := fs.Args()
132144
if len(filesToRead) == 0 {
133145
return fmt.Errorf(`Error: No files given as arguments.
@@ -220,7 +232,7 @@ Use "-" as filename to read from STDIN.`, execName(binName))
220232
if err != nil {
221233
termWidth = 80
222234
}
223-
r, err = human.Human(scoreCard, *verboseOutput, termWidth)
235+
r, err = human.Human(scoreCard, *verboseOutput, termWidth, useColor(*color))
224236
if err != nil {
225237
return err
226238
}
@@ -299,3 +311,34 @@ type namedReader struct {
299311
func (n namedReader) Name() string {
300312
return n.name
301313
}
314+
315+
func useColor(colorArg string) bool {
316+
// Respect user preference
317+
switch colorArg {
318+
case "always":
319+
return true
320+
case "never":
321+
return false
322+
}
323+
324+
// If running on Github Actions, use colors
325+
if _, ok := os.LookupEnv("GITHUB_ACTIONS"); ok {
326+
return true
327+
}
328+
329+
// If NO_COLOR is set, don't use color
330+
if _, ok := os.LookupEnv("NO_COLOR"); ok {
331+
return false
332+
}
333+
334+
// Dont use color if not a terminal
335+
if os.Getenv("TERM") == "dumb" {
336+
return false
337+
}
338+
if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) {
339+
return false
340+
}
341+
342+
// Use colors
343+
return true
344+
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/google/gofuzz v1.1.0 // indirect
2020
github.com/json-iterator/go v1.1.12 // indirect
2121
github.com/mattn/go-colorable v0.1.9 // indirect
22-
github.com/mattn/go-isatty v0.0.14 // indirect
22+
github.com/mattn/go-isatty v0.0.17 // indirect
2323
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2424
github.com/modern-go/reflect2 v1.0.2 // indirect
2525
github.com/pmezard/go-difflib v1.0.0 // indirect

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope
2525
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
2626
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
2727
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
28+
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
29+
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
2830
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
2931
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
3032
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -65,6 +67,7 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
6567
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6668
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6769
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
70+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6871
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
6972
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7073
golang.org/x/term v0.3.0 h1:qoo4akIqOcDME5bhc/NgxUdovd6BSS2uMsVjB56q1xI=

renderer/human/human.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ import (
1515
"github.com/zegl/kube-score/scorecard"
1616
)
1717

18-
func Human(scoreCard *scorecard.Scorecard, verboseOutput int, termWidth int) (io.Reader, error) {
18+
func Human(scoreCard *scorecard.Scorecard, verboseOutput int, termWidth int, useColors bool) (io.Reader, error) {
1919
// Print the items sorted by scorecard key
2020
var keys []string
2121
for k := range *scoreCard {
2222
keys = append(keys, k)
2323
}
2424
sort.Strings(keys)
2525

26+
// Override usage of colors to our own preference
27+
color.NoColor = !useColors
28+
2629
w := bytes.NewBufferString("")
2730

2831
for _, key := range keys {

0 commit comments

Comments
 (0)