-
Notifications
You must be signed in to change notification settings - Fork 224
/
lint.go
317 lines (268 loc) · 7.87 KB
/
lint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"encoding/json"
"fmt"
"os"
"slices"
"strings"
"unicode"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/superfly/flyctl/internal/command/root"
)
func newLintCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "lint",
Short: "Run checks against flyctl commands and flags",
Run: func(cmd *cobra.Command, args []string) {
root := root.New()
run := NewCheckRun(root)
table := tablewriter.NewWriter(os.Stdout)
table.SetBorder(false)
table.SetAutoWrapText(false)
table.SetHeader([]string{"Path", "Check", "Failure Reason"})
errors := run.Run()
for _, err := range errors {
table.Append([]string{
err.command,
err.check,
err.Error(),
})
}
table.Render()
fmt.Println()
fmt.Printf("%d checks failed\n", len(errors))
if len(errors) > 0 {
os.Exit(1)
}
},
}
return cmd
}
// add more to this list as needed. Just make sure they are actually common (in flyctl and other well known CLIs)!
var commonFlagShorthands = map[string]string{
"app": "a",
"org": "o",
"config": "c",
"access-token": "t",
"help": "h",
"json": "j",
}
type run struct {
rootCmd *cobra.Command
commandsByName map[string][]*cobra.Command
flagsByName map[string][]*pflag.Flag
}
func (r *run) Run() (errors []checkError) {
walk(r.rootCmd, func(cmd *cobra.Command, depth int) {
for name, fn := range commandChecks {
if err := fn(r, cmd); err != nil {
errors = append(errors, checkError{
command: cmd.CommandPath(),
check: name,
err: err,
})
}
}
cmd.Flags().VisitAll(func(f *pflag.Flag) {
for name, fn := range flagChecks {
if err := fn(r, cmd, f); err != nil {
errors = append(errors, checkError{
command: cmd.CommandPath() + " --" + f.Name,
check: name,
err: err,
})
}
}
})
})
return
}
func NewCheckRun(root *cobra.Command) *run {
r := &run{
rootCmd: root,
commandsByName: make(map[string][]*cobra.Command),
flagsByName: make(map[string][]*pflag.Flag),
}
walk(root, func(cmd *cobra.Command, depth int) {
r.commandsByName[cmd.Name()] = append(r.commandsByName[cmd.Name()], cmd)
cmd.Flags().VisitAll(func(f *pflag.Flag) {
r.flagsByName[f.Name] = append(r.flagsByName[f.Name], f)
})
})
return r
}
type checkError struct {
command string
check string
err error
}
func (e *checkError) Error() string {
return e.err.Error()
}
type cmdCheckFn func(run *run, cmd *cobra.Command) error
var commandChecks = map[string]cmdCheckFn{
"flags-in-usage": redundantUsageCheck,
"duplicate-description": duplicateDescription,
"description-too-long": shortDescriptionTooLong,
"inconsistent-aliases": inconsistentAliases,
"usage-punctuation": usagePunctuation,
"newline-in-description": newlineInDescription,
"missing-descriptions": missingDescriptions,
"short-description-case": shortDescriptionCasing,
"poor-command-name": poorCommandName,
"too-many-subcommands": tooManySubcommands,
"invalid-group": invalidGroup,
}
type flagCheckFn func(run *run, cmd *cobra.Command, flag *pflag.Flag) error
var flagChecks = map[string]flagCheckFn{
// disabled for now because there are a lot of these
// "flag-punctuation": flagPunctuation,
"misused-shorthand": misusedShorthand,
"inconsistent-flag-shorthands": inconsistentFlagShorthands,
}
func redundantUsageCheck(run *run, cmd *cobra.Command) error {
if strings.Contains(cmd.Use, "[flags]") {
return fmt.Errorf("redundant \"[flags]\" in usage string: \"%s\"; remove \"[flags]\"", cmd.Use)
}
return nil
}
func duplicateDescription(run *run, cmd *cobra.Command) error {
if cmd.Long == cmd.Short {
return fmt.Errorf("duplicate cmd.Long and cmd.Short; remove cmd.long")
}
return nil
}
func shortDescriptionCasing(run *run, cmd *cobra.Command) error {
if cmd.Short != "" && !unicode.IsUpper([]rune(cmd.Short)[0]) {
return fmt.Errorf("cmd.Short should be capitalized")
}
return nil
}
func newlineInDescription(run *run, cmd *cobra.Command) error {
if strings.Contains(cmd.Short, "\n") {
return fmt.Errorf("cmd.Short cannot contain newlines")
}
return nil
}
func missingDescriptions(run *run, cmd *cobra.Command) error {
if cmd.Short == "" && cmd.Long == "" {
return fmt.Errorf("no description; add cmd.Short and ideally cmd.Long")
} else if cmd.Short == "" {
return fmt.Errorf("has cmd.Long but not cmd.Short; add cmd.Short")
}
return nil
}
func shortDescriptionTooLong(run *run, cmd *cobra.Command) error {
if len(cmd.Short) > 80 {
return fmt.Errorf("cmd.Short is too long and risks wrapping; should be 80 characters or less")
}
return nil
}
func usagePunctuation(run *run, cmd *cobra.Command) error {
if strings.HasSuffix(cmd.Use, ".") {
return fmt.Errorf("cmd.Use should not end with a period")
}
return nil
}
func inconsistentAliases(run *run, cmd *cobra.Command) error {
aliases := map[string]int{}
cmdCount := len(run.commandsByName[cmd.Name()])
for _, otherCmd := range run.commandsByName[cmd.Name()] {
for _, alias := range otherCmd.Aliases {
aliases[alias]++
}
}
uncommon := []string{}
missing := []string{}
for alias, count := range aliases {
if float64(count)/float64(cmdCount) > 0.5 {
if !slices.Contains(cmd.Aliases, alias) {
missing = append(missing, alias)
}
} else {
if slices.Contains(cmd.Aliases, alias) {
uncommon = append(uncommon, alias)
}
}
}
if len(uncommon) > 0 {
return fmt.Errorf("has aliases not used by other commands with the same name: %s", encodeForMessage(uncommon))
}
if len(missing) > 0 {
return fmt.Errorf("missing aliases used by other commands with the same name: %s", encodeForMessage(missing))
}
return nil
}
func poorCommandName(run *run, cmd *cobra.Command) error {
var msg string
switch cmd.Name() {
case "destroy":
msg = "is too agro. consider renaming to \"delete\""
}
if msg != "" {
return fmt.Errorf("command name \"%s\" %s", cmd.Name(), msg)
}
return nil
}
func tooManySubcommands(run *run, cmd *cobra.Command) error {
var count int
for _, c := range cmd.Commands() {
if !c.Hidden {
count++
}
}
if count > 10 && len(cmd.Groups()) > 0 {
return fmt.Errorf("too many non-hidden subcommands (%d); consider using groups to organze them", count)
}
return nil
}
func invalidGroup(run *run, cmd *cobra.Command) error {
if cmd.GroupID != "" && !cmd.Parent().ContainsGroup(cmd.GroupID) {
groupIDs := []string{}
for _, g := range cmd.Parent().Groups() {
groupIDs = append(groupIDs, g.ID)
}
return fmt.Errorf("group \"%s\" is not registered on the parent command: %v", cmd.GroupID, groupIDs)
}
return nil
}
// func flagPunctuation(run *run, cmd *cobra.Command, flag *pflag.Flag) error {
// if !strings.HasSuffix(flag.Usage, ".") {
// return fmt.Errorf("flag.Usage should end with a period")
// }
// return nil
// }
func misusedShorthand(run *run, cmd *cobra.Command, flag *pflag.Flag) error {
if flag.Shorthand != "" {
for name, shorthand := range commonFlagShorthands {
if flag.Shorthand == shorthand && flag.Name != name {
return fmt.Errorf("shorthand \"%s\" should only be used with flags named \"%s\"", shorthand, name)
}
}
}
return nil
}
func inconsistentFlagShorthands(run *run, cmd *cobra.Command, flag *pflag.Flag) error {
shorthandCounts := map[string]int{}
flagCount := len(run.flagsByName[flag.Name])
for _, otherFlags := range run.flagsByName[flag.Name] {
shorthandCounts[otherFlags.Shorthand]++
}
for shorthand, count := range shorthandCounts {
if float64(count)/float64(flagCount) > 0.5 {
if flag.Shorthand != shorthand {
return fmt.Errorf("shorthand \"%s\" is inconsistent with other flags with the same name: %v", flag.Shorthand, encodeForMessage(shorthandCounts))
}
}
}
return nil
}
func encodeForMessage(x any) string {
b, err := json.Marshal(x)
if err != nil {
panic(err)
}
return string(b)
}