From deacdacd7f2cfaee7f8e0e9e01da72f8954b355b Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Tue, 12 Jun 2018 20:52:22 -0700 Subject: [PATCH 1/4] Add checkstyle stub --- pkg/commands/run.go | 2 ++ pkg/config/config.go | 9 ++++++++- pkg/printers/checkstyle.go | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 pkg/printers/checkstyle.go diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 92d417e6d32d..d9e50947247b 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -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) } diff --git a/pkg/config/config.go b/pkg/config/config.go index c2e4ad7204ba..261a9f8dfa87 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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 diff --git a/pkg/printers/checkstyle.go b/pkg/printers/checkstyle.go new file mode 100644 index 000000000000..a6af5ce71ac7 --- /dev/null +++ b/pkg/printers/checkstyle.go @@ -0,0 +1,17 @@ +package printers + +import ( + "context" + + "github.com/golangci/golangci-lint/pkg/result" +) + +type Checkstyle struct{} + +func NewCheckstyle() *Checkstyle { + return &Checkstyle{} +} + +func (Checkstyle) Print(ctx context.Context, issues <-chan result.Issue) (bool, error) { + return false, nil +} From 937768b0f93822c76ddfbbb9b65ee45a42ccdb69 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Tue, 12 Jun 2018 21:32:44 -0700 Subject: [PATCH 2/4] Implement checkstyle printer --- pkg/printers/checkstyle.go | 63 +++++++++++++++++++++++++++++++++++++- pkg/result/issue.go | 4 +++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/pkg/printers/checkstyle.go b/pkg/printers/checkstyle.go index a6af5ce71ac7..e85ff6f7a39f 100644 --- a/pkg/printers/checkstyle.go +++ b/pkg/printers/checkstyle.go @@ -2,10 +2,33 @@ 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 { @@ -13,5 +36,43 @@ func NewCheckstyle() *Checkstyle { } func (Checkstyle) Print(ctx context.Context, issues <-chan result.Issue) (bool, error) { - return false, nil + 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)) + 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 } diff --git a/pkg/result/issue.go b/pkg/result/issue.go index 4daa4e17281d..62c951472c0c 100644 --- a/pkg/result/issue.go +++ b/pkg/result/issue.go @@ -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{ From 6017ca5570cba782e63584fd1ad110cade5ef2dd Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Tue, 12 Jun 2018 21:43:30 -0700 Subject: [PATCH 3/4] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 83eee5470746..06ac8954320e 100644 --- a/README.md +++ b/README.md @@ -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) From d5e1ca54e80df908abbb4cef5d1fc09ae9f38820 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Wed, 13 Jun 2018 10:44:44 -0700 Subject: [PATCH 4/4] Code review fix --- pkg/printers/checkstyle.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/printers/checkstyle.go b/pkg/printers/checkstyle.go index e85ff6f7a39f..587b6f9fd673 100644 --- a/pkg/printers/checkstyle.go +++ b/pkg/printers/checkstyle.go @@ -63,7 +63,7 @@ func (Checkstyle) Print(ctx context.Context, issues <-chan result.Issue) (bool, file.Errors = append(file.Errors, newError) } - out.Files = make([]*checkstyleFile, len(files)) + out.Files = make([]*checkstyleFile, 0, len(files)) for _, file := range files { out.Files = append(out.Files, file) }