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