Skip to content

feat: new output format configuration #5440

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

Merged
merged 13 commits into from
Feb 19, 2025
Merged
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
107 changes: 68 additions & 39 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# This file is not a configuration example,
# it contains the exhaustive configuration with explanations of the options.

version: "2"

linters:
# Disable all linters.
# Default: false
Expand Down Expand Up @@ -4077,48 +4079,75 @@ issues:
fix: true


# output configuration options
# Output configuration options.
output:
# The formats used to render issues.
# Formats:
# - `colored-line-number`
# - `line-number`
# - `json`
# - `colored-tab`
# - `tab`
# - `html`
# - `checkstyle`
# - `code-climate`
# - `junit-xml`
# - `junit-xml-extended`
# - `github-actions`
# - `teamcity`
# - `sarif`
# Output path can be either `stdout`, `stderr` or path to the file to write to.
#
# For the CLI flag (`--out-format`), multiple formats can be specified by separating them by comma.
# The output can be specified for each of them by separating format name and path by colon symbol.
# Example: "--out-format=checkstyle:report.xml,json:stdout,colored-line-number"
# The CLI flag (`--out-format`) override the configuration file.
#
# Default:
# formats:
# - format: colored-line-number
# path: stdout
formats:
- format: json
path: stderr
- format: checkstyle
path: report.xml
- format: colored-line-number

# Print lines of code with issue.
# Default: true
print-issued-lines: false

# Print linter name in the end of issue text.
# Default: true
print-linter-name: false
# Prints issues in a text format with colors, line number, and linter name.
# This format is the default format.
text:
# Output path can be either `stdout`, `stderr` or path to the file to write to.
# Default: stdout
path: ./path/to/output.txt
# Print linter name in the end of issue text.
# Default: true
print-linter-name: false
# Print lines of code with issue.
# Default: true
print-issued-lines: false
# Use colors.
# Default: true
colors: false
# Prints issues in a JSON representation.
json:
# Output path can be either `stdout`, `stderr` or path to the file to write to.
# Default: stdout
path: ./path/to/output.json
# Prints issues in columns representation separated by tabulations.
tab:
# Output path can be either `stdout`, `stderr` or path to the file to write to.
# Default: stdout
path: ./path/to/output.txt
# Print linter name in the end of issue text.
# Default: true
print-linter-name: true
# Use colors.
# Default: true
colors: false
# Prints issues in an HTML page.
# It uses the Cloudflare CDN (cdnjs) and React.
html:
# Output path can be either `stdout`, `stderr` or path to the file to write to.
# Default: stdout
path: ./path/to/output.html
# Prints issues in the Checkstyle format.
checkstyle:
# Output path can be either `stdout`, `stderr` or path to the file to write to.
# Default: stdout
path: ./path/to/output.xml
# Prints issues in the Code Climate format.
code-climate:
# Output path can be either `stdout`, `stderr` or path to the file to write to.
# Default: stdout
path: ./path/to/output.json
# Prints issues in the JUnit XML format.
junit-xml:
# Output path can be either `stdout`, `stderr` or path to the file to write to.
# Default: stdout
path: ./path/to/output.xml
# Support extra JUnit XML fields.
# Default: false
extended: true
# Prints issues in the TeamCity format.
teamcity:
# Output path can be either `stdout`, `stderr` or path to the file to write to.
# Default: stdout
path: ./path/to/output.txt
# Prints issues in the SARIF format.
sarif:
# Output path can be either `stdout`, `stderr` or path to the file to write to.
# Default: stdout
path: ./path/to/output.json

# Add a prefix to the output file references.
# Default: ""
Expand Down
4 changes: 2 additions & 2 deletions docs/src/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Introduction
---

import { FaTwitter, FaSlack } from "react-icons/fa";
import { FaSlack } from "react-icons/fa";
import { IconContainer } from "lib/icons";

