Skip to content

Commit 30105a5

Browse files
committed
feat: add sort-order option
1 parent c0f89fb commit 30105a5

File tree

6 files changed

+400
-40
lines changed

6 files changed

+400
-40
lines changed

Diff for: .golangci.reference.yml

+21-1
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,34 @@ output:
109109
# Default: ""
110110
path-prefix: ""
111111

112-
# Sort results by: filepath, line and column.
112+
# Sort results.
113+
# See also `sort-order`.
113114
# Default: false
114115
sort-results: true
115116

117+
# Order to use when sorting results.
118+
# Require `sort-results` to `true`.
119+
# Possible values: `file`, `linter`, and `severity`.
120+
#
121+
# If the severity values are inside the following list, they are ordered in this order:
122+
# 1. error
123+
# 2. warning
124+
# 3. high
125+
# 4. medium
126+
# 5. low
127+
# Either they are sorted alphabetically.
128+
#
129+
# Default: ["file"]
130+
sort-order:
131+
- linter
132+
- severity
133+
- file # filepath, line, and column.
134+
116135
# Show statistics per linter.
117136
# Default: false
118137
show-stats: true
119138

139+
120140
# All available settings of specific linters.
121141
linters-settings:
122142
asasalint:

Diff for: pkg/commands/flagsets.go

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ func setupOutputFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
7171
color.GreenString("Make issues output unique by line"))
7272
internal.AddFlagAndBind(v, fs, fs.Bool, "sort-results", "output.sort-results", false,
7373
color.GreenString("Sort linter results"))
74+
internal.AddFlagAndBind(v, fs, fs.StringSlice, "sort-order", "output.sort-order", nil,
75+
color.GreenString("Sort order of linter results"))
7476
internal.AddFlagAndBind(v, fs, fs.String, "path-prefix", "output.path-prefix", "",
7577
color.GreenString("Path prefix to add to output"))
7678
internal.AddFlagAndBind(v, fs, fs.Bool, "show-stats", "output.show-stats", false, color.GreenString("Show statistics per linter"))

Diff for: pkg/config/output.go

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package config
22

3+
import (
4+
"errors"
5+
"fmt"
6+
"slices"
7+
)
8+
39
const (
410
OutFormatJSON = "json"
511
OutFormatLineNumber = "line-number"
@@ -28,11 +34,26 @@ var OutFormats = []string{
2834
}
2935

3036
type Output struct {
31-
Format string `mapstructure:"format"`
32-
PrintIssuedLine bool `mapstructure:"print-issued-lines"`
33-
PrintLinterName bool `mapstructure:"print-linter-name"`
34-
UniqByLine bool `mapstructure:"uniq-by-line"`
35-
SortResults bool `mapstructure:"sort-results"`
36-
PathPrefix string `mapstructure:"path-prefix"`
37-
ShowStats bool `mapstructure:"show-stats"`
37+
Format string `mapstructure:"format"`
38+
PrintIssuedLine bool `mapstructure:"print-issued-lines"`
39+
PrintLinterName bool `mapstructure:"print-linter-name"`
40+
UniqByLine bool `mapstructure:"uniq-by-line"`
41+
SortResults bool `mapstructure:"sort-results"`
42+
SortOrder []string `mapstructure:"sort-order"`
43+
PathPrefix string `mapstructure:"path-prefix"`
44+
ShowStats bool `mapstructure:"show-stats"`
45+
}
46+
47+
func (o *Output) Validate() error {
48+
if !o.SortResults && len(o.SortOrder) > 0 {
49+
return errors.New("sort-results should be 'true' to use sort-order")
50+
}
51+
52+
for _, s := range o.SortOrder {
53+
if !slices.Contains([]string{"linter", "file", "severity"}, s) {
54+
return fmt.Errorf("unsupported sort-order name %q", s)
55+
}
56+
}
57+
58+
return nil
3859
}

Diff for: pkg/config/output_test.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestOutput_Validate(t *testing.T) {
10+
testCases := []struct {
11+
desc string
12+
settings *Output
13+
}{
14+
{
15+
desc: "file",
16+
settings: &Output{
17+
SortResults: true,
18+
SortOrder: []string{"file"},
19+
},
20+
},
21+
{
22+
desc: "linter",
23+
settings: &Output{
24+
SortResults: true,
25+
SortOrder: []string{"linter"},
26+
},
27+
},
28+
{
29+
desc: "severity",
30+
settings: &Output{
31+
SortResults: true,
32+
SortOrder: []string{"severity"},
33+
},
34+
},
35+
}
36+
37+
for _, test := range testCases {
38+
test := test
39+
t.Run(test.desc, func(t *testing.T) {
40+
t.Parallel()
41+
42+
err := test.settings.Validate()
43+
require.NoError(t, err)
44+
})
45+
}
46+
}
47+
48+
func TestOutput_Validate_error(t *testing.T) {
49+
testCases := []struct {
50+
desc string
51+
settings *Output
52+
expected string
53+
}{
54+
{
55+
desc: "sort-results false and sort-order",
56+
settings: &Output{
57+
SortOrder: []string{"file"},
58+
},
59+
expected: "sort-results should be 'true' to use sort-order",
60+
},
61+
{
62+
desc: "invalid sort-order",
63+
settings: &Output{
64+
SortResults: true,
65+
SortOrder: []string{"a"},
66+
},
67+
expected: `unsupported sort-order name "a"`,
68+
},
69+
}
70+
71+
for _, test := range testCases {
72+
test := test
73+
t.Run(test.desc, func(t *testing.T) {
74+
t.Parallel()
75+
76+
err := test.settings.Validate()
77+
require.EqualError(t, err, test.expected)
78+
})
79+
}
80+
}

0 commit comments

Comments
 (0)