Skip to content

Commit fefa146

Browse files
committed
review
1 parent da5c595 commit fefa146

File tree

8 files changed

+48
-46
lines changed

8 files changed

+48
-46
lines changed

.golangci.example.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ output:
6363
# colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions
6464
# default is "colored-line-number"
6565
# multiple can be specified by separating them by comma, output can be provided
66-
# for each of them by separating format name and path by colon symbol. Output path can
67-
# be either `stdout`, `stderr` or path to the file to write to.
66+
# for each of them by separating format name and path by colon symbol.
67+
# Output path can be either `stdout`, `stderr` or path to the file to write to.
6868
# Example "checkstyle:report.json,colored-line-number"
6969
format: colored-line-number
7070

pkg/commands/run.go

+31-19
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"github.com/golangci/golangci-lint/pkg/result/processors"
2727
)
2828

29+
const defaultFileMode = 0644
30+
2931
func getDefaultIssueExcludeHelp() string {
3032
parts := []string{"Use or not use default excludes:"}
3133
for _, ep := range config.DefaultExcludePatterns {
@@ -381,8 +383,6 @@ func (e *Executor) setExitCodeIfIssuesFound(issues []result.Issue) {
381383
}
382384
}
383385

384-
const defaultFileMode = 0644
385-
386386
func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
387387
if err := e.goenv.Discover(ctx); err != nil {
388388
e.log.Warnf("Failed to discover go env: %s", err)
@@ -408,27 +408,11 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
408408
if len(out) < 2 {
409409
out = append(out, "")
410410
}
411-
w, shouldClose, err := e.createWriter(out[1])
412-
if err != nil {
413-
return fmt.Errorf("can't create output for %s: %w", out[1], err)
414-
}
415411

416-
p, err := e.createPrinter(out[0], w)
412+
err := e.printReports(ctx, issues, out[1], out[0])
417413
if err != nil {
418-
if file, ok := w.(io.Closer); shouldClose && ok {
419-
file.Close()
420-
}
421414
return err
422415
}
423-
if err = p.Print(ctx, issues); err != nil {
424-
if file, ok := w.(io.Closer); shouldClose && ok {
425-
file.Close()
426-
}
427-
return fmt.Errorf("can't print %d issues: %s", len(issues), err)
428-
}
429-
if file, ok := w.(io.Closer); shouldClose && ok {
430-
file.Close()
431-
}
432416
}
433417

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

425+
func (e *Executor) printReports(ctx context.Context, issues []result.Issue, path, format string) error {
426+
w, shouldClose, err := e.createWriter(path)
427+
if err != nil {
428+
return fmt.Errorf("can't create output for %s: %w", path, err)
429+
}
430+
431+
p, err := e.createPrinter(format, w)
432+
if err != nil {
433+
if file, ok := w.(io.Closer); shouldClose && ok {
434+
_ = file.Close()
435+
}
436+
return err
437+
}
438+
439+
if err = p.Print(ctx, issues); err != nil {
440+
if file, ok := w.(io.Closer); shouldClose && ok {
441+
_ = file.Close()
442+
}
443+
return fmt.Errorf("can't print %d issues: %s", len(issues), err)
444+
}
445+
446+
if file, ok := w.(io.Closer); shouldClose && ok {
447+
_ = file.Close()
448+
}
449+
450+
return nil
451+
}
452+
441453
func (e *Executor) createWriter(path string) (io.Writer, bool, error) {
442454
if path == "" || path == "stdout" {
443455
return logutils.StdOut, false, nil

pkg/printers/checkstyle.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ type Checkstyle struct {
3737
}
3838

3939
func NewCheckstyle(w io.Writer) *Checkstyle {
40-
return &Checkstyle{
41-
w: w,
42-
}
40+
return &Checkstyle{w: w}
4341
}
4442

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

89-
fmt.Fprintf(p.w, "%s%s\n", xml.Header, xmlfmt.FormatXML(string(data), "", " "))
87+
_, err = fmt.Fprintf(p.w, "%s%s\n", xml.Header, xmlfmt.FormatXML(string(data), "", " "))
88+
if err != nil {
89+
return err
90+
}
91+
9092
return nil
9193
}

pkg/printers/codeclimate.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ type CodeClimate struct {
2828
}
2929

3030
func NewCodeClimate(w io.Writer) *CodeClimate {
31-
return &CodeClimate{
32-
w: w,
33-
}
31+
return &CodeClimate{w: w}
3432
}
3533

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

58-
fmt.Fprint(p.w, string(outputJSON))
56+
_, err = fmt.Fprint(p.w, string(outputJSON))
57+
if err != nil {
58+
return err
59+
}
5960
return nil
6061
}

pkg/printers/github.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ const defaultGithubSeverity = "error"
1717
// NewGithub output format outputs issues according to GitHub actions format:
1818
// https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
1919
func NewGithub(w io.Writer) Printer {
20-
return &github{
21-
w: w,
22-
}
20+
return &github{w: w}
2321
}
2422

2523
// print each line as: ::error file=app.js,line=10,col=15::Something went wrong

pkg/printers/html.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ type HTML struct {
128128
}
129129

130130
func NewHTML(w io.Writer) *HTML {
131-
return &HTML{
132-
w: w,
133-
}
131+
return &HTML{w: w}
134132
}
135133

136134
func (p HTML) Print(_ context.Context, issues []result.Issue) error {

pkg/printers/json.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package printers
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"io"
87

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

39-
outputJSON, err := json.Marshal(res)
40-
if err != nil {
41-
return err
42-
}
43-
44-
fmt.Fprint(p.w, string(outputJSON))
45-
return nil
38+
return json.NewEncoder(p.w).Encode(res)
4639
}

pkg/printers/junitxml.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ type JunitXML struct {
3939
}
4040

4141
func NewJunitXML(w io.Writer) *JunitXML {
42-
return &JunitXML{
43-
w: w,
44-
}
42+
return &JunitXML{w: w}
4543
}
4644

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

0 commit comments

Comments
 (0)