Skip to content

Commit c2de8ef

Browse files
committed
feat: upgrade logrlint to v0.2.0; add config to logrlint
Signed-off-by: Timon Wong <[email protected]>
1 parent bddc63a commit c2de8ef

File tree

11 files changed

+73
-12
lines changed

11 files changed

+73
-12
lines changed

.golangci.reference.yml

+6
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,12 @@ linters-settings:
11111111
# Default: 1
11121112
tab-width: 1
11131113

1114+
logrlint:
1115+
disable-all: false
1116+
# By default, both logr and klog will be checked.
1117+
enable: []
1118+
disable: []
1119+
11141120
maintidx:
11151121
# Show functions with maintainability index lower than N.
11161122
# A high index indicates better maintainability (it's kind of the opposite of complexity).

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ require (
9595
github.com/tdakkota/asciicheck v0.1.1
9696
github.com/tetafro/godot v1.4.11
9797
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144
98-
github.com/timonwong/logrlint v0.1.0
98+
github.com/timonwong/logrlint v0.2.0
9999
github.com/tomarrell/wrapcheck/v2 v2.6.2
100100
github.com/tommy-muehle/go-mnd/v2 v2.5.0
101101
github.com/ultraware/funlen v0.0.3

go.sum

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

+12
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ var defaultLintersSettings = LintersSettings{
6666
LineLength: 120,
6767
TabWidth: 1,
6868
},
69+
LogrLint: LogrLintSettings{
70+
DisableAll: false,
71+
Enable: nil,
72+
Disable: nil,
73+
},
6974
MaintIdx: MaintIdxSettings{
7075
Under: 20,
7176
},
@@ -154,6 +159,7 @@ type LintersSettings struct {
154159
InterfaceBloat InterfaceBloatSettings
155160
Ireturn IreturnSettings
156161
Lll LllSettings
162+
LogrLint LogrLintSettings
157163
MaintIdx MaintIdxSettings
158164
Makezero MakezeroSettings
159165
Maligned MalignedSettings
@@ -470,6 +476,12 @@ type LllSettings struct {
470476
TabWidth int `mapstructure:"tab-width"`
471477
}
472478

479+
type LogrLintSettings struct {
480+
DisableAll bool `mapstructure:"disable-all"`
481+
Enable []string `mapstructure:"enable"`
482+
Disable []string `mapstructure:"disable"`
483+
}
484+
473485
type MaintIdxSettings struct {
474486
Under int `mapstructure:"under"`
475487
}

pkg/golinters/logrlint.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
package golinters
22

33
import (
4+
"strconv"
5+
"strings"
6+
47
"github.com/timonwong/logrlint"
58
"golang.org/x/tools/go/analysis"
69

10+
"github.com/golangci/golangci-lint/pkg/config"
711
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
812
)
913

10-
func NewLogrLint() *goanalysis.Linter {
11-
a := logrlint.Analyzer
14+
func NewLogrLint(settings *config.LogrLintSettings) *goanalysis.Linter {
15+
analyzer := logrlint.NewAnalyzer()
16+
cfg := map[string]map[string]interface{}{}
17+
if settings != nil {
18+
linterCfg := map[string]interface{}{
19+
"disableall": strconv.FormatBool(settings.DisableAll),
20+
"enable": strings.Join(settings.Enable, ","),
21+
"disable": strings.Join(settings.Disable, ","),
22+
}
23+
cfg[analyzer.Name] = linterCfg
24+
}
1225

1326
return goanalysis.NewLinter(
14-
a.Name,
15-
a.Doc,
16-
[]*analysis.Analyzer{a},
17-
nil,
27+
analyzer.Name,
28+
analyzer.Doc,
29+
[]*analysis.Analyzer{analyzer},
30+
cfg,
1831
).WithLoadMode(goanalysis.LoadModeTypesInfo)
1932
}

pkg/lint/lintersdb/manager.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
140140
interfaceBloatCfg *config.InterfaceBloatSettings
141141
ireturnCfg *config.IreturnSettings
142142
lllCfg *config.LllSettings
143+
logrlintCfg *config.LogrLintSettings
143144
maintIdxCfg *config.MaintIdxSettings
144145
makezeroCfg *config.MakezeroSettings
145146
malignedCfg *config.MalignedSettings
@@ -214,6 +215,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
214215
interfaceBloatCfg = &m.cfg.LintersSettings.InterfaceBloat
215216
ireturnCfg = &m.cfg.LintersSettings.Ireturn
216217
lllCfg = &m.cfg.LintersSettings.Lll
218+
logrlintCfg = &m.cfg.LintersSettings.LogrLint
217219
maintIdxCfg = &m.cfg.LintersSettings.MaintIdx
218220
makezeroCfg = &m.cfg.LintersSettings.Makezero
219221
malignedCfg = &m.cfg.LintersSettings.Maligned
@@ -583,7 +585,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
583585
WithSince("v1.8.0").
584586
WithPresets(linter.PresetStyle),
585587

586-
linter.NewConfig(golinters.NewLogrLint()).
588+
linter.NewConfig(golinters.NewLogrLint(logrlintCfg)).
587589
WithSince("v1.49.0").
588590
WithLoadForGoAnalysis().
589591
WithPresets(linter.PresetBugs).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
linters-settings:
2+
logrlint:
3+
disable-all: true
4+
enable: [logr]

test/testdata/logrlint/go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module logrlint
22

33
go 1.16
44

5-
require github.com/go-logr/logr v1.2.3
5+
require (
6+
github.com/go-logr/logr v1.2.3
7+
k8s.io/klog/v2 v2.70.1
8+
)

test/testdata/logrlint/go.sum

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/testdata/logrlint/logrlint.go renamed to test/testdata/logrlint/logrlint_all.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import (
55
"fmt"
66

77
"github.com/go-logr/logr"
8+
"k8s.io/klog/v2"
89
)
910

10-
func Example() {
11+
func ExampleAll() {
1112
log := logr.Discard()
1213
log = log.WithValues("key") // want `odd number of arguments passed as key-value pairs for logging`
1314
log.Info("message", "key1", "value1", "key2", "value2", "key3") // want `odd number of arguments passed as key-value pairs for logging`
1415
log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging`
1516
log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2", "value2")
17+
18+
klog.InfoS("message", "key1") // want `odd number of arguments passed as key-value pairs for logging`
1619
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//golangcitest:args -Elogrlint
2+
//golangcitest:config_path configs/logrlint_check_logronly.yml
3+
package logrlint
4+
5+
import (
6+
"github.com/go-logr/logr"
7+
"k8s.io/klog/v2"
8+
)
9+
10+
func ExampleLogrOnly() {
11+
log := logr.Discard()
12+
log.Info("message", "key1", "value1", "key2", "value2", "key3") // want `odd number of arguments passed as key-value pairs for logging`
13+
14+
klog.InfoS("message", "key1")
15+
}

0 commit comments

Comments
 (0)