Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkstyle support #95

Merged
merged 4 commits into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from all 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, 0, len(files))
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