Skip to content

feat(severity): color warning severity, non-zero exit when issue != default severity #2595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 8 additions & 3 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,13 @@ func (e *Executor) setOutputToDevNull() (savedStdout, savedStderr *os.File) {
}

func (e *Executor) setExitCodeIfIssuesFound(issues []result.Issue) {
if len(issues) != 0 {
e.exitCode = e.cfg.Run.ExitCodeIfIssuesFound
for i := range issues {
// If we find an issue with a severity equal to the default (non-modified)
// then we exit with the normal issues exit code.
if issues[i].Severity == e.cfg.Severity.Default {
e.exitCode = e.cfg.Run.ExitCodeIfIssuesFound
break
}
}
}

Expand Down Expand Up @@ -471,7 +476,7 @@ func (e *Executor) createPrinter(format string, w io.Writer) (printers.Printer,
p = printers.NewJSON(&e.reportData, w)
case config.OutFormatColoredLineNumber, config.OutFormatLineNumber:
p = printers.NewText(e.cfg.Output.PrintIssuedLine,
format == config.OutFormatColoredLineNumber, e.cfg.Output.PrintLinterName,
format == config.OutFormatColoredLineNumber, e.cfg.Output.PrintLinterName, e.cfg.Output.PrintSeverity,
e.log.Child("text_printer"), w)
case config.OutFormatTab:
p = printers.NewTab(e.cfg.Output.PrintLinterName, e.log.Child("tab_printer"), w)
Expand Down
1 change: 1 addition & 0 deletions pkg/config/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Output struct {
Color string
PrintIssuedLine bool `mapstructure:"print-issued-lines"`
PrintLinterName bool `mapstructure:"print-linter-name"`
PrintSeverity bool `mapstructure:"print-severity"`
Copy link
Author

@jaredallard jaredallard Feb 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like this print-severity field in general, but I want to try to maintain older behaviour if possible. I was thinking of adding something like exit-non-zero-severity to replace the default severity field usage in this PR. Still need to look at this codebase more to figure out how that's implemented.

UniqByLine bool `mapstructure:"uniq-by-line"`
SortResults bool `mapstructure:"sort-results"`
PrintWelcomeMessage bool `mapstructure:"print-welcome"`
Expand Down
14 changes: 12 additions & 2 deletions pkg/printers/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ import (

type Text struct {
printIssuedLine bool
printSeverity bool
useColors bool
printLinterName bool

log logutils.Log
w io.Writer
}

func NewText(printIssuedLine, useColors, printLinterName bool, log logutils.Log, w io.Writer) *Text {
func NewText(printIssuedLine, useColors, printLinterName, printSeverity bool, log logutils.Log, w io.Writer) *Text {
return &Text{
printIssuedLine: printIssuedLine,
printSeverity: printSeverity,
useColors: useColors,
printLinterName: printLinterName,
log: log,
Expand Down Expand Up @@ -56,10 +58,18 @@ func (p *Text) Print(ctx context.Context, issues []result.Issue) error {
}

func (p Text) printIssue(i *result.Issue) {
text := p.SprintfColored(color.FgRed, "%s", strings.TrimSpace(i.Text))
issueColor := color.FgRed
if i.Severity == "warning" {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure this isn't great because this seems to attach to specific severity levels -- which I don't see well defined in this repository. Let me know if we want to define coloring a different way, if at all. I like this, personally.

issueColor = color.FgYellow
}

text := p.SprintfColored(issueColor, "%s", strings.TrimSpace(i.Text))
if p.printLinterName {
text += fmt.Sprintf(" (%s)", i.FromLinter)
}
if p.printSeverity {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only time I really found this useful is if coloring is turned off, so I'm happy to remove it / restructure this.

text += fmt.Sprintf(" %s", i.Severity)
}
pos := p.SprintfColored(color.Bold, "%s:%d", i.FilePath(), i.Line())
if i.Pos.Column != 0 {
pos += fmt.Sprintf(":%d", i.Pos.Column)
Expand Down
2 changes: 1 addition & 1 deletion pkg/printers/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestText_Print(t *testing.T) {

buf := new(bytes.Buffer)

printer := NewText(true, false, true, logutils.NewStderrLog(""), buf)
printer := NewText(true, false, true, false, logutils.NewStderrLog(""), buf)

err := printer.Print(context.Background(), issues)
require.NoError(t, err)
Expand Down