Closes #75: golangci-lint linters now prints current linters configuration

This commit is contained in:
Denis Isaev 2018-07-29 19:33:24 +03:00 committed by Isaev Denis
parent d02ac2466e
commit 069e66aa91
7 changed files with 123 additions and 44 deletions

View File

@ -92,7 +92,7 @@ Directories are NOT analyzed recursively. To analyze them recursively append `/.
GolangCI-Lint can be used with zero configuration. By default the following linters are enabled:
```
$ golangci-lint linters
$ golangci-lint help linters
Enabled by default linters:
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]
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 [
and the following linters are disabled by default:
```
$ golangci-lint linters
$ golangci-lint help linters
...
Disabled by default linters:
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:
# Supported Linters
To see a list of supported linters and which linters are enabled/disabled:
```
golangci-lint linters
golangci-lint help linters
```
## Enabled By Default Linters
@ -321,6 +321,11 @@ The config file has lower priority than command-line options. If the same bool/s
and in the config file, the option from command-line will be used.
Slice options (e.g. list of enabled/disabled linters) are combined from the command-line and config file.
To see a list of enabled by your configuration linters:
```
golangci-lint linters
```
## Command-Line Options
```
golangci-lint run -h

View File

@ -92,13 +92,13 @@ Directories are NOT analyzed recursively. To analyze them recursively append `/.
GolangCI-Lint can be used with zero configuration. By default the following linters are enabled:
```
$ golangci-lint linters
$ golangci-lint help linters
{{.LintersCommandOutputEnabledOnly}}
```
and the following linters are disabled by default:
```
$ golangci-lint linters
$ golangci-lint help linters
...
{{.LintersCommandOutputDisabledOnly}}
```
@ -255,7 +255,7 @@ The following great projects use golangci-lint:
# Supported Linters
To see a list of supported linters and which linters are enabled/disabled:
```
golangci-lint linters
golangci-lint help linters
```
## Enabled By Default Linters
@ -269,6 +269,11 @@ The config file has lower priority than command-line options. If the same bool/s
and in the config file, the option from command-line will be used.
Slice options (e.g. list of enabled/disabled linters) are combined from the command-line and config file.
To see a list of enabled by your configuration linters:
```
golangci-lint linters
```
## Command-Line Options
```
golangci-lint run -h

View File

@ -33,6 +33,7 @@ func NewExecutor(version, commit, date string) *Executor {
e.initRoot()
e.initRun()
e.initHelp()
e.initLinters()
return e

68
pkg/commands/help.go Normal file
View File

@ -0,0 +1,68 @@
package commands
import (
"fmt"
"os"
"strings"
"github.com/fatih/color"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/spf13/cobra"
)
func (e *Executor) initHelp() {
helpCmd := &cobra.Command{
Use: "help",
Short: "Help",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
e.log.Fatalf("Can't run help: %s", err)
}
},
}
e.rootCmd.AddCommand(helpCmd)
lintersHelpCmd := &cobra.Command{
Use: "linters",
Short: "Help about linters",
Run: e.executeLintersHelp,
}
helpCmd.AddCommand(lintersHelpCmd)
}
func printLinterConfigs(lcs []linter.Config) {
for _, lc := range lcs {
fmt.Fprintf(logutils.StdOut, "%s: %s [fast: %t]\n", color.YellowString(lc.Linter.Name()),
lc.Linter.Desc(), !lc.DoesFullImport)
}
}
func (e Executor) executeLintersHelp(cmd *cobra.Command, args []string) {
var enabledLCs, disabledLCs []linter.Config
for _, lc := range lintersdb.GetAllSupportedLinterConfigs() {
if lc.EnabledByDefault {
enabledLCs = append(enabledLCs, lc)
} else {
disabledLCs = append(disabledLCs, lc)
}
}
color.Green("Enabled by default linters:\n")
printLinterConfigs(enabledLCs)
color.Red("\nDisabled by default linters:\n")
printLinterConfigs(disabledLCs)
color.Green("\nLinters presets:")
for _, p := range lintersdb.AllPresets() {
linters := lintersdb.GetAllLinterConfigsForPreset(p)
linterNames := []string{}
for _, lc := range linters {
linterNames = append(linterNames, lc.Linter.Name())
}
fmt.Fprintf(logutils.StdOut, "%s: %s\n", color.YellowString(p), strings.Join(linterNames, ", "))
}
os.Exit(0)
}

