From 5faffb8e16237fa5407da8cd8c957875cd2c63ae Mon Sep 17 00:00:00 2001 From: Timon Wong Date: Fri, 26 Aug 2022 11:05:13 +0800 Subject: [PATCH] feat: upgrade logrlint to v0.3.0; add config to logrlint Signed-off-by: Timon Wong --- .golangci.reference.yml | 6 +++++ go.mod | 2 +- go.sum | 4 +-- pkg/config/linters_settings.go | 12 +++++++++ pkg/golinters/logrlint.go | 25 ++++++++++++++----- pkg/lint/lintersdb/manager.go | 4 ++- .../configs/logrlint_check_logronly.yml | 4 +++ test/testdata/logrlint/go.mod | 5 +++- test/testdata/logrlint/go.sum | 3 +++ .../logrlint/{logrlint.go => logrlint_all.go} | 5 +++- test/testdata/logrlint/logrlint_logronly.go | 15 +++++++++++ 11 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 test/testdata/logrlint/configs/logrlint_check_logronly.yml rename test/testdata/logrlint/{logrlint.go => logrlint_all.go} (81%) create mode 100644 test/testdata/logrlint/logrlint_logronly.go diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 1003fc111626..068139348ade 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1111,6 +1111,12 @@ linters-settings: # Default: 1 tab-width: 1 + logrlint: + disable-all: false + # By default, both logr and klog will be checked. + enable: [] + disable: [] + maintidx: # Show functions with maintainability index lower than N. # A high index indicates better maintainability (it's kind of the opposite of complexity). diff --git a/go.mod b/go.mod index cff3b89fc920..21e5de1c87f7 100644 --- a/go.mod +++ b/go.mod @@ -95,7 +95,7 @@ require ( github.com/tdakkota/asciicheck v0.1.1 github.com/tetafro/godot v1.4.11 github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144 - github.com/timonwong/logrlint v0.1.0 + github.com/timonwong/logrlint v0.3.0 github.com/tomarrell/wrapcheck/v2 v2.6.2 github.com/tommy-muehle/go-mnd/v2 v2.5.0 github.com/ultraware/funlen v0.0.3 diff --git a/go.sum b/go.sum index cb8bbdd72363..e30e16aa9f05 100644 --- a/go.sum +++ b/go.sum @@ -532,8 +532,8 @@ github.com/tetafro/godot v1.4.11 h1:BVoBIqAf/2QdbFmSwAWnaIqDivZdOV0ZRwEm6jivLKw= github.com/tetafro/godot v1.4.11/go.mod h1:LR3CJpxDVGlYOWn3ZZg1PgNZdTUvzsZWu8xaEohUpn8= github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144 h1:kl4KhGNsJIbDHS9/4U9yQo1UcPQM0kOMJHn29EoH/Ro= github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk= -github.com/timonwong/logrlint v0.1.0 h1:phZCcypL/vtx6cGxObJgWZ5wexZF5SXFPLOM+ru0e/M= -github.com/timonwong/logrlint v0.1.0/go.mod h1:Zleg4Gw+kRxNej+Ra7o+tEaW5k1qthTaYKU7rSD39LU= +github.com/timonwong/logrlint v0.3.0 h1:PO/nT7Ww5vvpHBfqG4M0vyme4CwmbwIrS/JjMvTiG8s= +github.com/timonwong/logrlint v0.3.0/go.mod h1:5V+qRnjYXE74IDzxox865j/FoTfEEoJDyV0yT3RMu6g= github.com/tklauser/go-sysconf v0.3.10 h1:IJ1AZGZRWbY8T5Vfk04D9WOA5WSejdflXxP03OUqALw= github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYaQ2hGm7jmxEFk= github.com/tklauser/numcpus v0.4.0 h1:E53Dm1HjH1/R2/aoCtXtPgzmElmn51aOkhCFSuZq//o= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index f1c36c380d60..c24c4c039747 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -66,6 +66,11 @@ var defaultLintersSettings = LintersSettings{ LineLength: 120, TabWidth: 1, }, + LogrLint: LogrLintSettings{ + DisableAll: false, + Enable: nil, + Disable: nil, + }, MaintIdx: MaintIdxSettings{ Under: 20, }, @@ -154,6 +159,7 @@ type LintersSettings struct { InterfaceBloat InterfaceBloatSettings Ireturn IreturnSettings Lll LllSettings + LogrLint LogrLintSettings MaintIdx MaintIdxSettings Makezero MakezeroSettings Maligned MalignedSettings @@ -470,6 +476,12 @@ type LllSettings struct { TabWidth int `mapstructure:"tab-width"` } +type LogrLintSettings struct { + DisableAll bool `mapstructure:"disable-all"` + Enable []string `mapstructure:"enable"` + Disable []string `mapstructure:"disable"` +} + type MaintIdxSettings struct { Under int `mapstructure:"under"` } diff --git a/pkg/golinters/logrlint.go b/pkg/golinters/logrlint.go index 9899808c7476..d098358ec706 100644 --- a/pkg/golinters/logrlint.go +++ b/pkg/golinters/logrlint.go @@ -1,19 +1,32 @@ package golinters import ( + "strconv" + "strings" + "github.com/timonwong/logrlint" "golang.org/x/tools/go/analysis" + "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" ) -func NewLogrLint() *goanalysis.Linter { - a := logrlint.Analyzer +func NewLogrLint(settings *config.LogrLintSettings) *goanalysis.Linter { + analyzer := logrlint.NewAnalyzer() + cfg := map[string]map[string]interface{}{} + if settings != nil { + linterCfg := map[string]interface{}{ + "disableall": strconv.FormatBool(settings.DisableAll), + "enable": strings.Join(settings.Enable, ","), + "disable": strings.Join(settings.Disable, ","), + } + cfg[analyzer.Name] = linterCfg + } return goanalysis.NewLinter( - a.Name, - a.Doc, - []*analysis.Analyzer{a}, - nil, + analyzer.Name, + analyzer.Doc, + []*analysis.Analyzer{analyzer}, + cfg, ).WithLoadMode(goanalysis.LoadModeTypesInfo) } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index b17ce3befd79..c95aee32e970 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -140,6 +140,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { interfaceBloatCfg *config.InterfaceBloatSettings ireturnCfg *config.IreturnSettings lllCfg *config.LllSettings + logrlintCfg *config.LogrLintSettings maintIdxCfg *config.MaintIdxSettings makezeroCfg *config.MakezeroSettings malignedCfg *config.MalignedSettings @@ -214,6 +215,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { interfaceBloatCfg = &m.cfg.LintersSettings.InterfaceBloat ireturnCfg = &m.cfg.LintersSettings.Ireturn lllCfg = &m.cfg.LintersSettings.Lll + logrlintCfg = &m.cfg.LintersSettings.LogrLint maintIdxCfg = &m.cfg.LintersSettings.MaintIdx makezeroCfg = &m.cfg.LintersSettings.Makezero malignedCfg = &m.cfg.LintersSettings.Maligned @@ -583,7 +585,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithSince("v1.8.0"). WithPresets(linter.PresetStyle), - linter.NewConfig(golinters.NewLogrLint()). + linter.NewConfig(golinters.NewLogrLint(logrlintCfg)). WithSince("v1.49.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs). diff --git a/test/testdata/logrlint/configs/logrlint_check_logronly.yml b/test/testdata/logrlint/configs/logrlint_check_logronly.yml new file mode 100644 index 000000000000..e2a24cb2cfdd --- /dev/null +++ b/test/testdata/logrlint/configs/logrlint_check_logronly.yml @@ -0,0 +1,4 @@ +linters-settings: + logrlint: + disable-all: true + enable: [logr] diff --git a/test/testdata/logrlint/go.mod b/test/testdata/logrlint/go.mod index a9d8d16ad374..092ffc542349 100644 --- a/test/testdata/logrlint/go.mod +++ b/test/testdata/logrlint/go.mod @@ -2,4 +2,7 @@ module logrlint go 1.16 -require github.com/go-logr/logr v1.2.3 +require ( + github.com/go-logr/logr v1.2.3 + k8s.io/klog/v2 v2.70.1 +) diff --git a/test/testdata/logrlint/go.sum b/test/testdata/logrlint/go.sum index 6da913857d04..355fc4a08482 100644 --- a/test/testdata/logrlint/go.sum +++ b/test/testdata/logrlint/go.sum @@ -1,2 +1,5 @@ +github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +k8s.io/klog/v2 v2.70.1 h1:7aaoSdahviPmR+XkS7FyxlkkXs6tHISSG03RxleQAVQ= +k8s.io/klog/v2 v2.70.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= diff --git a/test/testdata/logrlint/logrlint.go b/test/testdata/logrlint/logrlint_all.go similarity index 81% rename from test/testdata/logrlint/logrlint.go rename to test/testdata/logrlint/logrlint_all.go index 6277dea4e146..b5b21650e9ea 100644 --- a/test/testdata/logrlint/logrlint.go +++ b/test/testdata/logrlint/logrlint_all.go @@ -5,12 +5,15 @@ import ( "fmt" "github.com/go-logr/logr" + "k8s.io/klog/v2" ) -func Example() { +func ExampleAll() { log := logr.Discard() log = log.WithValues("key") // want `odd number of arguments passed as key-value pairs for logging` log.Info("message", "key1", "value1", "key2", "value2", "key3") // want `odd number of arguments passed as key-value pairs for logging` log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging` log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2", "value2") + + klog.InfoS("message", "key1") // want `odd number of arguments passed as key-value pairs for logging` } diff --git a/test/testdata/logrlint/logrlint_logronly.go b/test/testdata/logrlint/logrlint_logronly.go new file mode 100644 index 000000000000..76b0805561a0 --- /dev/null +++ b/test/testdata/logrlint/logrlint_logronly.go @@ -0,0 +1,15 @@ +//golangcitest:args -Elogrlint +//golangcitest:config_path configs/logrlint_check_logronly.yml +package logrlint + +import ( + "github.com/go-logr/logr" + "k8s.io/klog/v2" +) + +func ExampleLogrOnly() { + log := logr.Discard() + log.Info("message", "key1", "value1", "key2", "value2", "key3") // want `odd number of arguments passed as key-value pairs for logging` + + klog.InfoS("message", "key1") +}