Skip to content

Commit 219a547

Browse files
mxpvjirfag
authored andcommitted
Checkstyle support (#95)
Implement checkstyle printer
1 parent a1a9215 commit 219a547

File tree

5 files changed

+93
-2
lines changed

5 files changed

+93
-2
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ Usage:
231231
golangci-lint run [flags]
232232
233233
Flags:
234-
--out-format string Format of output: colored-line-number|line-number|json|tab (default "colored-line-number")
234+
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle (default "colored-line-number")
235235
--print-issued-lines Print lines of code with issue (default true)
236236
--print-linter-name Print linter name in issue line (default true)
237237
--issues-exit-code int Exit code when issues were found (default 1)

Diff for: pkg/commands/run.go

+2
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
256256
format == config.OutFormatColoredLineNumber, e.cfg.Output.PrintLinterName)
257257
case config.OutFormatTab:
258258
p = printers.NewTab(e.cfg.Output.PrintLinterName)
259+
case config.OutFormatCheckstyle:
260+
p = printers.NewCheckstyle()
259261
default:
260262
return fmt.Errorf("unknown output format %s", format)
261263
}

Diff for: pkg/config/config.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ const (
1111
OutFormatLineNumber = "line-number"
1212
OutFormatColoredLineNumber = "colored-line-number"
1313
OutFormatTab = "tab"
14+
OutFormatCheckstyle = "checkstyle"
1415
)
1516

16-
var OutFormats = []string{OutFormatColoredLineNumber, OutFormatLineNumber, OutFormatJSON, OutFormatTab}
17+
var OutFormats = []string{
18+
OutFormatColoredLineNumber,
19+
OutFormatLineNumber,
20+
OutFormatJSON,
21+
OutFormatTab,
22+
OutFormatCheckstyle,
23+
}
1724

1825
type ExcludePattern struct {
1926
Pattern string

Diff for: pkg/printers/checkstyle.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package printers
2+
3+
import (
4+
"context"
5+
"encoding/xml"
6+
"fmt"
7+
8+
"github.com/golangci/golangci-lint/pkg/result"
9+
)
10+
11+
type checkstyleOutput struct {
12+
XMLName xml.Name `xml:"checkstyle"`
13+
Version string `xml:"version,attr"`
14+
Files []*checkstyleFile `xml:"file"`
15+
}
16+
17+
type checkstyleFile struct {
18+
Name string `xml:"name,attr"`
19+
Errors []*checkstyleError `xml:"error"`
20+
}
21+
22+
type checkstyleError struct {
23+
Column int `xml:"column,attr"`
24+
Line int `xml:"line,attr"`
25+
Message string `xml:"message,attr"`
26+
Severity string `xml:"severity,attr"`
27+
Source string `xml:"source,attr"`
28+
}
29+
30+
const defaultSeverity = "error"
31+
32+
type Checkstyle struct{}
33+
34+
func NewCheckstyle() *Checkstyle {
35+
return &Checkstyle{}
36+
}
37+
38+
func (Checkstyle) Print(ctx context.Context, issues <-chan result.Issue) (bool, error) {
39+
out := checkstyleOutput{
40+
Version: "5.0",
41+
}
42+
43+
files := map[string]*checkstyleFile{}
44+
45+
for issue := range issues {
46+
file, ok := files[issue.FilePath()]
47+
if !ok {
48+
file = &checkstyleFile{
49+
Name: issue.FilePath(),
50+
}
51+
52+
files[issue.FilePath()] = file
53+
}
54+
55+
newError := &checkstyleError{
56+
Column: issue.Column(),
57+
Line: issue.Line(),
58+
Message: issue.Text,
59+
Source: issue.FromLinter,
60+
Severity: defaultSeverity,
61+
}
62+
63+
file.Errors = append(file.Errors, newError)
64+
}
65+
66+
out.Files = make([]*checkstyleFile, 0, len(files))
67+
for _, file := range files {
68+
out.Files = append(out.Files, file)
69+
}
70+
71+
data, err := xml.Marshal(&out)
72+
if err != nil {
73+
return false, err
74+
}
75+
76+
fmt.Fprintf(StdOut, "%s%s\n", xml.Header, data)
77+
return len(files) > 0, nil
78+
}

Diff for: pkg/result/issue.go

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ func (i Issue) Line() int {
2323
return i.Pos.Line
2424
}
2525

26+
func (i Issue) Column() int {
27+
return i.Pos.Column
28+
}
29+
2630
func (i Issue) GetLineRange() Range {
2731
if i.LineRange == nil {
2832
return Range{

0 commit comments

Comments
 (0)