Skip to content

Commit

Permalink
renderer: override color support logic to add colors on GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
zegl committed Jan 3, 2023
1 parent 74e551f commit c965b17
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
45 changes: 44 additions & 1 deletion cmd/kube-score/main.go
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"path/filepath"

"github.com/mattn/go-isatty"
flag "github.com/spf13/pflag"
"golang.org/x/term"

Expand Down Expand Up @@ -106,6 +107,7 @@ func scoreFiles(binName string, args []string) error {
printHelp := fs.Bool("help", false, "Print help")
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.")
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.")
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.")
optionalTests := fs.StringSlice("enable-optional-test", []string{}, "Enable an optional test, can be set multiple times")
ignoreTests := fs.StringSlice("ignore-test", []string{}, "Disable a test, can be set multiple times")
disableIgnoreChecksAnnotation := fs.Bool("disable-ignore-checks-annotations", false, "Set to true to disable the effect of the 'kube-score/ignore' annotations")
Expand All @@ -128,6 +130,16 @@ func scoreFiles(binName string, args []string) error {
return fmt.Errorf("Error: --output-format must be set to: 'human', 'json', 'sarif' or 'ci'")
}

acceptedColors := map[string]bool{
"auto": true,
"always": true,
"never": true,
}
if !acceptedColors[*color] {
fs.Usage()
return fmt.Errorf("Error: --color must be set to: 'auto', 'always' or 'never'")
}

filesToRead := fs.Args()
if len(filesToRead) == 0 {
return fmt.Errorf(`Error: No files given as arguments.
Expand Down Expand Up @@ -220,7 +232,7 @@ Use "-" as filename to read from STDIN.`, execName(binName))
if err != nil {
termWidth = 80
}
r, err = human.Human(scoreCard, *verboseOutput, termWidth)
r, err = human.Human(scoreCard, *verboseOutput, termWidth, useColor(*color))
if err != nil {
return err
}
Expand Down Expand Up @@ -299,3 +311,34 @@ type namedReader struct {
func (n namedReader) Name() string {
return n.name
}

func useColor(colorArg string) bool {
// Respect user preference
switch colorArg {
case "always":
return true
case "never":
return false
}

// If running on Github Actions, use colors
if _, ok := os.LookupEnv("GITHUB_ACTIONS"); ok {
return true
}

// If NO_COLOR is set, don't use color
if _, ok := os.LookupEnv("NO_COLOR"); ok {
return false
}

// Dont use color if not a terminal
if os.Getenv("TERM") == "dumb" {
return false
}
if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) {
return false
}

// Use colors
return true
}
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -19,7 +19,7 @@ require (
github.com/google/gofuzz v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Expand Up @@ -25,6 +25,8 @@ github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -65,6 +67,7 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.3.0 h1:qoo4akIqOcDME5bhc/NgxUdovd6BSS2uMsVjB56q1xI=
Expand Down
5 changes: 4 additions & 1 deletion renderer/human/human.go
Expand Up @@ -15,14 +15,17 @@ import (
"github.com/zegl/kube-score/scorecard"
)

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

// Override usage of colors to our own preference
color.NoColor = !useColors

w := bytes.NewBufferString("")

for _, key := range keys {
Expand Down

0 comments on commit c965b17

Please sign in to comment.