|
| 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 | +} |
0 commit comments