1
1
package commands
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
6
7
"github.com/fatih/color"
@@ -14,8 +15,14 @@ import (
14
15
"github.com/golangci/golangci-lint/pkg/logutils"
15
16
)
16
17
18
+ type lintersHelp struct {
19
+ Enabled []linterHelp
20
+ Disabled []linterHelp
21
+ }
22
+
17
23
type lintersOptions struct {
18
24
config.LoaderOptions
25
+ JSON bool
19
26
}
20
27
21
28
type lintersCommand struct {
@@ -54,6 +61,8 @@ func newLintersCommand(logger logutils.Log) *lintersCommand {
54
61
setupConfigFileFlagSet (fs , & c .opts .LoaderOptions )
55
62
setupLintersFlagSet (c .viper , fs )
56
63
64
+ fs .BoolVar (& c .opts .JSON , "json" , false , color .GreenString ("Display as JSON" ))
65
+
57
66
c .cmd = lintersCmd
58
67
59
68
return c
@@ -85,7 +94,7 @@ func (c *lintersCommand) execute(_ *cobra.Command, _ []string) error {
85
94
}
86
95
87
96
var enabledLinters []* linter.Config
88
- var disabledLCs []* linter.Config
97
+ var disabledLinters []* linter.Config
89
98
90
99
for _ , lc := range c .dbManager .GetAllSupportedLinterConfigs () {
91
100
if lc .Internal {
@@ -97,16 +106,31 @@ func (c *lintersCommand) execute(_ *cobra.Command, _ []string) error {
97
106
}
98
107
99
108
if enabledLintersMap [lc .Name ()] == nil {
100
- disabledLCs = append (disabledLCs , lc )
109
+ disabledLinters = append (disabledLinters , lc )
101
110
} else {
102
111
enabledLinters = append (enabledLinters , lc )
103
112
}
104
113
}
105
114
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
+
106
129
color .Green ("Enabled by your configuration linters:\n " )
107
130
printLinters (enabledLinters )
131
+
108
132
color .Red ("\n Disabled by your configuration linters:\n " )
109
- printLinters (disabledLCs )
133
+ printLinters (disabledLinters )
110
134
111
135
return nil
112
136
}
0 commit comments