11package commands
22
33import (
4- "encoding/json"
5- "fmt"
6- "maps"
7- "slices"
84 "strings"
95 "unicode"
106 "unicode/utf8"
117
128 "github.com/fatih/color"
139 "github.com/spf13/cobra"
1410
15- "github.com/golangci/golangci-lint/pkg/config"
16- "github.com/golangci/golangci-lint/pkg/lint/linter"
1711 "github.com/golangci/golangci-lint/pkg/lint/lintersdb"
1812 "github.com/golangci/golangci-lint/pkg/logutils"
1913)
2014
21- type linterHelp struct {
22- Name string `json:"name"`
23- Desc string `json:"description"`
24- Groups []string `json:"groups"`
25- Fast bool `json:"fast"`
26- AutoFix bool `json:"autoFix"`
27- Deprecated bool `json:"deprecated"`
28- Since string `json:"since"`
29- OriginalURL string `json:"originalURL,omitempty"`
30- }
31-
3215type helpOptions struct {
3316 JSON bool
3417}
@@ -60,12 +43,23 @@ func newHelpCommand(logger logutils.Log) *helpCommand {
6043 Short : "Help about linters" ,
6144 Args : cobra .NoArgs ,
6245 ValidArgsFunction : cobra .NoFileCompletions ,
63- RunE : c .execute ,
64- PreRunE : c .preRunE ,
46+ RunE : c .lintersExecute ,
47+ PreRunE : c .lintersPreRunE ,
6548 }
6649
6750 helpCmd .AddCommand (lintersCmd )
6851
52+ formattersCmd := & cobra.Command {
53+ Use : "formatters" ,
54+ Short : "Help about formatters" ,
55+ Args : cobra .NoArgs ,
56+ ValidArgsFunction : cobra .NoFileCompletions ,
57+ RunE : c .formattersExecute ,
58+ PreRunE : c .formattersPreRunE ,
59+ }
60+
61+ helpCmd .AddCommand (formattersCmd )
62+
6963 fs := lintersCmd .Flags ()
7064 fs .SortFlags = false // sort them as they are defined here
7165
@@ -76,122 +70,6 @@ func newHelpCommand(logger logutils.Log) *helpCommand {
7670 return c
7771}
7872
79- func (c * helpCommand ) preRunE (_ * cobra.Command , _ []string ) error {
80- // The command doesn't depend on the real configuration.
81- // It just needs the list of all plugins and all presets.
82- dbManager , err := lintersdb .NewManager (c .log .Child (logutils .DebugKeyLintersDB ), config .NewDefault (), lintersdb .NewLinterBuilder ())
83- if err != nil {
84- return err
85- }
86-
87- c .dbManager = dbManager
88-
89- return nil
90- }
91-
92- func (c * helpCommand ) execute (_ * cobra.Command , _ []string ) error {
93- if c .opts .JSON {
94- return c .printJSON ()
95- }
96-
97- c .print ()
98-
99- return nil
100- }
101-
102- func (c * helpCommand ) printJSON () error {
103- var linters []linterHelp
104-
105- for _ , lc := range c .dbManager .GetAllSupportedLinterConfigs () {
106- if lc .Internal {
107- continue
108- }
109-
110- groups := []string {config .GroupAll }
111-
112- if ! lc .IsSlowLinter () {
113- groups = append (groups , config .GroupFast )
114- }
115-
116- linters = append (linters , linterHelp {
117- Name : lc .Name (),
118- Desc : formatDescription (lc .Linter .Desc ()),
119- Groups : slices .Concat (groups , slices .Collect (maps .Keys (lc .Groups ))),
120- Fast : ! lc .IsSlowLinter (),
121- AutoFix : lc .CanAutoFix ,
122- Deprecated : lc .IsDeprecated (),
123- Since : lc .Since ,
124- OriginalURL : lc .OriginalURL ,
125- })
126- }
127-
128- return json .NewEncoder (c .cmd .OutOrStdout ()).Encode (linters )
129- }
130-
131- func (c * helpCommand ) print () {
132- var enabledLCs , disabledLCs []* linter.Config
133- for _ , lc := range c .dbManager .GetAllSupportedLinterConfigs () {
134- if lc .Internal {
135- continue
136- }
137-
138- if lc .FromGroup (config .GroupStandard ) {
139- enabledLCs = append (enabledLCs , lc )
140- } else {
141- disabledLCs = append (disabledLCs , lc )
142- }
143- }
144-
145- color .Green ("Enabled by default linters:\n " )
146- printLinters (enabledLCs )
147-
148- color .Red ("\n Disabled by default linters:\n " )
149- printLinters (disabledLCs )
150- }
151-
152- func printLinters (lcs []* linter.Config ) {
153- slices .SortFunc (lcs , func (a , b * linter.Config ) int {
154- if a .IsDeprecated () && b .IsDeprecated () {
155- return strings .Compare (a .Name (), b .Name ())
156- }
157-
158- if a .IsDeprecated () {
159- return 1
160- }
161-
162- if b .IsDeprecated () {
163- return - 1
164- }
165-
166- return strings .Compare (a .Name (), b .Name ())
167- })
168-
169- for _ , lc := range lcs {
170- desc := formatDescription (lc .Linter .Desc ())
171-
172- deprecatedMark := ""
173- if lc .IsDeprecated () {
174- deprecatedMark = " [" + color .RedString ("deprecated" ) + "]"
175- }
176-
177- var capabilities []string
178- if ! lc .IsSlowLinter () {
179- capabilities = append (capabilities , color .BlueString ("fast" ))
180- }
181- if lc .CanAutoFix {
182- capabilities = append (capabilities , color .GreenString ("auto-fix" ))
183- }
184-
185- var capability string
186- if capabilities != nil {
187- capability = " [" + strings .Join (capabilities , ", " ) + "]"
188- }
189-
190- _ , _ = fmt .Fprintf (logutils .StdOut , "%s%s: %s%s\n " ,
191- color .YellowString (lc .Name ()), deprecatedMark , desc , capability )
192- }
193- }
194-
19573func formatDescription (desc string ) string {
19674 desc = strings .TrimSpace (desc )
19775
0 commit comments