View File

@ -1,57 +1,53 @@
package commands
import (
"fmt"
"log"
"os"
"strings"
"github.com/fatih/color"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/spf13/cobra"
)
func (e *Executor) initLinters() {
var lintersCmd = &cobra.Command{
lintersCmd := &cobra.Command{
Use: "linters",
Short: "List linters",
Short: "List current linters configuration",
Run: e.executeLinters,
}
e.rootCmd.AddCommand(lintersCmd)
e.initRunConfiguration(lintersCmd)
}
func printLinterConfigs(lcs []linter.Config) {
for _, lc := range lcs {
fmt.Fprintf(logutils.StdOut, "%s: %s [fast: %t]\n", color.YellowString(lc.Linter.Name()),
lc.Linter.Desc(), !lc.DoesFullImport)
func IsLinterInConfigsList(name string, linters []linter.Config) bool {
for _, linter := range linters {
if linter.Linter.Name() == name {
return true
}
}
return false
}
func (e Executor) executeLinters(cmd *cobra.Command, args []string) {
var enabledLCs, disabledLCs []linter.Config
enabledLCs, err := lintersdb.GetEnabledLinters(e.cfg, e.log.Child("lintersdb"))
if err != nil {
log.Fatalf("Can't get enabled linters: %s", err)
}
color.Green("Enabled by your configuration linters:\n")
printLinterConfigs(enabledLCs)
var disabledLCs []linter.Config
for _, lc := range lintersdb.GetAllSupportedLinterConfigs() {
if lc.EnabledByDefault {
enabledLCs = append(enabledLCs, lc)
} else {
if !IsLinterInConfigsList(lc.Linter.Name(), enabledLCs) {
disabledLCs = append(disabledLCs, lc)
}
}
color.Green("Enabled by default linters:\n")
printLinterConfigs(enabledLCs)
color.Red("\nDisabled by default linters:\n")
color.Red("\nDisabled by your configuration linters:\n")
printLinterConfigs(disabledLCs)
color.Green("\nLinters presets:")
for _, p := range lintersdb.AllPresets() {
linters := lintersdb.GetAllLinterConfigsForPreset(p)
linterNames := []string{}
for _, lc := range linters {
linterNames = append(linterNames, lc.Linter.Name())
}
fmt.Fprintf(logutils.StdOut, "%s: %s\n", color.YellowString(p), strings.Join(linterNames, ", "))
}
os.Exit(0)
}

View File

@ -161,17 +161,8 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config) {
}
func (e *Executor) initRun() {
var runCmd = &cobra.Command{
Use: "run",
Short: welcomeMessage,
Run: e.executeRun,
}
e.rootCmd.AddCommand(runCmd)
runCmd.SetOutput(logutils.StdOut) // use custom output to properly color it in Windows terminals
fs := runCmd.Flags()
func (e *Executor) initRunConfiguration(cmd *cobra.Command) {
fs := cmd.Flags()
fs.SortFlags = false // sort them as they are defined here
initFlagSet(fs, e.cfg)
@ -199,6 +190,19 @@ func (e *Executor) initRun() {
fixSlicesFlags(fs)
}
func (e *Executor) initRun() {
var runCmd = &cobra.Command{
Use: "run",
Short: welcomeMessage,
Run: e.executeRun,
}
e.rootCmd.AddCommand(runCmd)
runCmd.SetOutput(logutils.StdOut) // use custom output to properly color it in Windows terminals
e.initRunConfiguration(runCmd)
}
func fixSlicesFlags(fs *pflag.FlagSet) {
// It's a dirty hack to set flag.Changed to true for every string slice flag.
// It's necessary to merge config and command-line slices: otherwise command-line

View File

@ -57,7 +57,7 @@ func buildTemplateContext() (map[string]interface{}, error) {
return nil, fmt.Errorf("can't run go install: %s", err)
}
lintersOut, err := exec.Command("golangci-lint", "linters").Output()
lintersOut, err := exec.Command("golangci-lint", "help", "linters").Output()
if err != nil {
return nil, fmt.Errorf("can't run linters cmd: %s", err)
}