1package commands
2
3import (
4	"fmt"
5	"os"
6	"sort"
7	"strings"
8
9	"github.com/fatih/color"
10	"github.com/spf13/cobra"
11
12	"github.com/golangci/golangci-lint/pkg/lint/linter"
13	"github.com/golangci/golangci-lint/pkg/logutils"
14)
15
16func (e *Executor) initHelp() {
17	helpCmd := &cobra.Command{
18		Use:   "help",
19		Short: "Help",
20		Run: func(cmd *cobra.Command, args []string) {
21			if len(args) != 0 {
22				e.log.Fatalf("Usage: golangci-lint help")
23			}
24			if err := cmd.Help(); err != nil {
25				e.log.Fatalf("Can't run help: %s", err)
26			}
27		},
28	}
29	e.rootCmd.SetHelpCommand(helpCmd)
30
31	lintersHelpCmd := &cobra.Command{
32		Use:   "linters",
33		Short: "Help about linters",
34		Run:   e.executeLintersHelp,
35	}
36	helpCmd.AddCommand(lintersHelpCmd)
37}
38
39func printLinterConfigs(lcs []*linter.Config) {
40	sort.Slice(lcs, func(i, j int) bool {
41		return strings.Compare(lcs[i].Name(), lcs[j].Name()) < 0
42	})
43	for _, lc := range lcs {
44		altNamesStr := ""
45		if len(lc.AlternativeNames) != 0 {
46			altNamesStr = fmt.Sprintf(" (%s)", strings.Join(lc.AlternativeNames, ", "))
47		}
48		fmt.Fprintf(logutils.StdOut, "%s%s: %s [fast: %t, auto-fix: %t]\n", color.YellowString(lc.Name()),
49			altNamesStr, lc.Linter.Desc(), !lc.IsSlowLinter(), lc.CanAutoFix)
50	}
51}
52
53func (e *Executor) executeLintersHelp(_ *cobra.Command, args []string) {
54	if len(args) != 0 {
55		e.log.Fatalf("Usage: golangci-lint help linters")
56	}
57
58	var enabledLCs, disabledLCs []*linter.Config
59	for _, lc := range e.DBManager.GetAllSupportedLinterConfigs() {
60		if lc.EnabledByDefault {
61			enabledLCs = append(enabledLCs, lc)
62		} else {
63			disabledLCs = append(disabledLCs, lc)
64		}
65	}
66
67	color.Green("Enabled by default linters:\n")
68	printLinterConfigs(enabledLCs)
69	color.Red("\nDisabled by default linters:\n")
70	printLinterConfigs(disabledLCs)
71
72	color.Green("\nLinters presets:")
73	for _, p := range e.DBManager.AllPresets() {
74		linters := e.DBManager.GetAllLinterConfigsForPreset(p)
75		linterNames := []string{}
76		for _, lc := range linters {
77			linterNames = append(linterNames, lc.Name())
78		}
79		sort.Strings(linterNames)
80		fmt.Fprintf(logutils.StdOut, "%s: %s\n", color.YellowString(p), strings.Join(linterNames, ", "))
81	}
82
83	os.Exit(0)
84}
85