Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jan 4, 2022
1 parent da5c595 commit fefa146
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .golangci.example.yml
Expand Up @@ -63,8 +63,8 @@ output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions
# default is "colored-line-number"
# multiple can be specified by separating them by comma, output can be provided
# for each of them by separating format name and path by colon symbol. Output path can
# be either `stdout`, `stderr` or path to the file to write to.
# for each of them by separating format name and path by colon symbol.
# Output path can be either `stdout`, `stderr` or path to the file to write to.
# Example "checkstyle:report.json,colored-line-number"
format: colored-line-number

Expand Down
50 changes: 31 additions & 19 deletions pkg/commands/run.go
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/golangci/golangci-lint/pkg/result/processors"
)

const defaultFileMode = 0644

func getDefaultIssueExcludeHelp() string {
parts := []string{"Use or not use default excludes:"}
for _, ep := range config.DefaultExcludePatterns {
Expand Down Expand Up @@ -381,8 +383,6 @@ func (e *Executor) setExitCodeIfIssuesFound(issues []result.Issue) {
}
}

const defaultFileMode = 0644

func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
if err := e.goenv.Discover(ctx); err != nil {
e.log.Warnf("Failed to discover go env: %s", err)
Expand All @@ -408,27 +408,11 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
if len(out) < 2 {
out = append(out, "")
}
w, shouldClose, err := e.createWriter(out[1])
if err != nil {
return fmt.Errorf("can't create output for %s: %w", out[1], err)
}

p, err := e.createPrinter(out[0], w)
err := e.printReports(ctx, issues, out[1], out[0])
if err != nil {
if file, ok := w.(io.Closer); shouldClose && ok {
file.Close()
}
return err
}
if err = p.Print(ctx, issues); err != nil {
if file, ok := w.(io.Closer); shouldClose && ok {
file.Close()
}
return fmt.Errorf("can't print %d issues: %s", len(issues), err)
}
if file, ok := w.(io.Closer); shouldClose && ok {
file.Close()
}
}

e.setExitCodeIfIssuesFound(issues)
Expand All @@ -438,6 +422,34 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
return nil
}

func (e *Executor) printReports(ctx context.Context, issues []result.Issue, path, format string) error {
w, shouldClose, err := e.createWriter(path)
if err != nil {
return fmt.Errorf("can't create output for %s: %w", path, err)
}

p, err := e.createPrinter(format, w)
if err != nil {
if file, ok := w.(io.Closer); shouldClose && ok {
_ = file.Close()
}
return err
}

if err = p.Print(ctx, issues); err != nil {
if file, ok := w.(io.Closer); shouldClose && ok {
_ = file.Close()
}
return fmt.Errorf("can't print %d issues: %s", len(issues), err)
}

if file, ok := w.(io.Closer); shouldClose && ok {
_ = file.Close()
}

return nil
}

func (e *Executor) createWriter(path string) (io.Writer, bool, error) {
if path == "" || path == "stdout" {
return logutils.StdOut, false, nil
Expand Down
10 changes: 6 additions & 4 deletions pkg/printers/checkstyle.go
Expand Up @@ -37,9 +37,7 @@ type Checkstyle struct {
}

func NewCheckstyle(w io.Writer) *Checkstyle {
return &Checkstyle{
w: w,
}
return &Checkstyle{w: w}
}

func (p Checkstyle) Print(ctx context.Context, issues []result.Issue) error {
Expand Down Expand Up @@ -86,6 +84,10 @@ func (p Checkstyle) Print(ctx context.Context, issues []result.Issue) error {
return err
}

fmt.Fprintf(p.w, "%s%s\n", xml.Header, xmlfmt.FormatXML(string(data), "", " "))
_, err = fmt.Fprintf(p.w, "%s%s\n", xml.Header, xmlfmt.FormatXML(string(data), "", " "))
if err != nil {
return err
}

return nil
}
9 changes: 5 additions & 4 deletions pkg/printers/codeclimate.go
Expand Up @@ -28,9 +28,7 @@ type CodeClimate struct {
}

func NewCodeClimate(w io.Writer) *CodeClimate {
return &CodeClimate{
w: w,
}
return &CodeClimate{w: w}
}

func (p CodeClimate) Print(ctx context.Context, issues []result.Issue) error {
Expand All @@ -55,6 +53,9 @@ func (p CodeClimate) Print(ctx context.Context, issues []result.Issue) error {
return err
}

fmt.Fprint(p.w, string(outputJSON))
_, err = fmt.Fprint(p.w, string(outputJSON))
if err != nil {
return err
}
return nil
}
4 changes: 1 addition & 3 deletions pkg/printers/github.go
Expand Up @@ -17,9 +17,7 @@ const defaultGithubSeverity = "error"
// NewGithub output format outputs issues according to GitHub actions format:
// https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
func NewGithub(w io.Writer) Printer {
return &github{
w: w,
}
return &github{w: w}
}

// print each line as: ::error file=app.js,line=10,col=15::Something went wrong
Expand Down
4 changes: 1 addition & 3 deletions pkg/printers/html.go
Expand Up @@ -128,9 +128,7 @@ type HTML struct {
}

func NewHTML(w io.Writer) *HTML {
return &HTML{
w: w,
}
return &HTML{w: w}
}

func (p HTML) Print(_ context.Context, issues []result.Issue) error {
Expand Down
9 changes: 1 addition & 8 deletions pkg/printers/json.go
Expand Up @@ -3,7 +3,6 @@ package printers
import (
"context"
"encoding/json"
"fmt"
"io"

"github.com/golangci/golangci-lint/pkg/report"
Expand Down Expand Up @@ -36,11 +35,5 @@ func (p JSON) Print(ctx context.Context, issues []result.Issue) error {
res.Issues = []result.Issue{}
}

outputJSON, err := json.Marshal(res)
if err != nil {
return err
}

fmt.Fprint(p.w, string(outputJSON))
return nil
return json.NewEncoder(p.w).Encode(res)
}
4 changes: 1 addition & 3 deletions pkg/printers/junitxml.go
Expand Up @@ -39,9 +39,7 @@ type JunitXML struct {
}

func NewJunitXML(w io.Writer) *JunitXML {
return &JunitXML{
w: w,
}
return &JunitXML{w: w}
}

func (p JunitXML) Print(ctx context.Context, issues []result.Issue) error {
Expand Down

0 comments on commit fefa146

Please sign in to comment.