forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelp.go
101 lines (74 loc) · 2.15 KB
/
help.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
package commands
import (
"strings"
"unicode"
"unicode/utf8"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/golangci/golangci-lint/v2/pkg/lint/lintersdb"
"github.com/golangci/golangci-lint/v2/pkg/logutils"
)
type helpOptions struct {
JSON bool
}
type helpCommand struct {
cmd *cobra.Command
opts helpOptions
dbManager *lintersdb.Manager
log logutils.Log
}
func newHelpCommand(logger logutils.Log) *helpCommand {
c := &helpCommand{log: logger}
helpCmd := &cobra.Command{
Use: "help",
Short: "Help",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return cmd.Help()
},
}
lintersCmd := &cobra.Command{
Use: "linters",
Short: "Help about linters",
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: c.lintersExecute,
PreRunE: c.lintersPreRunE,
}
fsLinter := lintersCmd.Flags()
fsLinter.SortFlags = false // sort them as they are defined here
fsLinter.BoolVar(&c.opts.JSON, "json", false, color.GreenString("Display as JSON"))
helpCmd.AddCommand(lintersCmd)
formattersCmd := &cobra.Command{
Use: "formatters",
Short: "Help about formatters",
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: c.formattersExecute,
PreRunE: c.formattersPreRunE,
}
fsFormatter := formattersCmd.Flags()
fsFormatter.SortFlags = false // sort them as they are defined here
fsFormatter.BoolVar(&c.opts.JSON, "json", false, color.GreenString("Display as JSON"))
helpCmd.AddCommand(formattersCmd)
c.cmd = helpCmd
return c
}
func formatDescription(desc string) string {
desc = strings.TrimSpace(desc)
if desc == "" {
return desc
}
// If the linter description spans multiple lines, truncate everything following the first newline
endFirstLine := strings.IndexRune(desc, '\n')
if endFirstLine > 0 {
desc = desc[:endFirstLine]
}
rawDesc := []rune(desc)
r, _ := utf8.DecodeRuneInString(desc)
rawDesc[0] = unicode.ToUpper(r)
if rawDesc[len(rawDesc)-1] != '.' {
rawDesc = append(rawDesc, '.')
}
return string(rawDesc)
}