Skip to content

Commit a7d29b8

Browse files
committed
disable the congrats message
There is now an extra switch '-s' to disable the congrats message when there are no issues detected Fixes: golangci#110
1 parent 9fa9e2b commit a7d29b8

File tree

9 files changed

+51
-18
lines changed

9 files changed

+51
-18
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ Global Flags:
294294
-j, --concurrency int Concurrency (default NumCPU) (default 8)
295295
--cpu-profile-path string Path to CPU profile output file
296296
--mem-profile-path string Path to memory profile output file
297+
-s, --silent disables congrats outputs
297298
-v, --verbose verbose output
298299
299300
```

pkg/commands/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func (e *Executor) needVersionOption() bool {
8383

8484
func initRootFlagSet(fs *pflag.FlagSet, cfg *config.Config, needVersionOption bool) {
8585
fs.BoolVarP(&cfg.Run.IsVerbose, "verbose", "v", false, wh("verbose output"))
86+
fs.BoolVarP(&cfg.Run.Silent, "silent", "s", false, wh("disables congrats outputs"))
8687
fs.StringVar(&cfg.Run.CPUProfilePath, "cpu-profile-path", "", wh("Path to CPU profile output file"))
8788
fs.StringVar(&cfg.Run.MemProfilePath, "mem-profile-path", "", wh("Path to memory profile output file"))
8889
fs.IntVarP(&cfg.Run.Concurrency, "concurrency", "j", getDefaultConcurrency(), wh("Concurrency (default NumCPU)"))

pkg/commands/run.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,16 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
255255
format := e.cfg.Output.Format
256256
switch format {
257257
case config.OutFormatJSON:
258-
p = printers.NewJSON()
258+
p = printers.NewJSON(e.cfg.Run.Silent)
259259
case config.OutFormatColoredLineNumber, config.OutFormatLineNumber:
260260
p = printers.NewText(e.cfg.Output.PrintIssuedLine,
261-
format == config.OutFormatColoredLineNumber, e.cfg.Output.PrintLinterName,
261+
format == config.OutFormatColoredLineNumber, e.cfg.Output.PrintLinterName, e.cfg.Run.Silent,
262262
e.log.Child("text_printer"))
263263
case config.OutFormatTab:
264-
p = printers.NewTab(e.cfg.Output.PrintLinterName,
264+
p = printers.NewTab(e.cfg.Output.PrintLinterName, e.cfg.Run.Silent,
265265
e.log.Child("tab_printer"))
266266
case config.OutFormatCheckstyle:
267-
p = printers.NewCheckstyle()
267+
p = printers.NewCheckstyle(e.cfg.Run.Silent)
268268
default:
269269
return fmt.Errorf("unknown output format %s", format)
270270
}

pkg/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func GetDefaultExcludePatternsStrings() []string {
9292

9393
type Run struct {
9494
IsVerbose bool `mapstructure:"verbose"`
95+
Silent bool
9596
CPUProfilePath string
9697
MemProfilePath string
9798
Concurrency int

pkg/printers/checkstyle.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ type checkstyleError struct {
3030

3131
const defaultSeverity = "error"
3232

33-
type Checkstyle struct{}
33+
type Checkstyle struct {
34+
silent bool
35+
}
3436

35-
func NewCheckstyle() *Checkstyle {
36-
return &Checkstyle{}
37+
func NewCheckstyle(silent bool) *Checkstyle {
38+
return &Checkstyle{
39+
silent: silent,
40+
}
3741
}
3842

39-
func (Checkstyle) Print(ctx context.Context, issues <-chan result.Issue) (bool, error) {
43+
func (c Checkstyle) Print(ctx context.Context, issues <-chan result.Issue) (bool, error) {
4044
out := checkstyleOutput{
4145
Version: "5.0",
4246
}
@@ -64,6 +68,10 @@ func (Checkstyle) Print(ctx context.Context, issues <-chan result.Issue) (bool,
6468
file.Errors = append(file.Errors, newError)
6569
}
6670

71+
if len(files) == 0 && c.silent {
72+
return false, nil
73+
}
74+
6775
out.Files = make([]*checkstyleFile, 0, len(files))
6876
for _, file := range files {
6977
out.Files = append(out.Files, file)

pkg/printers/json.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,30 @@ import (
99
"github.com/golangci/golangci-lint/pkg/result"
1010
)
1111

12-
type JSON struct{}
12+
type JSON struct {
13+
silent bool
14+
}
1315

14-
func NewJSON() *JSON {
15-
return &JSON{}
16+
func NewJSON(silent bool) *JSON {
17+
return &JSON{
18+
silent: silent,
19+
}
1620
}
1721

1822
type JSONResult struct {
1923
Issues []result.Issue
2024
}
2125

22-
func (JSON) Print(ctx context.Context, issues <-chan result.Issue) (bool, error) {
26+
func (j JSON) Print(ctx context.Context, issues <-chan result.Issue) (bool, error) {
2327
allIssues := []result.Issue{}
2428
for i := range issues {
2529
allIssues = append(allIssues, i)
2630
}
2731

32+
if len(allIssues) == 0 && j.silent {
33+
return false, nil
34+
}
35+
2836
res := JSONResult{
2937
Issues: allIssues,
3038
}

pkg/printers/tab.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import (
1313

1414
type Tab struct {
1515
printLinterName bool
16+
silent bool
1617
log logutils.Log
1718
}
1819

19-
func NewTab(printLinterName bool, log logutils.Log) *Tab {
20+
func NewTab(printLinterName bool, silent bool, log logutils.Log) *Tab {
2021
return &Tab{
2122
printLinterName: printLinterName,
23+
silent: silent,
2224
log: log,
2325
}
2426
}
@@ -40,8 +42,10 @@ func (p *Tab) Print(ctx context.Context, issues <-chan result.Issue) (bool, erro
4042
if issuesN != 0 {
4143
p.log.Infof("Found %d issues", issuesN)
4244
} else if ctx.Err() == nil { // don't print "congrats" if timeouted
43-
outStr := p.SprintfColored(color.FgGreen, "Congrats! No issues were found.")
44-
fmt.Fprintln(logutils.StdOut, outStr)
45+
if !p.silent {
46+
outStr := p.SprintfColored(color.FgGreen, "Congrats! No issues were found.")
47+
fmt.Fprintln(logutils.StdOut, outStr)
48+
}
4549
}
4650

4751
if err := w.Flush(); err != nil {

pkg/printers/text.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ type Text struct {
2020
printIssuedLine bool
2121
useColors bool
2222
printLinterName bool
23+
silent bool
2324

2425
cache filesCache
2526
log logutils.Log
2627
}
2728

28-
func NewText(printIssuedLine, useColors, printLinterName bool, log logutils.Log) *Text {
29+
func NewText(printIssuedLine, useColors, printLinterName bool, silent bool, log logutils.Log) *Text {
2930
return &Text{
3031
printIssuedLine: printIssuedLine,
3132
useColors: useColors,
3233
printLinterName: printLinterName,
34+
silent: silent,
3335
cache: filesCache{},
3436
log: log,
3537
}
@@ -92,8 +94,10 @@ func (p *Text) Print(ctx context.Context, issues <-chan result.Issue) (bool, err
9294
if issuesN != 0 {
9395
p.log.Infof("Found %d issues", issuesN)
9496
} else if ctx.Err() == nil { // don't print "congrats" if timeouted
95-
outStr := p.SprintfColored(color.FgGreen, "Congrats! No issues were found.")
96-
fmt.Fprintln(logutils.StdOut, outStr)
97+
if !p.silent {
98+
outStr := p.SprintfColored(color.FgGreen, "Congrats! No issues were found.")
99+
fmt.Fprintln(logutils.StdOut, outStr)
100+
}
97101
}
98102

99103
return issuesN != 0, nil

test/run_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ func checkNoIssuesRun(t *testing.T, out string, exitCode int) {
3333
assert.Equal(t, "Congrats! No issues were found.\n", out)
3434
}
3535

36+
func TestCongratsMessageGoneIfSilent(t *testing.T) {
37+
out, exitCode := runGolangciLint(t, "../...", "-s")
38+
assert.Equal(t, exitcodes.Success, exitCode)
39+
assert.Equal(t, "", out)
40+
}
41+
3642
func TestCongratsMessageIfNoIssues(t *testing.T) {
3743
out, exitCode := runGolangciLint(t, "../...")
3844
checkNoIssuesRun(t, out, exitCode)

0 commit comments

Comments
 (0)