Closes #75: golangci-lint linters now prints current linters configuration
This commit is contained in:
parent
d02ac2466e
commit
069e66aa91
11
README.md
11
README.md
@ -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 can be used with zero configuration. By default the following linters are enabled:
|
||||||
```
|
```
|
||||||
$ golangci-lint linters
|
$ golangci-lint help linters
|
||||||
Enabled by default 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]
|
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]
|
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:
|
and the following linters are disabled by default:
|
||||||
```
|
```
|
||||||
$ golangci-lint linters
|
$ golangci-lint help linters
|
||||||
...
|
...
|
||||||
Disabled by default linters:
|
Disabled by default linters:
|
||||||
golint: Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes [fast: true]
|
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
|
# Supported Linters
|
||||||
To see a list of supported linters and which linters are enabled/disabled:
|
To see a list of supported linters and which linters are enabled/disabled:
|
||||||
```
|
```
|
||||||
golangci-lint linters
|
golangci-lint help linters
|
||||||
```
|
```
|
||||||
|
|
||||||
## Enabled By Default 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.
|
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.
|
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
|
## Command-Line Options
|
||||||
```
|
```
|
||||||
golangci-lint run -h
|
golangci-lint run -h
|
||||||
|
@ -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 can be used with zero configuration. By default the following linters are enabled:
|
||||||
```
|
```
|
||||||
$ golangci-lint linters
|
$ golangci-lint help linters
|
||||||
{{.LintersCommandOutputEnabledOnly}}
|
{{.LintersCommandOutputEnabledOnly}}
|
||||||
```
|
```
|
||||||
|
|
||||||
and the following linters are disabled by default:
|
and the following linters are disabled by default:
|
||||||
```
|
```
|
||||||
$ golangci-lint linters
|
$ golangci-lint help linters
|
||||||
...
|
...
|
||||||
{{.LintersCommandOutputDisabledOnly}}
|
{{.LintersCommandOutputDisabledOnly}}
|
||||||
```
|
```
|
||||||
@ -255,7 +255,7 @@ The following great projects use golangci-lint:
|
|||||||
# Supported Linters
|
# Supported Linters
|
||||||
To see a list of supported linters and which linters are enabled/disabled:
|
To see a list of supported linters and which linters are enabled/disabled:
|
||||||
```
|
```
|
||||||
golangci-lint linters
|
golangci-lint help linters
|
||||||
```
|
```
|
||||||
|
|
||||||
## Enabled By Default 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.
|
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.
|
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
|
## Command-Line Options
|
||||||
```
|
```
|
||||||
golangci-lint run -h
|
golangci-lint run -h
|
||||||
|
@ -33,6 +33,7 @@ func NewExecutor(version, commit, date string) *Executor {
|
|||||||
|
|
||||||
e.initRoot()
|
e.initRoot()
|
||||||
e.initRun()
|
e.initRun()
|
||||||
|
e.initHelp()
|
||||||
e.initLinters()
|
e.initLinters()
|
||||||
|
|
||||||
return e
|
return e
|
||||||
|
68
pkg/commands/help.go
Normal file
68
pkg/commands/help.go
Normal 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)
|
||||||
|
}
|
@ -1,57 +1,53 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/golangci/golangci-lint/pkg/lint/linter"
|
"github.com/golangci/golangci-lint/pkg/lint/linter"
|
||||||
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
|
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
|
||||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e *Executor) initLinters() {
|
func (e *Executor) initLinters() {
|
||||||
var lintersCmd = &cobra.Command{
|
lintersCmd := &cobra.Command{
|
||||||
Use: "linters",
|
Use: "linters",
|
||||||
Short: "List linters",
|
Short: "List current linters configuration",
|
||||||
Run: e.executeLinters,
|
Run: e.executeLinters,
|
||||||
}
|
}
|
||||||
e.rootCmd.AddCommand(lintersCmd)
|
e.rootCmd.AddCommand(lintersCmd)
|
||||||
|
e.initRunConfiguration(lintersCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func printLinterConfigs(lcs []linter.Config) {
|
func IsLinterInConfigsList(name string, linters []linter.Config) bool {
|
||||||
for _, lc := range lcs {
|
for _, linter := range linters {
|
||||||
fmt.Fprintf(logutils.StdOut, "%s: %s [fast: %t]\n", color.YellowString(lc.Linter.Name()),
|
if linter.Linter.Name() == name {
|
||||||
lc.Linter.Desc(), !lc.DoesFullImport)
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Executor) executeLinters(cmd *cobra.Command, args []string) {
|
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() {
|
for _, lc := range lintersdb.GetAllSupportedLinterConfigs() {
|
||||||
if lc.EnabledByDefault {
|
if !IsLinterInConfigsList(lc.Linter.Name(), enabledLCs) {
|
||||||
enabledLCs = append(enabledLCs, lc)
|
|
||||||
} else {
|
|
||||||
disabledLCs = append(disabledLCs, lc)
|
disabledLCs = append(disabledLCs, lc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
color.Green("Enabled by default linters:\n")
|
color.Red("\nDisabled by your configuration linters:\n")
|
||||||
printLinterConfigs(enabledLCs)
|
|
||||||
color.Red("\nDisabled by default linters:\n")
|
|
||||||
printLinterConfigs(disabledLCs)
|
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)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
@ -161,17 +161,8 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Executor) initRun() {
|
func (e *Executor) initRunConfiguration(cmd *cobra.Command) {
|
||||||
var runCmd = &cobra.Command{
|
fs := cmd.Flags()
|
||||||
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()
|
|
||||||
fs.SortFlags = false // sort them as they are defined here
|
fs.SortFlags = false // sort them as they are defined here
|
||||||
initFlagSet(fs, e.cfg)
|
initFlagSet(fs, e.cfg)
|
||||||
|
|
||||||
@ -199,6 +190,19 @@ func (e *Executor) initRun() {
|
|||||||
fixSlicesFlags(fs)
|
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) {
|
func fixSlicesFlags(fs *pflag.FlagSet) {
|
||||||
// It's a dirty hack to set flag.Changed to true for every string slice flag.
|
// 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
|
// It's necessary to merge config and command-line slices: otherwise command-line
|
||||||
|
@ -57,7 +57,7 @@ func buildTemplateContext() (map[string]interface{}, error) {
|
|||||||
return nil, fmt.Errorf("can't run go install: %s", err)
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("can't run linters cmd: %s", err)
|
return nil, fmt.Errorf("can't run linters cmd: %s", err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user