From 2862ca630ca03d6071cee454c54046d0362a1707 Mon Sep 17 00:00:00 2001 From: Matthew Cobbing Date: Tue, 8 Jun 2021 00:33:40 +0100 Subject: [PATCH] output: generate HTML report (#2043) --- pkg/commands/run.go | 2 + pkg/config/output.go | 2 + pkg/printers/html.go | 155 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 pkg/printers/html.go diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 31a4e791..271fffe9 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -432,6 +432,8 @@ func (e *Executor) createPrinter() (printers.Printer, error) { p = printers.NewCheckstyle() case config.OutFormatCodeClimate: p = printers.NewCodeClimate() + case config.OutFormatHTML: + p = printers.NewHTML() case config.OutFormatJunitXML: p = printers.NewJunitXML() case config.OutFormatGithubActions: diff --git a/pkg/config/output.go b/pkg/config/output.go index c95d58fb..d67f110f 100644 --- a/pkg/config/output.go +++ b/pkg/config/output.go @@ -7,6 +7,7 @@ const ( OutFormatTab = "tab" OutFormatCheckstyle = "checkstyle" OutFormatCodeClimate = "code-climate" + OutFormatHTML = "html" OutFormatJunitXML = "junit-xml" OutFormatGithubActions = "github-actions" ) @@ -18,6 +19,7 @@ var OutFormats = []string{ OutFormatTab, OutFormatCheckstyle, OutFormatCodeClimate, + OutFormatHTML, OutFormatJunitXML, OutFormatGithubActions, } diff --git a/pkg/printers/html.go b/pkg/printers/html.go new file mode 100644 index 00000000..65ab753b --- /dev/null +++ b/pkg/printers/html.go @@ -0,0 +1,155 @@ +package printers + +import ( + "context" + "fmt" + "html/template" + "strings" + + "github.com/golangci/golangci-lint/pkg/logutils" + "github.com/golangci/golangci-lint/pkg/result" +) + +const templateContent = ` + + + + golangci-lint + + + + + + + + + + +
+
+
+
+
+ + + +` + +type htmlIssue struct { + Title string + Pos string + Linter string + Code string +} + +type HTML struct{} + +func NewHTML() *HTML { + return &HTML{} +} + +func (h HTML) Print(_ context.Context, issues []result.Issue) error { + var htmlIssues []htmlIssue + + for i := range issues { + pos := fmt.Sprintf("%s:%d", issues[i].FilePath(), issues[i].Line()) + if issues[i].Pos.Column != 0 { + pos += fmt.Sprintf(":%d", issues[i].Pos.Column) + } + + htmlIssues = append(htmlIssues, htmlIssue{ + Title: strings.TrimSpace(issues[i].Text), + Pos: pos, + Linter: issues[i].FromLinter, + Code: strings.Join(issues[i].SourceLines, "\n"), + }) + } + + t, err := template.New("golangci-lint").Parse(templateContent) + if err != nil { + return err + } + + return t.Execute(logutils.StdOut, struct{ Issues []htmlIssue }{Issues: htmlIssues}) +}