Skip to content

Commit 0e0476f

Browse files
committed
feat: commands formatters/linters: use homogeneous flags
1 parent de4c186 commit 0e0476f

File tree

6 files changed

+86
-27
lines changed

6 files changed

+86
-27
lines changed

.github/workflows/pr.yml

+3
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,12 @@ jobs:
154154
- run: ./golangci-lint help
155155
- run: ./golangci-lint help linters
156156
- run: ./golangci-lint help linters --json
157+
- run: ./golangci-lint help formatters
157158
- run: ./golangci-lint help formatters --json
158159
- run: ./golangci-lint linters
160+
- run: ./golangci-lint linters --json
159161
- run: ./golangci-lint formatters
162+
- run: ./golangci-lint formatters --json
160163
- run: ./golangci-lint version
161164
- run: ./golangci-lint version --short
162165
- run: ./golangci-lint version --debug

pkg/commands/formatters.go

+24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"encoding/json"
45
"fmt"
56

67
"github.com/fatih/color"
@@ -14,8 +15,14 @@ import (
1415
"github.com/golangci/golangci-lint/pkg/logutils"
1516
)
1617

18+
type formattersHelp struct {
19+
Enabled []formatterHelp
20+
Disabled []formatterHelp
21+
}
22+
1723
type formattersOptions struct {
1824
config.LoaderOptions
25+
JSON bool
1926
}
2027

