Skip to content

Commit 069e66a

Browse files
committed
Closes #75: golangci-lint linters now prints current linters configuration
1 parent d02ac24 commit 069e66a

File tree

7 files changed

+123
-44
lines changed

7 files changed

+123
-44
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Directories are NOT analyzed recursively. To analyze them recursively append `/.
9292

9393
GolangCI-Lint can be used with zero configuration. By default the following linters are enabled:
9494
```
95-
$ golangci-lint linters
95+
$ golangci-lint help linters
9696
Enabled by default linters:
9797
govet: Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: false]
9898
errcheck: Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: false]
@@ -109,7 +109,7 @@ typecheck: Like the front-end of a Go compiler, parses and type-checks Go code [
109109

110110
and the following linters are disabled by default:
111111
```
112-
$ golangci-lint linters
112+
$ golangci-lint help linters
113113
...
114114
Disabled by default linters:
115115
golint: Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes [fast: true]
@@ -282,7 +282,7 @@ The following great projects use golangci-lint:
282282
# Supported Linters
283283
To see a list of supported linters and which linters are enabled/disabled:
284284
```
285-
golangci-lint linters
285+
golangci-lint help linters
286286
```
287287

288288
## Enabled By Default Linters
@@ -321,6 +321,11 @@ The config file has lower priority than command-line options. If the same bool/s
321321
and in the config file, the option from command-line will be used.
322322
Slice options (e.g. list of enabled/disabled linters) are combined from the command-line and config file.
323323

324+
To see a list of enabled by your configuration linters:
325+
```
326+
golangci-lint linters
327+
```
328+
324329
## Command-Line Options
325330
```
326331
golangci-lint run -h

README.tmpl.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ Directories are NOT analyzed recursively. To analyze them recursively append `/.
9292

9393
GolangCI-Lint can be used with zero configuration. By default the following linters are enabled:
9494
```
95-
$ golangci-lint linters
95+
$ golangci-lint help linters
9696
{{.LintersCommandOutputEnabledOnly}}
9797
```
9898

9999
and the following linters are disabled by default:
100100
```
101-
$ golangci-lint linters
101+
$ golangci-lint help linters
102102
...
103103
{{.LintersCommandOutputDisabledOnly}}
104104
```
@@ -255,7 +255,7 @@ The following great projects use golangci-lint:
255255
# Supported Linters
256256
To see a list of supported linters and which linters are enabled/disabled:
257257
```
258-
golangci-lint linters
258+
golangci-lint help linters
259259
```
260260

261261
## Enabled By Default Linters
@@ -269,6 +269,11 @@ The config file has lower priority than command-line options. If the same bool/s
269269
and in the config file, the option from command-line will be used.
270270
Slice options (e.g. list of enabled/disabled linters) are combined from the command-line and config file.
271271

272+
To see a list of enabled by your configuration linters:
273+
```
274+
golangci-lint linters
275+
```
276+
272277
## Command-Line Options
273278
```
274279
golangci-lint run -h

pkg/commands/executor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func NewExecutor(version, commit, date string) *Executor {
3333

3434
e.initRoot()
3535
e.initRun()
36+
e.initHelp()
3637
e.initLinters()
3738

3839
return e

pkg/commands/help.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
"github.com/fatih/color"
9+
"github.com/golangci/golangci-lint/pkg/lint/linter"
10+
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
11+
"github.com/golangci/golangci-lint/pkg/logutils"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
func (e *Executor) initHelp() {
16+
helpCmd := &cobra.Command{
17+
Use: "help",
18+
Short: "Help",
19+
Run: func(cmd *cobra.Command, args []string) {
20+
if err := cmd.Help(); err != nil {
21+
e.log.Fatalf("Can't run help: %s", err)
22+
}
23+
},
24+
}
25+
e.rootCmd.AddCommand(helpCmd)
26+
27+
lintersHelpCmd := &cobra.Command{
28+
Use: "linters",
29+
Short: "Help about linters",
30+
Run: e.executeLintersHelp,
31+
}
32+
helpCmd.AddCommand(lintersHelpCmd)
33+
}
34+
35+
func printLinterConfigs(lcs []linter.Config) {
36+
for _, lc := range lcs {
37+
fmt.Fprintf(logutils.StdOut, "%s: %s [fast: %t]\n", color.YellowString(lc.Linter.Name()),
38+
lc.Linter.Desc(), !lc.DoesFullImport)
39+
}
40+
}
41+
42+
func (e Executor) executeLintersHelp(cmd *cobra.Command, args []string) {
43+
var enabledLCs, disabledLCs []linter.Config
44+
for _, lc := range lintersdb.GetAllSupportedLinterConfigs() {
45+
if lc.EnabledByDefault {
46+
enabledLCs = append(enabledLCs, lc)
47+
} else {
48+
disabledLCs = append(disabledLCs, lc)
49+
}
50+
}
51+
52+
color.Green("Enabled by default linters:\n")
53+
printLinterConfigs(enabledLCs)
54+
color.Red("\nDisabled by default linters:\n")
55+
printLinterConfigs(disabledLCs)
56+
57+
color.Green("\nLinters presets:")
58+
for _, p := range lintersdb.AllPresets() {
59+
linters := lintersdb.GetAllLinterConfigsForPreset(p)
60+
linterNames := []string{}
61+
for _, lc := range linters {
62+
linterNames = append(linterNames, lc.Linter.Name())
63+
}
64+
fmt.Fprintf(logutils.StdOut, "%s: %s\n", color.YellowString(p), strings.Join(linterNames, ", "))
65+
}
66+
67+
os.Exit(0)
68+
}

pkg/commands/linters.go

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,53 @@
11
package commands
22

33
import (
4-
"fmt"
4+
"log"
55
"os"
6-
"strings"
76

87
"github.com/fatih/color"
98
"github.com/golangci/golangci-lint/pkg/lint/linter"
109
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
11-
"github.com/golangci/golangci-lint/pkg/logutils"
1210
"github.com/spf13/cobra"
1311
)
1412

1513
func (e *Executor) initLinters() {
16-
var lintersCmd = &cobra.Command{
14+
lintersCmd := &cobra.Command{
1715
Use: "linters",
18-
Short: "List linters",
16+
Short: "List current linters configuration",
1917
Run: e.executeLinters,
2018
}
2119
e.rootCmd.AddCommand(lintersCmd)
20+
e.initRunConfiguration(lintersCmd)
2221
}
2322

24-
func printLinterConfigs(lcs []linter.Config) {
25-
for _, lc := range lcs {
26-
fmt.Fprintf(logutils.StdOut, "%s: %s [fast: %t]\n", color.YellowString(lc.Linter.Name()),
27-
lc.Linter.Desc(), !lc.DoesFullImport)
23+
func IsLinterInConfigsList(name string, linters []linter.Config) bool {
24+
for _, linter := range linters {
25+
if linter.Linter.Name() == name {
26+
return true
27+
}
2828
}
29+
30+
return false
2931
}
3032

3133
func (e Executor) executeLinters(cmd *cobra.Command, args []string) {
32-
var enabledLCs, disabledLCs []linter.Config
33-
for _, lc := range lintersdb.GetAllSupportedLinterConfigs() {
34-
if lc.EnabledByDefault {
35-
enabledLCs = append(enabledLCs, lc)
36-
} else {
37-
disabledLCs = append(disabledLCs, lc)
38-
}
34+
enabledLCs, err := lintersdb.GetEnabledLinters(e.cfg, e.log.Child("lintersdb"))
35+
if err != nil {
36+
log.Fatalf("Can't get enabled linters: %s", err)
3937
}
4038

41-
color.Green("Enabled by default linters:\n")
39+
color.Green("Enabled by your configuration linters:\n")
4240
printLinterConfigs(enabledLCs)
43-
color.Red("\nDisabled by default linters:\n")
44-
printLinterConfigs(disabledLCs)
4541

46-
color.Green("\nLinters presets:")
47-
for _, p := range lintersdb.AllPresets() {
48-
linters := lintersdb.GetAllLinterConfigsForPreset(p)
49-
linterNames := []string{}
50-
for _, lc := range linters {
51-
linterNames = append(linterNames, lc.Linter.Name())
42+
var disabledLCs []linter.Config
43+
for _, lc := range lintersdb.GetAllSupportedLinterConfigs() {
44+
if !IsLinterInConfigsList(lc.Linter.Name(), enabledLCs) {
45+
disabledLCs = append(disabledLCs, lc)
5246
}
53-
fmt.Fprintf(logutils.StdOut, "%s: %s\n", color.YellowString(p), strings.Join(linterNames, ", "))
5447
}
5548

49+
color.Red("\nDisabled by your configuration linters:\n")
50+
printLinterConfigs(disabledLCs)
51+
5652
os.Exit(0)
5753
}

pkg/commands/run.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,8 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config) {
161161

162162
}
163163

164-
func (e *Executor) initRun() {
165-
var runCmd = &cobra.Command{
166-
Use: "run",
167-
Short: welcomeMessage,
168-
Run: e.executeRun,
169-
}
170-
e.rootCmd.AddCommand(runCmd)
171-
172-
runCmd.SetOutput(logutils.StdOut) // use custom output to properly color it in Windows terminals
173-
174-
fs := runCmd.Flags()
164+
func (e *Executor) initRunConfiguration(cmd *cobra.Command) {
165+
fs := cmd.Flags()
175166
fs.SortFlags = false // sort them as they are defined here
176167
initFlagSet(fs, e.cfg)
177168

@@ -199,6 +190,19 @@ func (e *Executor) initRun() {
199190
fixSlicesFlags(fs)
200191
}
201192

193+
func (e *Executor) initRun() {
194+
var runCmd = &cobra.Command{
195+
Use: "run",
196+
Short: welcomeMessage,
197+
Run: e.executeRun,
198+
}
199+
e.rootCmd.AddCommand(runCmd)
200+
201+
runCmd.SetOutput(logutils.StdOut) // use custom output to properly color it in Windows terminals
202+
203+
e.initRunConfiguration(runCmd)
204+
}
205+
202206
func fixSlicesFlags(fs *pflag.FlagSet) {
203207
// It's a dirty hack to set flag.Changed to true for every string slice flag.
204208
// It's necessary to merge config and command-line slices: otherwise command-line

scripts/gen_readme/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func buildTemplateContext() (map[string]interface{}, error) {
5757
return nil, fmt.Errorf("can't run go install: %s", err)
5858
}
5959

60-
lintersOut, err := exec.Command("golangci-lint", "linters").Output()
60+
lintersOut, err := exec.Command("golangci-lint", "help", "linters").Output()
6161
if err != nil {
6262
return nil, fmt.Errorf("can't run linters cmd: %s", err)
6363
}

0 commit comments

Comments
 (0)