github.com/elek/golangci-lint@v1.42.2-0.20211208090441-c05b7fcb3a9a/pkg/commands/help.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"sort"
     7  	"strings"
     8  
     9  	"github.com/fatih/color"
    10  	"github.com/spf13/cobra"
    11  
    12  	"github.com/elek/golangci-lint/pkg/lint/linter"
    13  	"github.com/elek/golangci-lint/pkg/logutils"
    14  )
    15  
    16  func (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  
    39  func 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  
    49  		// If the linter description spans multiple lines, truncate everything following the first newline
    50  		linterDescription := lc.Linter.Desc()
    51  		firstNewline := strings.IndexRune(linterDescription, '\n')
    52  		if firstNewline > 0 {
    53  			linterDescription = linterDescription[:firstNewline]
    54  		}
    55  
    56  		fmt.Fprintf(logutils.StdOut, "%s%s: %s [fast: %t, auto-fix: %t]\n", color.YellowString(lc.Name()),
    57  			altNamesStr, linterDescription, !lc.IsSlowLinter(), lc.CanAutoFix)
    58  	}
    59  }
    60  
    61  func (e *Executor) executeLintersHelp(_ *cobra.Command, args []string) {
    62  	if len(args) != 0 {
    63  		e.log.Fatalf("Usage: golangci-lint help linters")
    64  	}
    65  
    66  	var enabledLCs, disabledLCs []*linter.Config
    67  	for _, lc := range e.DBManager.GetAllSupportedLinterConfigs() {
    68  		if lc.EnabledByDefault {
    69  			enabledLCs = append(enabledLCs, lc)
    70  		} else {
    71  			disabledLCs = append(disabledLCs, lc)
    72  		}
    73  	}
    74  
    75  	color.Green("Enabled by default linters:\n")
    76  	printLinterConfigs(enabledLCs)
    77  	color.Red("\nDisabled by default linters:\n")
    78  	printLinterConfigs(disabledLCs)
    79  
    80  	color.Green("\nLinters presets:")
    81  	for _, p := range e.DBManager.AllPresets() {
    82  		linters := e.DBManager.GetAllLinterConfigsForPreset(p)
    83  		linterNames := []string{}
    84  		for _, lc := range linters {
    85  			linterNames = append(linterNames, lc.Name())
    86  		}
    87  		sort.Strings(linterNames)
    88  		fmt.Fprintf(logutils.StdOut, "%s: %s\n", color.YellowString(p), strings.Join(linterNames, ", "))
    89  	}
    90  
    91  	os.Exit(0)
    92  }