Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ Usage:
golangci-lint run [flags]

Flags:
--out-format string Format of output: colored-line-number|line-number|json|tab (default "colored-line-number")
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle (default "colored-line-number")
--print-issued-lines Print lines of code with issue (default true)
--print-linter-name Print linter name in issue line (default true)
--issues-exit-code int Exit code when issues were found (default 1)
Expand Down
2 changes: 2 additions & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
format == config.OutFormatColoredLineNumber, e.cfg.Output.PrintLinterName)
case config.OutFormatTab:
p = printers.NewTab(e.cfg.Output.PrintLinterName)
case config.OutFormatCheckstyle:
p = printers.NewCheckstyle()
default:
return fmt.Errorf("unknown output format %s", format)
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ const (
OutFormatLineNumber = "line-number"
OutFormatColoredLineNumber = "colored-line-number"
OutFormatTab = "tab"
OutFormatCheckstyle = "checkstyle"
)

var OutFormats = []string{OutFormatColoredLineNumber, OutFormatLineNumber, OutFormatJSON, OutFormatTab}
var OutFormats = []string{
OutFormatColoredLineNumber,
OutFormatLineNumber,
OutFormatJSON,
OutFormatTab,
OutFormatCheckstyle,
}

type ExcludePattern struct {
Pattern string
Expand Down
78 changes: 78 additions & 0 deletions pkg/printers/checkstyle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package printers

import (
"context"
"encoding/xml"
"fmt"

"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 defaultSeverity = "error"

type Checkstyle struct{}

func NewCheckstyle() *Checkstyle {
return &Checkstyle{}
}

func (Checkstyle) Print(ctx context.Context, issues <-chan result.Issue) (bool, error) {
out := checkstyleOutput{
Version: "5.0",
}

files := map[string]*checkstyleFile{}

for issue := range issues {
file, ok := files[issue.FilePath()]
if !ok {
file = &checkstyleFile{
Name: issue.FilePath(),
}

files[issue.FilePath()] = file
}

newError := &checkstyleError{
Column: issue.Column(),
Line: issue.Line(),
Message: issue.Text,
Source: issue.FromLinter,
Severity: defaultSeverity,
}

file.Errors = append(file.Errors, newError)
}

out.Files = make([]*checkstyleFile, len(files))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe make([]*checkstyleFile, 0, len(files))?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of syntax is not supported for maps.
It'll give an error too many arguments to make(map)
https://play.golang.org/p/b7b5e8jgEVp

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it’s a slice, not map

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, crap. yes, you're right :)

for _, file := range files {
out.Files = append(out.Files, file)
}

data, err := xml.Marshal(&out)
if err != nil {
return false, err
}

fmt.Fprintf(StdOut, "%s%s\n", xml.Header, data)
return len(files) > 0, nil
}
4 changes: 4 additions & 0 deletions pkg/result/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (i Issue) Line() int {
return i.Pos.Line
}

func (i Issue) Column() int {
return i.Pos.Column
}

func (i Issue) GetLineRange() Range {
if i.LineRange == nil {
return Range{
Expand Down