Skip to content

Commit cbe5208

Browse files
committed
feat: new help commands related to formatters
1 parent 9d5aef5 commit cbe5208

File tree

8 files changed

+409
-135
lines changed

8 files changed

+409
-135
lines changed

Diff for: .github/workflows/pr.yml

+2
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,7 @@ jobs:
153153

154154
- run: ./golangci-lint help
155155
- run: ./golangci-lint help linters
156+
- run: ./golangci-lint help formatters
156157
- run: ./golangci-lint linters
158+
- run: ./golangci-lint formatters
157159
- run: ./golangci-lint version

Diff for: .golangci.yml

+5
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ linters:
182182
- gocritic
183183
text: "hugeParam:"
184184

185+
# The codes are close but this is not duplication.
186+
- path: pkg/commands/(formatters|linters).go
187+
linters:
188+
- dupl
189+
185190
formatters:
186191
enable:
187192
- gofmt

Diff for: pkg/commands/formatters.go

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/fatih/color"
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/viper"
9+
10+
"github.com/golangci/golangci-lint/pkg/config"
11+
"github.com/golangci/golangci-lint/pkg/goformatters"
12+
"github.com/golangci/golangci-lint/pkg/lint/linter"
13+
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
14+
"github.com/golangci/golangci-lint/pkg/logutils"
15+
)
16+
17+
type formattersOptions struct {
18+
config.LoaderOptions
19+
}
20+
21+
type formattersCommand struct {
22+
viper *viper.Viper
23+
cmd *cobra.Command
24+
25+
opts formattersOptions
26+
27+
cfg *config.Config
28+
29+
log logutils.Log
30+
31+
dbManager *lintersdb.Manager
32+
}
33+
34+
func newFormattersCommand(logger logutils.Log) *formattersCommand {
35+
c := &formattersCommand{
36+
viper: viper.New(),
37+
cfg: config.NewDefault(),
38+
log: logger,
39+
}
40+
41+
formattersCmd := &cobra.Command{
42+
Use: "formatters",
43+
Short: "List current formatters configuration",
44+
Args: cobra.NoArgs,
45+
ValidArgsFunction: cobra.NoFileCompletions,
46+
RunE: c.execute,
47+
PreRunE: c.preRunE,
48+
SilenceUsage: true,
49+
}
50+
51+
fs := formattersCmd.Flags()
52+
fs.SortFlags = false // sort them as they are defined here
53+
54+
setupConfigFileFlagSet(fs, &c.opts.LoaderOptions)
55+
setupLintersFlagSet(c.viper, fs)
56+
57+
c.cmd = formattersCmd
58+
59+
return c
60+
}
61+
62+
func (c *formattersCommand) preRunE(cmd *cobra.Command, args []string) error {
63+
loader := config.NewLoader(c.log.Child(logutils.DebugKeyConfigReader), c.viper, cmd.Flags(), c.opts.LoaderOptions, c.cfg, args)
64+
65+
err := loader.Load(config.LoadOptions{Validation: true})
66+
if err != nil {
67+
return fmt.Errorf("can't load config: %w", err)
68+
}
69+
70+
dbManager, err := lintersdb.NewManager(c.log.Child(logutils.DebugKeyLintersDB), c.cfg,
71+
lintersdb.NewLinterBuilder(), lintersdb.NewPluginModuleBuilder(c.log), lintersdb.NewPluginGoBuilder(c.log))
72+
if err != nil {
73+
return err
74+
}
75+
76+
c.dbManager = dbManager
77+
78+
return nil
79+
}
80+
81+
func (c *formattersCommand) execute(_ *cobra.Command, _ []string) error {
82+
enabledLintersMap, err := c.dbManager.GetEnabledLintersMap()
83+
if err != nil {
84+
return fmt.Errorf("can't get enabled formatters: %w", err)
85+
}
86+
87+
var enabledLinters []*linter.Config
88+
var disabledLCs []*linter.Config
89+
90+
for _, lc := range c.dbManager.GetAllSupportedLinterConfigs() {
91+
if lc.Internal {
92+
continue
93+
}
94+
95+
if !goformatters.IsFormatter(lc.Name()) {
96+
continue
97+
}
98+
99+
if enabledLintersMap[lc.Name()] == nil {
100+
disabledLCs = append(disabledLCs, lc)
101+
} else {
102+
enabledLinters = append(enabledLinters, lc)
103+
}
104+
}
105+
106+
color.Green("Enabled by your configuration formatters:\n")
107+
printFormatters(enabledLinters)
108+
color.Red("\nDisabled by your configuration formatters:\n")
109+
printFormatters(disabledLCs)
110+
111+
return nil
112+
}

Diff for: pkg/commands/help.go

+13-135
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
11
package commands
22

33
import (
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-
3215
type 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("\nDisabled 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-
19573
func formatDescription(desc string) string {
19674
desc = strings.TrimSpace(desc)
19775

0 commit comments

Comments
 (0)