forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckstyle.go
93 lines (73 loc) · 1.79 KB
/
checkstyle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package printers
import (
"context"
"encoding/xml"
"fmt"
"io"
"github.com/go-xmlfmt/xmlfmt"
"github.com/golangci/golangci-lint/pkg/result"
)
type checkstyleOutput struct {
XMLName xml.Name `xml:"checkstyle"`
Version string `xml:"version,attr"`
Files []*checkstyleFile `xml:"file"`
}
type checkstyleFile struct {
Name string `xml:"name,attr"`
Errors []*checkstyleError `xml:"error"`
}
type checkstyleError struct {
Column int `xml:"column,attr"`
Line int `xml:"line,attr"`
Message string `xml:"message,attr"`
Severity string `xml:"severity,attr"`
Source string `xml:"source,attr"`
}
const defaultCheckstyleSeverity = "error"
type Checkstyle struct {
w io.Writer
}
func NewCheckstyle(w io.Writer) *Checkstyle {
return &Checkstyle{w: w}
}
func (p Checkstyle) Print(ctx context.Context, issues []result.Issue) error {
out := checkstyleOutput{
Version: "5.0",
}
files := map[string]*checkstyleFile{}
for i := range issues {
issue := &issues[i]
file, ok := files[issue.FilePath()]
if !ok {
file = &checkstyleFile{
Name: issue.FilePath(),
}
files[issue.FilePath()] = file
}
severity := defaultCheckstyleSeverity
if issue.Severity != "" {
severity = issue.Severity
}
newError := &checkstyleError{
Column: issue.Column(),
Line: issue.Line(),
Message: issue.Text,
Source: issue.FromLinter,
Severity: severity,
}
file.Errors = append(file.Errors, newError)
}
out.Files = make([]*checkstyleFile, 0, len(files))
for _, file := range files {
out.Files = append(out.Files, file)
}
data, err := xml.Marshal(&out)
if err != nil {
return err
}
_, err = fmt.Fprintf(p.w, "%s%s\n", xml.Header, xmlfmt.FormatXML(string(data), "", " "))
if err != nil {
return err
}
return nil
}