Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sort-order option #4467

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,33 @@ output:
# Default: ""
path-prefix: ""

# Sort results by: filepath, line and column.
# Sort results by the order defined in `sort-order`.
ldez marked this conversation as resolved.
Show resolved Hide resolved
# Default: false
sort-results: true

# Order to use when sorting results.
# Require `sort-results` to `true`.
# Possible values: `file`, `linter`, and `severity`.
#
# If the severity values are inside the following list, they are ordered in this order:
# 1. error
# 2. warning
# 3. high
# 4. medium
# 5. low
# Either they are sorted alphabetically.
#
# Default: ["file"]
sort-order:
- linter
- severity
- file # filepath, line, and column.

# Show statistics per linter.
# Default: false
show-stats: true


# All available settings of specific linters.
linters-settings:
asasalint:
Expand Down
2 changes: 2 additions & 0 deletions pkg/commands/flagsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func setupOutputFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
color.GreenString("Make issues output unique by line"))
internal.AddFlagAndBind(v, fs, fs.Bool, "sort-results", "output.sort-results", false,
color.GreenString("Sort linter results"))
internal.AddFlagAndBind(v, fs, fs.StringSlice, "sort-order", "output.sort-order", nil,
color.GreenString("Sort order of linter results"))
internal.AddFlagAndBind(v, fs, fs.String, "path-prefix", "output.path-prefix", "",
color.GreenString("Path prefix to add to output"))
internal.AddFlagAndBind(v, fs, fs.Bool, "show-stats", "output.show-stats", false, color.GreenString("Show statistics per linter"))
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (c *Config) Validate() error {
c.Severity.Validate,
c.LintersSettings.Validate,
c.Linters.Validate,
c.Output.Validate,
}

for _, v := range validators {
Expand Down
35 changes: 28 additions & 7 deletions pkg/config/output.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package config

import (
"errors"
"fmt"
"slices"
)

const (
OutFormatJSON = "json"
OutFormatLineNumber = "line-number"
Expand Down Expand Up @@ -28,11 +34,26 @@ var OutFormats = []string{
}

type Output struct {
Format string `mapstructure:"format"`
PrintIssuedLine bool `mapstructure:"print-issued-lines"`
PrintLinterName bool `mapstructure:"print-linter-name"`
UniqByLine bool `mapstructure:"uniq-by-line"`
SortResults bool `mapstructure:"sort-results"`
PathPrefix string `mapstructure:"path-prefix"`
ShowStats bool `mapstructure:"show-stats"`
Format string `mapstructure:"format"`
PrintIssuedLine bool `mapstructure:"print-issued-lines"`
PrintLinterName bool `mapstructure:"print-linter-name"`
UniqByLine bool `mapstructure:"uniq-by-line"`
SortResults bool `mapstructure:"sort-results"`
SortOrder []string `mapstructure:"sort-order"`
PathPrefix string `mapstructure:"path-prefix"`
ShowStats bool `mapstructure:"show-stats"`
}

func (o *Output) Validate() error {
if !o.SortResults && len(o.SortOrder) > 0 {
return errors.New("sort-results should be 'true' to use sort-order")
}

for _, order := range o.SortOrder {
ldez marked this conversation as resolved.
Show resolved Hide resolved
if !slices.Contains([]string{"linter", "file", "severity"}, order) {
return fmt.Errorf("unsupported sort-order name %q", order)
}
}

return nil
}
80 changes: 80 additions & 0 deletions pkg/config/output_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package config

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestOutput_Validate(t *testing.T) {
testCases := []struct {
desc string
settings *Output
}{
{
desc: "file",
settings: &Output{
SortResults: true,
SortOrder: []string{"file"},
},
},
{
desc: "linter",
settings: &Output{
SortResults: true,
SortOrder: []string{"linter"},
},
},
{
desc: "severity",
settings: &Output{
SortResults: true,
SortOrder: []string{"severity"},
},
},
}

for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()

err := test.settings.Validate()
require.NoError(t, err)
})
}
}

func TestOutput_Validate_error(t *testing.T) {
testCases := []struct {
desc string
settings *Output
expected string
}{
{
desc: "sort-results false and sort-order",
settings: &Output{
SortOrder: []string{"file"},
},
expected: "sort-results should be 'true' to use sort-order",
},
{
desc: "invalid sort-order",
settings: &Output{
SortResults: true,
SortOrder: []string{"a"},
},
expected: `unsupported sort-order name "a"`,
},
}

for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()

err := test.settings.Validate()
require.EqualError(t, err, test.expected)
})
}
}
Loading
Loading