github.com/chenfeining/golangci-lint@v1.0.2-0.20230730162517-14c6c67868df/pkg/commands/help.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "sort" 6 "strings" 7 8 "github.com/fatih/color" 9 "github.com/spf13/cobra" 10 11 "github.com/chenfeining/golangci-lint/pkg/lint/linter" 12 "github.com/chenfeining/golangci-lint/pkg/logutils" 13 ) 14 15 func (e *Executor) initHelp() { 16 helpCmd := &cobra.Command{ 17 Use: "help", 18 Short: "Help", 19 Args: cobra.NoArgs, 20 RunE: func(cmd *cobra.Command, _ []string) error { 21 return cmd.Help() 22 }, 23 } 24 e.rootCmd.SetHelpCommand(helpCmd) 25 26 lintersHelpCmd := &cobra.Command{ 27 Use: "linters", 28 Short: "Help about linters", 29 Args: cobra.NoArgs, 30 ValidArgsFunction: cobra.NoFileCompletions, 31 Run: e.executeLintersHelp, 32 } 33 helpCmd.AddCommand(lintersHelpCmd) 34 } 35 36 func printLinterConfigs(lcs []*linter.Config) { 37 sort.Slice(lcs, func(i, j int) bool { 38 return lcs[i].Name() < lcs[j].Name() 39 }) 40 for _, lc := range lcs { 41 altNamesStr := "" 42 if len(lc.AlternativeNames) != 0 { 43 altNamesStr = fmt.Sprintf(" (%s)", strings.Join(lc.AlternativeNames, ", ")) 44 } 45 46 // If the linter description spans multiple lines, truncate everything following the first newline 47 linterDescription := lc.Linter.Desc() 48 firstNewline := strings.IndexRune(linterDescription, '\n') 49 if firstNewline > 0 { 50 linterDescription = linterDescription[:firstNewline] 51 } 52 53 deprecatedMark := "" 54 if lc.IsDeprecated() { 55 deprecatedMark = " [" + color.RedString("deprecated") + "]" 56 } 57 58 fmt.Fprintf(logutils.StdOut, "%s%s%s: %s [fast: %t, auto-fix: %t]\n", color.YellowString(lc.Name()), 59 altNamesStr, deprecatedMark, linterDescription, !lc.IsSlowLinter(), lc.CanAutoFix) 60 } 61 } 62 63 func (e *Executor) executeLintersHelp(_ *cobra.Command, _ []string) { 64 var enabledLCs, disabledLCs []*linter.Config 65 for _, lc := range e.DBManager.GetAllSupportedLinterConfigs() { 66 if lc.Internal { 67 continue 68 } 69 70 if lc.EnabledByDefault { 71 enabledLCs = append(enabledLCs, lc) 72 } else { 73 disabledLCs = append(disabledLCs, lc) 74 } 75 } 76 77 color.Green("Enabled by default linters:\n") 78 printLinterConfigs(enabledLCs) 79 color.Red("\nDisabled by default linters:\n") 80 printLinterConfigs(disabledLCs) 81 82 color.Green("\nLinters presets:") 83 for _, p := range e.DBManager.AllPresets() { 84 linters := e.DBManager.GetAllLinterConfigsForPreset(p) 85 var linterNames []string 86 for _, lc := range linters { 87 if lc.Internal { 88 continue 89 } 90 91 linterNames = append(linterNames, lc.Name()) 92 } 93 sort.Strings(linterNames) 94 fmt.Fprintf(logutils.StdOut, "%s: %s\n", color.YellowString(p), strings.Join(linterNames, ", ")) 95 } 96 }