Skip to content

Commit

Permalink
lint/rpt: Handle no-color ttys (#1213)
Browse files Browse the repository at this point in the history
This fixes #1211 by supporting
the NO_COLOR environment variable and checking if the output is a tty
before using color.

The color.NoColor global is used as described here: https://github.com/fatih/color?tab=readme-ov-file#disableenable-color

Signed-off-by: Charlie Egan <[email protected]>
  • Loading branch information
charlieegan3 authored Oct 21, 2024
1 parent 63b90e1 commit 234e36b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ func buildPrettyViolationsTable(violations []report.Violation) string {
}

table.Append([]string{yellow("Rule:"), violation.Title})

// if there is no support for color, then we show the level in addition
// so that the level of the violation is still clear
if color.NoColor {
table.Append([]string{"Level:", violation.Level})
}

table.Append([]string{yellow("Description:"), description})
table.Append([]string{yellow("Category:"), violation.Category})
// End location ignored here as it's not too interesting in this format and line:column
Expand Down
1 change: 1 addition & 0 deletions pkg/reporter/testdata/pretty/reporter-long-text.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rule: long-violation
Level: warning
Description: violation with a long description
Category: long
Location: b.rego:22:18
Expand Down
2 changes: 2 additions & 0 deletions pkg/reporter/testdata/pretty/reporter.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Rule: breaking-the-law
Level: error
Description: Rego must not break the law!
Category: legal
Location: a.rego:1:1
Text: package illegal
Documentation: https://example.com/illegal

Rule: questionable-decision
Level: warning
Description: Questionable decision found
Category: really?
Location: b.rego:22:18
Expand Down

0 comments on commit 234e36b

Please sign in to comment.