forked from alecthomas/gometalinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkstyle.go
61 lines (55 loc) · 1.35 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
package main
import (
"encoding/xml"
"fmt"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
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"`
}
func outputToCheckstyle(issues chan *Issue) int {
var lastFile *checkstyleFile
out := checkstyleOutput{
Version: "5.0",
}
status := 0
for issue := range issues {
if lastFile != nil && lastFile.Name != issue.Path {
out.Files = append(out.Files, lastFile)
lastFile = nil
}
if lastFile == nil {
lastFile = &checkstyleFile{
Name: issue.Path,
}
}
lastFile.Errors = append(lastFile.Errors, &checkstyleError{
Column: issue.Col,
Line: issue.Line,
Message: issue.Message,
Severity: string(issue.Severity),
Source: issue.Linter.Name,
})
status = 1
}
if lastFile != nil {
out.Files = append(out.Files, lastFile)
}
d, err := xml.Marshal(&out)
kingpin.FatalIfError(err, "")
fmt.Printf("%s%s\n", xml.Header, d)
return status
}