2128
type formattersCommand struct {
@@ -54,6 +61,8 @@ func newFormattersCommand(logger logutils.Log) *formattersCommand {
5461
setupConfigFileFlagSet(fs, &c.opts.LoaderOptions)
5562
setupLintersFlagSet(c.viper, fs)
5663

64+
fs.BoolVar(&c.opts.JSON, "json", false, color.GreenString("Display as JSON"))
65+
5766
c.cmd = formattersCmd
5867

5968
return c
@@ -103,8 +112,23 @@ func (c *formattersCommand) execute(_ *cobra.Command, _ []string) error {
103112
}
104113
}
105114

115+
if c.opts.JSON {
116+
formatters := formattersHelp{}
117+
118+
for _, lc := range enabledFormatters {
119+
formatters.Enabled = append(formatters.Enabled, newFormatterHelp(lc))
120+
}
121+
122+
for _, lc := range disabledFormatters {
123+
formatters.Disabled = append(formatters.Disabled, newFormatterHelp(lc))
124+
}
125+
126+
return json.NewEncoder(c.cmd.OutOrStdout()).Encode(formatters)
127+
}
128+
106129
color.Green("Enabled by your configuration formatters:\n")
107130
printFormatters(enabledFormatters)
131+
108132
color.Red("\nDisabled by your configuration formatters:\n")
109133
printFormatters(disabledFormatters)
110134

pkg/commands/help_formatters.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ type formatterHelp struct {
2424
OriginalURL string `json:"originalURL,omitempty"`
2525
}
2626

27+
func newFormatterHelp(lc *linter.Config) formatterHelp {
28+
return formatterHelp{
29+
Name: lc.Name(),
30+
Desc: formatDescription(lc.Linter.Desc()),
31+
Deprecated: lc.IsDeprecated(),
32+
Since: lc.Since,
33+
OriginalURL: lc.OriginalURL,
34+
}
35+
}
36+
2737
func (c *helpCommand) formattersPreRunE(_ *cobra.Command, _ []string) error {
2838
// The command doesn't depend on the real configuration.
2939
dbManager, err := lintersdb.NewManager(c.log.Child(logutils.DebugKeyLintersDB), config.NewDefault(), lintersdb.NewLinterBuilder())
@@ -58,13 +68,7 @@ func (c *helpCommand) formattersPrintJSON() error {
5868
continue
5969
}
6070

61-
formatters = append(formatters, formatterHelp{
62-
Name: lc.Name(),
63-
Desc: formatDescription(lc.Linter.Desc()),
64-
Deprecated: lc.IsDeprecated(),
65-
Since: lc.Since,
66-
OriginalURL: lc.OriginalURL,
67-
})
71+
formatters = append(formatters, newFormatterHelp(lc))
6872
}
6973

7074
return json.NewEncoder(c.cmd.OutOrStdout()).Encode(formatters)

pkg/commands/help_linters.go

+20-16
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ type linterHelp struct {
2828
OriginalURL string `json:"originalURL,omitempty"`
2929
}
3030

31+
func newLinterHelp(lc *linter.Config) linterHelp {
32+
groups := []string{config.GroupAll}
33+
34+
if !lc.IsSlowLinter() {
35+
groups = append(groups, config.GroupFast)
36+
}
37+
38+
return linterHelp{
39+
Name: lc.Name(),
40+
Desc: formatDescription(lc.Linter.Desc()),
41+
Groups: slices.Concat(groups, slices.Collect(maps.Keys(lc.Groups))),
42+
Fast: !lc.IsSlowLinter(),
43+
AutoFix: lc.CanAutoFix,
44+
Deprecated: lc.IsDeprecated(),
45+
Since: lc.Since,
46+
OriginalURL: lc.OriginalURL,
47+
}
48+
}
49+
3150
func (c *helpCommand) lintersPreRunE(_ *cobra.Command, _ []string) error {
3251
// The command doesn't depend on the real configuration.
3352
dbManager, err := lintersdb.NewManager(c.log.Child(logutils.DebugKeyLintersDB), config.NewDefault(), lintersdb.NewLinterBuilder())
@@ -62,22 +81,7 @@ func (c *helpCommand) lintersPrintJSON() error {
6281
continue
6382
}
6483

65-
groups := []string{config.GroupAll}
66-
67-
if !lc.IsSlowLinter() {
68-
groups = append(groups, config.GroupFast)
69-
}
70-
71-
linters = append(linters, linterHelp{
72-
Name: lc.Name(),
73-
Desc: formatDescription(lc.Linter.Desc()),
74-
Groups: slices.Concat(groups, slices.Collect(maps.Keys(lc.Groups))),
75-
Fast: !lc.IsSlowLinter(),
76-
AutoFix: lc.CanAutoFix,
77-
Deprecated: lc.IsDeprecated(),
78-
Since: lc.Since,
79-
OriginalURL: lc.OriginalURL,
80-
})
84+
linters = append(linters, newLinterHelp(lc))
8185
}
8286

8387
return json.NewEncoder(c.cmd.OutOrStdout()).Encode(linters)

pkg/commands/linters.go

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"encoding/json"
45
"fmt"
56

67
"github.com/fatih/color"
@@ -14,8 +15,14 @@ import (
1415
"github.com/golangci/golangci-lint/pkg/logutils"
1516
)
1617

18+
type lintersHelp struct {
19+
Enabled []linterHelp
20+
Disabled []linterHelp
21+
}
22+
1723
type lintersOptions struct {
1824
config.LoaderOptions
25+
JSON bool
1926
}
2027

2128
type lintersCommand struct {
@@ -54,6 +61,8 @@ func newLintersCommand(logger logutils.Log) *lintersCommand {
5461
setupConfigFileFlagSet(fs, &c.opts.LoaderOptions)
5562
setupLintersFlagSet(c.viper, fs)
5663

64+
fs.BoolVar(&c.opts.JSON, "json", false, color.GreenString("Display as JSON"))
65+
5766
c.cmd = lintersCmd
5867

5968
return c
@@ -85,7 +94,7 @@ func (c *lintersCommand) execute(_ *cobra.Command, _ []string) error {
8594
}
8695

8796
var enabledLinters []*linter.Config
88-
var disabledLCs []*linter.Config
97+
var disabledLinters []*linter.Config
8998

9099
for _, lc := range c.dbManager.GetAllSupportedLinterConfigs() {
91100
if lc.Internal {
@@ -97,16 +106,31 @@ func (c *lintersCommand) execute(_ *cobra.Command, _ []string) error {
97106
}
98107

99108
if enabledLintersMap[lc.Name()] == nil {
100-
disabledLCs = append(disabledLCs, lc)
109+
disabledLinters = append(disabledLinters, lc)
101110
} else {
102111
enabledLinters = append(enabledLinters, lc)
103112
}
104113
}
105114

115+
if c.opts.JSON {
116+
formatters := lintersHelp{}
117+
118+
for _, lc := range enabledLinters {
119+
formatters.Enabled = append(formatters.Enabled, newLinterHelp(lc))
120+
}
121+
122+
for _, lc := range disabledLinters {
123+
formatters.Disabled = append(formatters.Disabled, newLinterHelp(lc))
124+
}
125+
126+
return json.NewEncoder(c.cmd.OutOrStdout()).Encode(formatters)
127+
}
128+
106129
color.Green("Enabled by your configuration linters:\n")
107130
printLinters(enabledLinters)
131+
108132
color.Red("\nDisabled by your configuration linters:\n")
109-
printLinters(disabledLCs)
133+
printLinters(disabledLinters)
110134

111135
return nil
112136
}

pkg/commands/migrate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (c *migrateCommand) execute(_ *cobra.Command, _ []string) error {
134134

135135
func (c *migrateCommand) preRunE(cmd *cobra.Command, _ []string) error {
136136
switch strings.ToLower(c.opts.format) {
137-
case "", "yml", "yaml", "toml", "json": //nolint:goconst // Constants are useless in this context.
137+
case "", "yml", "yaml", "toml", "json":
138138
// Valid format.
139139
default:
140140
return fmt.Errorf("unsupported format: %s", c.opts.format)

0 commit comments

Comments
 (0)