1
1
package commands
2
2
3
3
import (
4
- "encoding/json"
5
- "fmt"
6
- "maps"
7
- "slices"
8
4
"strings"
9
5
"unicode"
10
6
"unicode/utf8"
11
7
12
8
"github.com/fatih/color"
13
9
"github.com/spf13/cobra"
14
10
15
- "github.com/golangci/golangci-lint/pkg/config"
16
- "github.com/golangci/golangci-lint/pkg/lint/linter"
17
11
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
18
12
"github.com/golangci/golangci-lint/pkg/logutils"
19
13
)
20
14
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
-
32
15
type helpOptions struct {
33
16
JSON bool
34
17
}
@@ -60,12 +43,23 @@ func newHelpCommand(logger logutils.Log) *helpCommand {
60
43
Short : "Help about linters" ,
61
44
Args : cobra .NoArgs ,
62
45
ValidArgsFunction : cobra .NoFileCompletions ,
63
- RunE : c .execute ,
64
- PreRunE : c .preRunE ,
46
+ RunE : c .lintersExecute ,
47
+ PreRunE : c .lintersPreRunE ,
65
48
}
66
49
67
50
helpCmd .AddCommand (lintersCmd )
68
51
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
+
69
63
fs := lintersCmd .Flags ()
70
64
fs .SortFlags = false // sort them as they are defined here
71
65
@@ -76,122 +70,6 @@ func newHelpCommand(logger logutils.Log) *helpCommand {
76
70
return c
77
71
}
78
72
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
-
195
73
func formatDescription (desc string ) string {
196
74
desc = strings .TrimSpace (desc )
197
75
0 commit comments