Skip to content

Commit 76d896a

Browse files
authored
feat: new output format configuration (#5440)
1 parent 814361e commit 76d896a

File tree

162 files changed

+758
-563
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+758
-563
lines changed

.golangci.next.reference.yml

+68-39
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# This file is not a configuration example,
55
# it contains the exhaustive configuration with explanations of the options.
66

7+
version: "2"
8+
79
linters:
810
# Disable all linters.
911
# Default: false
@@ -4077,48 +4079,75 @@ issues:
40774079
fix: true
40784080

40794081

4080-
# output configuration options
4082+
# Output configuration options.
40814083
output:
40824084
# The formats used to render issues.
4083-
# Formats:
4084-
# - `colored-line-number`
4085-
# - `line-number`
4086-
# - `json`
4087-
# - `colored-tab`
4088-
# - `tab`
4089-
# - `html`
4090-
# - `checkstyle`
4091-
# - `code-climate`
4092-
# - `junit-xml`
4093-
# - `junit-xml-extended`
4094-
# - `github-actions`
4095-
# - `teamcity`
4096-
# - `sarif`
4097-
# Output path can be either `stdout`, `stderr` or path to the file to write to.
4098-
#
4099-
# For the CLI flag (`--out-format`), multiple formats can be specified by separating them by comma.
4100-
# The output can be specified for each of them by separating format name and path by colon symbol.
4101-
# Example: "--out-format=checkstyle:report.xml,json:stdout,colored-line-number"
4102-
# The CLI flag (`--out-format`) override the configuration file.
4103-
#
4104-
# Default:
4105-
# formats:
4106-
# - format: colored-line-number
4107-
# path: stdout
41084085
formats:
4109-
- format: json
4110-
path: stderr
4111-
- format: checkstyle
4112-
path: report.xml
4113-
- format: colored-line-number
4114-
4115-
# Print lines of code with issue.
4116-
# Default: true
4117-
print-issued-lines: false
4118-
4119-
# Print linter name in the end of issue text.
4120-
# Default: true
4121-
print-linter-name: false
4086+
# Prints issues in a text format with colors, line number, and linter name.
4087+
# This format is the default format.
4088+
text:
4089+
# Output path can be either `stdout`, `stderr` or path to the file to write to.
4090+
# Default: stdout
4091+
path: ./path/to/output.txt
4092+
# Print linter name in the end of issue text.
4093+
# Default: true
4094+
print-linter-name: false
4095+
# Print lines of code with issue.
4096+
# Default: true
4097+
print-issued-lines: false
4098+
# Use colors.
4099+
# Default: true
4100+
colors: false
4101+
# Prints issues in a JSON representation.
4102+
json:
4103+
# Output path can be either `stdout`, `stderr` or path to the file to write to.
4104+
# Default: stdout
4105+
path: ./path/to/output.json
4106+
# Prints issues in columns representation separated by tabulations.
4107+
tab:
4108+
# Output path can be either `stdout`, `stderr` or path to the file to write to.
4109+
# Default: stdout
4110+
path: ./path/to/output.txt
4111+
# Print linter name in the end of issue text.
4112+
# Default: true
4113+
print-linter-name: true
4114+
# Use colors.
4115+
# Default: true
4116+
colors: false
4117+
# Prints issues in an HTML page.
4118+
# It uses the Cloudflare CDN (cdnjs) and React.
4119+
html:
4120+
# Output path can be either `stdout`, `stderr` or path to the file to write to.
4121+
# Default: stdout
4122+
path: ./path/to/output.html
4123+
# Prints issues in the Checkstyle format.
4124+
checkstyle:
4125+
# Output path can be either `stdout`, `stderr` or path to the file to write to.
4126+
# Default: stdout
4127+
path: ./path/to/output.xml
4128+
# Prints issues in the Code Climate format.
4129+
code-climate:
4130+
# Output path can be either `stdout`, `stderr` or path to the file to write to.
4131+
# Default: stdout
4132+
path: ./path/to/output.json
4133+
# Prints issues in the JUnit XML format.
4134+
junit-xml:
4135+
# Output path can be either `stdout`, `stderr` or path to the file to write to.
4136+
# Default: stdout
4137+
path: ./path/to/output.xml
4138+
# Support extra JUnit XML fields.
4139+
# Default: false
4140+
extended: true
4141+
# Prints issues in the TeamCity format.
4142+
teamcity:
4143+
# Output path can be either `stdout`, `stderr` or path to the file to write to.
4144+
# Default: stdout
4145+
path: ./path/to/output.txt
4146+
# Prints issues in the SARIF format.
4147+
sarif:
4148+
# Output path can be either `stdout`, `stderr` or path to the file to write to.
4149+
# Default: stdout
4150+
path: ./path/to/output.json
41224151

41234152
# Add a prefix to the output file references.
41244153
# Default: ""

docs/src/docs/index.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Introduction
33
---
44

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

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

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

jsonschema/golangci.next.jsonschema.json

+101-44
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,39 @@
454454
"cfg",
455455
"wd"
456456
]
457+
},
458+
"simple-format": {
459+
"type": "object",
460+
"additionalProperties": false,
461+
"properties": {
462+
"path": {
463+
"$ref": "#/definitions/formats-path",
464+
"default": "stdout"
465+
}
466+
}
467+
},
468+
"formats-path" : {
469+
"anyOf": [
470+
{
471+
"enum": [
472+
"stdout",
473+
"stderr"
474+
]
475+
},
476+
{
477+
"type": "string"
478+
}
479+
]
457480
}
458481
},
459482
"type": "object",
460483
"additionalProperties": false,
484+
"required": ["version"],
461485
"properties": {
486+
"version": {
487+
"type": "string",
488+
"default": "2"
489+
},
462490
"run": {
463491
"description": "Options for analysis running,",
464492
"type": "object",
@@ -530,54 +558,83 @@
530558
"properties": {
531559
"formats": {
532560
"description": "Output formats to use.",
533-
"type": "array",
534-
"items": {
535-
"type": "object",
536-
"additionalProperties": false,
537-
"properties": {
538-
"path": {
539-
"default": "stdout",
540-
"anyOf": [
541-
{
542-
"enum": [ "stdout", "stderr" ]
543-
},
544-
{
545-
"type": "string"
546-
}
547-
]
548-
},
549-
"format": {
550-
"default": "colored-line-number",
551-
"enum": [
552-
"colored-line-number",
553-
"line-number",
554-
"json",
555-
"colored-tab",
556-
"tab",
557-
"html",
558-
"checkstyle",
559-
"code-climate",
560-
"junit-xml",
561-
"junit-xml-extended",
562-
"github-actions",
563-
"teamcity",
564-
"sarif"
565-
]
561+
"type": "object",
562+
"additionalProperties": false,
563+
"properties": {
564+
"text": {
565+
"type": "object",
566+
"additionalProperties": false,
567+
"properties": {
568+
"path": {
569+
"$ref": "#/definitions/formats-path",
570+
"default": "stdout"
571+
},
572+
"print-linter-name": {
573+
"type": "boolean",
574+
"default": true
575+
},
576+
"print-issued-lines": {
577+
"type": "boolean",
578+
"default": true
579+
},
580+
"colors": {
581+
"type": "boolean",
582+
"default": true
583+
}
584+
}
585+
},
586+
"json": {
587+
"$ref": "#/definitions/simple-format"
588+
},
589+
"tab": {
590+
"type": "object",
591+
"additionalProperties": false,
592+
"properties": {
593+
"path": {
594+
"$ref": "#/definitions/formats-path",
595+
"default": "stdout"
596+
},
597+
"print-linter-name": {
598+
"type": "boolean",
599+
"default": true
600+
},
601+
"colors": {
602+
"type": "boolean",
603+
"default": true
604+
}
566605
}
567606
},
568-
"required": ["format"]
607+
"html": {
608+
"$ref": "#/definitions/simple-format"
609+
},
610+
"checkstyle": {
611+
"$ref": "#/definitions/simple-format"
612+
},
613+
"code-climate": {
614+
"$ref": "#/definitions/simple-format"
615+
},
616+
"junit-xml": {
617+
"type": "object",
618+
"additionalProperties": false,
619+
"properties": {
620+
"path": {
621+
"$ref": "#/definitions/formats-path",
622+
"default": "stdout"
623+
},
624+
"extended": {
625+
"type": "boolean",
626+
"default": true
627+
}
628+
}
629+
},
630+
"teamcity": {
631+
"$ref": "#/definitions/simple-format"
632+
},
633+
"sarif": {
634+
"$ref": "#/definitions/simple-format"
635+
}
569636
}
570637
},
571-
"print-issued-lines": {
572-
"description": "Print lines of code with issue.",
573-
"type": "boolean",
574-
"default": true
575-
},
576-
"print-linter-name": {
577-
"description": "Print linter name in the end of issue text.",
578-
"type": "boolean",
579-
"default": true
580-
},
581638
"path-prefix": {
582639
"description": "Add a prefix to the output file references.",
583640
"type": "string",

pkg/commands/flagsets.go

+47-6
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,60 @@ func setupRunFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
7272
}
7373

7474
func setupOutputFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
75-
internal.AddFlagAndBind(v, fs, fs.String, "out-format", "output.formats", config.OutFormatColoredLineNumber,
76-
formatList("Formats of output:", config.AllOutputFormats))
77-
internal.AddFlagAndBind(v, fs, fs.Bool, "print-issued-lines", "output.print-issued-lines", true,
78-
color.GreenString("Print lines of code with issue"))
79-
internal.AddFlagAndBind(v, fs, fs.Bool, "print-linter-name", "output.print-linter-name", true,
80-
color.GreenString("Print linter name in issue line"))
8175
internal.AddFlagAndBind(v, fs, fs.Bool, "sort-results", "output.sort-results", false,
8276
color.GreenString("Sort linter results"))
8377
internal.AddFlagAndBind(v, fs, fs.StringSlice, "sort-order", "output.sort-order", nil,
8478
color.GreenString("Sort order of linter results"))
8579
internal.AddFlagAndBind(v, fs, fs.String, "path-prefix", "output.path-prefix", "",
8680
color.GreenString("Path prefix to add to output"))
8781
internal.AddFlagAndBind(v, fs, fs.Bool, "show-stats", "output.show-stats", false, color.GreenString("Show statistics per linter"))
82+
83+
setupOutputFormatsFlagSet(v, fs)
84+
}
85+
86+
func setupOutputFormatsFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
87+
outputPathDesc := "Output path can be either `stdout`, `stderr` or path to the file to write to."
88+
printLinterNameDesc := "Print linter name in the end of issue text."
89+
colorsDesc := "Use colors."
90+
91+
internal.AddFlagAndBind(v, fs, fs.String, "output.text.path", "output.formats.text.path", "",
92+
color.GreenString(outputPathDesc))
93+
internal.AddFlagAndBind(v, fs, fs.Bool, "output.text.print-linter-name", "output.formats.text.print-linter-name", true,
94+
color.GreenString(printLinterNameDesc))
95+
internal.AddFlagAndBind(v, fs, fs.Bool, "output.text.print-issued-lines", "output.formats.text.print-issued-lines", true,
96+
color.GreenString("Print lines of code with issue."))
97+
internal.AddFlagAndBind(v, fs, fs.Bool, "output.text.colors", "output.formats.text.colors", true,
98+
color.GreenString(colorsDesc))
99+
100+
internal.AddFlagAndBind(v, fs, fs.String, "output.json.path", "output.formats.json.path", "",
101+
color.GreenString(outputPathDesc))
102+
103+
internal.AddFlagAndBind(v, fs, fs.String, "output.tab.path", "output.formats.tab.path", "",
104+
color.GreenString(outputPathDesc))
105+
internal.AddFlagAndBind(v, fs, fs.Bool, "output.tab.print-linter-name", "output.formats.tab.print-linter-name",
106+
true, color.GreenString(printLinterNameDesc))
107+
internal.AddFlagAndBind(v, fs, fs.Bool, "output.tab.colors", "output.formats.tab.colors", true,
108+
color.GreenString(colorsDesc))
109+
110+
internal.AddFlagAndBind(v, fs, fs.String, "output.html.path", "output.formats.html.path", "",
111+
color.GreenString(outputPathDesc))
112+
113+
internal.AddFlagAndBind(v, fs, fs.String, "output.checkstyle.path", "output.formats.checkstyle.path", "",
114+
color.GreenString(outputPathDesc))
115+
116+
internal.AddFlagAndBind(v, fs, fs.String, "output.code-climate.path", "output.formats.code-climate.path", "",
117+
color.GreenString(outputPathDesc))
118+
119+
internal.AddFlagAndBind(v, fs, fs.String, "output.junit-xml.path", "output.formats.junit-xml.path", "",
120+
color.GreenString(outputPathDesc))
121+
internal.AddFlagAndBind(v, fs, fs.Bool, "output.junit-xml.extended", "output.formats.junit-xml.extended", false,
122+
color.GreenString("Support extra JUnit XML fields."))
123+
124+
internal.AddFlagAndBind(v, fs, fs.String, "output.teamcity.path", "output.formats.teamcity.path", "",
125+
color.GreenString(outputPathDesc))
126+
127+
internal.AddFlagAndBind(v, fs, fs.String, "output.sarif.path", "output.formats.sarif.path", "",
128+
color.GreenString(outputPathDesc))
88129
}
89130

90131
//nolint:gomnd // magic numbers here is ok

pkg/commands/run.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,11 @@ func (c *runCommand) preRunE(_ *cobra.Command, args []string) error {
194194

195195
c.dbManager = dbManager
196196

197-
printer, err := printers.NewPrinter(c.log, &c.cfg.Output, c.reportData, c.cfg.GetBasePath())
197+
c.printer, err = printers.NewPrinter(c.log, &c.cfg.Output.Formats, c.reportData, c.cfg.GetBasePath())
198198
if err != nil {
199199
return err
200200
}
201201

202-
c.printer = printer
203-
204202
c.goenv = goutil.NewEnv(c.log.Child(logutils.DebugKeyGoEnv))
205203

206204
c.fileCache = fsutils.NewFileCache()

0 commit comments

Comments
 (0)