[![License](https://img.shields.io/github/license/golangci/golangci-lint)](https://github.com/golangci/golangci-lint/blob/HEAD/LICENSE)
Expand Down Expand Up @@ -30,7 +30,7 @@ Follow the news and releases:
- 🖥 [Integrations](/welcome/integrations) with VS Code, Sublime Text, GoLand, GNU Emacs, Vim, GitHub Actions.
- 🥇 [A lot of linters](/usage/linters) included, no need to install them.
- 📈 Minimum number of [false positives](/usage/false-positives) because of tuned default settings.
- 🔥 Nice output with colors, source code lines and marked `identifiers`.
- 🔥 Nice outputs: text with colors and source code lines, JSON, tab, HTML, Checkstyle, Code-Climate, JUnit-XML, TeamCity, SARIF.

[Get started now!](/welcome/install)

Expand Down
145 changes: 101 additions & 44 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,39 @@
"cfg",
"wd"
]
},
"simple-format": {
"type": "object",
"additionalProperties": false,
"properties": {
"path": {
"$ref": "#/definitions/formats-path",
"default": "stdout"
}
}
},
"formats-path" : {
"anyOf": [
{
"enum": [
"stdout",
"stderr"
]
},
{
"type": "string"
}
]
}
},
"type": "object",
"additionalProperties": false,
"required": ["version"],
"properties": {
"version": {
"type": "string",
"default": "2"
},
"run": {
"description": "Options for analysis running,",
"type": "object",
Expand Down Expand Up @@ -530,54 +558,83 @@
"properties": {
"formats": {
"description": "Output formats to use.",
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"path": {
"default": "stdout",
"anyOf": [
{
"enum": [ "stdout", "stderr" ]
},
{
"type": "string"
}
]
},
"format": {
"default": "colored-line-number",
"enum": [
"colored-line-number",
"line-number",
"json",
"colored-tab",
"tab",
"html",
"checkstyle",
"code-climate",
"junit-xml",
"junit-xml-extended",
"github-actions",
"teamcity",
"sarif"
]
"type": "object",
"additionalProperties": false,
"properties": {
"text": {
"type": "object",
"additionalProperties": false,
"properties": {
"path": {
"$ref": "#/definitions/formats-path",
"default": "stdout"
},
"print-linter-name": {
"type": "boolean",
"default": true
},
"print-issued-lines": {
"type": "boolean",
"default": true
},
"colors": {
"type": "boolean",
"default": true
}
}
},
"json": {
"$ref": "#/definitions/simple-format"
},
"tab": {
"type": "object",
"additionalProperties": false,
"properties": {
"path": {
"$ref": "#/definitions/formats-path",
"default": "stdout"
},
"print-linter-name": {
"type": "boolean",
"default": true
},
"colors": {
"type": "boolean",
"default": true
}
}
},
"required": ["format"]
"html": {
"$ref": "#/definitions/simple-format"
},
"checkstyle": {
"$ref": "#/definitions/simple-format"
},
"code-climate": {
"$ref": "#/definitions/simple-format"
},
"junit-xml": {
"type": "object",
"additionalProperties": false,
"properties": {
"path": {
"$ref": "#/definitions/formats-path",
"default": "stdout"
},
"extended": {
"type": "boolean",
"default": true
}
}
},
"teamcity": {
"$ref": "#/definitions/simple-format"
},
"sarif": {
"$ref": "#/definitions/simple-format"
}
}
},
"print-issued-lines": {
"description": "Print lines of code with issue.",
"type": "boolean",
"default": true
},
"print-linter-name": {
"description": "Print linter name in the end of issue text.",
"type": "boolean",
"default": true
},
"path-prefix": {
"description": "Add a prefix to the output file references.",
"type": "string",
Expand Down
53 changes: 47 additions & 6 deletions pkg/commands/flagsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,60 @@ func setupRunFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
}

func setupOutputFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
internal.AddFlagAndBind(v, fs, fs.String, "out-format", "output.formats", config.OutFormatColoredLineNumber,
formatList("Formats of output:", config.AllOutputFormats))
internal.AddFlagAndBind(v, fs, fs.Bool, "print-issued-lines", "output.print-issued-lines", true,
color.GreenString("Print lines of code with issue"))
internal.AddFlagAndBind(v, fs, fs.Bool, "print-linter-name", "output.print-linter-name", true,
color.GreenString("Print linter name in issue line"))
internal.AddFlagAndBind(v, fs, fs.Bool, "sort-results", "output.sort-results", false,
color.GreenString("Sort linter results"))
internal.AddFlagAndBind(v, fs, fs.StringSlice, "sort-order", "output.sort-order", nil,
color.GreenString("Sort order of linter results"))
internal.AddFlagAndBind(v, fs, fs.String, "path-prefix", "output.path-prefix", "",
color.GreenString("Path prefix to add to output"))
internal.AddFlagAndBind(v, fs, fs.Bool, "show-stats", "output.show-stats", false, color.GreenString("Show statistics per linter"))

setupOutputFormatsFlagSet(v, fs)
}

func setupOutputFormatsFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
outputPathDesc := "Output path can be either `stdout`, `stderr` or path to the file to write to."
printLinterNameDesc := "Print linter name in the end of issue text."
colorsDesc := "Use colors."

internal.AddFlagAndBind(v, fs, fs.String, "output.text.path", "output.formats.text.path", "",
color.GreenString(outputPathDesc))
internal.AddFlagAndBind(v, fs, fs.Bool, "output.text.print-linter-name", "output.formats.text.print-linter-name", true,
color.GreenString(printLinterNameDesc))
internal.AddFlagAndBind(v, fs, fs.Bool, "output.text.print-issued-lines", "output.formats.text.print-issued-lines", true,
color.GreenString("Print lines of code with issue."))
internal.AddFlagAndBind(v, fs, fs.Bool, "output.text.colors", "output.formats.text.colors", true,
color.GreenString(colorsDesc))

internal.AddFlagAndBind(v, fs, fs.String, "output.json.path", "output.formats.json.path", "",
color.GreenString(outputPathDesc))

internal.AddFlagAndBind(v, fs, fs.String, "output.tab.path", "output.formats.tab.path", "",
color.GreenString(outputPathDesc))
internal.AddFlagAndBind(v, fs, fs.Bool, "output.tab.print-linter-name", "output.formats.tab.print-linter-name",
true, color.GreenString(printLinterNameDesc))
internal.AddFlagAndBind(v, fs, fs.Bool, "output.tab.colors", "output.formats.tab.colors", true,
color.GreenString(colorsDesc))

internal.AddFlagAndBind(v, fs, fs.String, "output.html.path", "output.formats.html.path", "",
color.GreenString(outputPathDesc))

internal.AddFlagAndBind(v, fs, fs.String, "output.checkstyle.path", "output.formats.checkstyle.path", "",
color.GreenString(outputPathDesc))

internal.AddFlagAndBind(v, fs, fs.String, "output.code-climate.path", "output.formats.code-climate.path", "",
color.GreenString(outputPathDesc))

internal.AddFlagAndBind(v, fs, fs.String, "output.junit-xml.path", "output.formats.junit-xml.path", "",
color.GreenString(outputPathDesc))
internal.AddFlagAndBind(v, fs, fs.Bool, "output.junit-xml.extended", "output.formats.junit-xml.extended", false,
color.GreenString("Support extra JUnit XML fields."))

internal.AddFlagAndBind(v, fs, fs.String, "output.teamcity.path", "output.formats.teamcity.path", "",
color.GreenString(outputPathDesc))

internal.AddFlagAndBind(v, fs, fs.String, "output.sarif.path", "output.formats.sarif.path", "",
color.GreenString(outputPathDesc))
}

//nolint:gomnd // magic numbers here is ok
Expand Down
4 changes: 1 addition & 3 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,11 @@ func (c *runCommand) preRunE(_ *cobra.Command, args []string) error {

c.dbManager = dbManager

printer, err := printers.NewPrinter(c.log, &c.cfg.Output, c.reportData, c.cfg.GetBasePath())
c.printer, err = printers.NewPrinter(c.log, &c.cfg.Output.Formats, c.reportData, c.cfg.GetBasePath())
if err != nil {
return err
}

c.printer = printer

c.goenv = goutil.NewEnv(c.log.Child(logutils.DebugKeyGoEnv))

c.fileCache = fsutils.NewFileCache()
Expand Down
Loading
Loading