Skip to content

Commit fb7cc99

Browse files
authored
feat: use homogeneous JSON flags (#5522)
1 parent 0bd493b commit fb7cc99

File tree

7 files changed

+114
-62
lines changed

7 files changed

+114
-62
lines changed

.github/workflows/pr.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,21 @@ jobs:
148148

149149
- run: ./golangci-lint config
150150
- run: ./golangci-lint config path
151+
- run: ./golangci-lint config path --json
151152
# TODO(ldez) after v2: golangci.next.jsonschema.json -> golangci.jsonschema.json
152153
- run: ./golangci-lint config verify --schema jsonschema/golangci.next.jsonschema.json
153154

154155
- run: ./golangci-lint help
155156
- run: ./golangci-lint help linters
156157
- run: ./golangci-lint help linters --json
158+
- run: ./golangci-lint help formatters
157159
- run: ./golangci-lint help formatters --json
158160
- run: ./golangci-lint linters
161+
- run: ./golangci-lint linters --json
159162
- run: ./golangci-lint formatters
163+
- run: ./golangci-lint formatters --json
160164
- run: ./golangci-lint version
161-
- run: ./golangci-lint version --format json
165+
- run: ./golangci-lint version --short
166+
- run: ./golangci-lint version --debug
167+
- run: ./golangci-lint version --json
168+
- run: ./golangci-lint version --json --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+
linters := lintersHelp{}
117+
118+
for _, lc := range enabledLinters {
119+
linters.Enabled = append(linters.Enabled, newLinterHelp(lc))
120+
}
121+
122+
for _, lc := range disabledLinters {
123+
linters.Disabled = append(linters.Disabled, newLinterHelp(lc))
124+
}
125+
126+
return json.NewEncoder(c.cmd.OutOrStdout()).Encode(linters)
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)

pkg/commands/version.go

+23-34
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,28 @@ import (
66
"io"
77
"os"
88
"runtime/debug"
9-
"strings"
109

1110
"github.com/fatih/color"
1211
"github.com/spf13/cobra"
1312
)
1413

1514
type BuildInfo struct {
16-
GoVersion string `json:"goVersion"`
17-
Version string `json:"version"`
18-
Commit string `json:"commit"`
19-
Date string `json:"date"`
15+
GoVersion string `json:"goVersion"`
16+
Version string `json:"version"`
17+
Commit string `json:"commit"`
18+
Date string `json:"date"`
19+
BuildInfo *debug.BuildInfo `json:"buildInfo,omitempty"`
2020
}
2121

2222
func (b BuildInfo) String() string {
2323
return fmt.Sprintf("golangci-lint has version %s built with %s from %s on %s",
2424
b.Version, b.GoVersion, b.Commit, b.Date)
2525
}
2626

27-
type versionInfo struct {
28-
Info BuildInfo
29-
BuildInfo *debug.BuildInfo
30-
}
31-
3227
type versionOptions struct {
33-
Format string
34-
Debug bool
28+
Debug bool
29+
JSON bool
30+
Short bool
3531
}
3632

3733
type versionCommand struct {
@@ -55,43 +51,36 @@ func newVersionCommand(info BuildInfo) *versionCommand {
5551
fs := versionCmd.Flags()
5652
fs.SortFlags = false // sort them as they are defined here
5753

58-
fs.StringVar(&c.opts.Format, "format", "", color.GreenString("The version's format can be: 'short', 'json'"))
5954
fs.BoolVar(&c.opts.Debug, "debug", false, color.GreenString("Add build information"))
55+
fs.BoolVar(&c.opts.JSON, "json", false, color.GreenString("Display as JSON"))
56+
fs.BoolVar(&c.opts.Short, "short", false, color.GreenString("Display only the version number"))
6057

6158
c.cmd = versionCmd
6259

6360
return c
6461
}
6562

6663
func (c *versionCommand) execute(_ *cobra.Command, _ []string) error {
64+
var info *debug.BuildInfo
6765
if c.opts.Debug {
68-
info, ok := debug.ReadBuildInfo()
69-
if !ok {
70-
return nil
71-
}
72-
73-
switch strings.ToLower(c.opts.Format) {
74-
case "json":
75-
return json.NewEncoder(os.Stdout).Encode(versionInfo{
76-
Info: c.info,
77-
BuildInfo: info,
78-
})
79-
80-
default:
81-
fmt.Println(info.String())
82-
return printVersion(os.Stdout, c.info)
83-
}
66+
info, _ = debug.ReadBuildInfo()
8467
}
8568

86-
switch strings.ToLower(c.opts.Format) {
87-
case "short":
88-
fmt.Println(c.info.Version)
89-
return nil
69+
switch {
70+
case c.opts.JSON:
71+
c.info.BuildInfo = info
9072

91-
case "json":
9273
return json.NewEncoder(os.Stdout).Encode(c.info)
74+
case c.opts.Short:
75+
fmt.Println(c.info.Version)
76+
77+
return nil
9378

9479
default:
80+
if info != nil {
81+
fmt.Println(info.String())
82+
}
83+
9584
return printVersion(os.Stdout, c.info)
9685
}
9786
}

0 commit comments

Comments
 (0)