From dfc6fb3c83d1ee1eff9d3f3731747a8e92f29718 Mon Sep 17 00:00:00 2001 From: Cody Ley-Han Date: Wed, 4 Sep 2019 23:29:38 -0700 Subject: [PATCH] Add user supplied error messages in depguard issues --- .golangci.yml | 2 ++ README.md | 2 ++ pkg/config/config.go | 7 ++++--- pkg/golinters/depguard.go | 22 +++++++++++++++++++++- test/testdata/configs/depguard.yml | 7 +++++++ test/testdata/depguard.go | 5 ++--- 6 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 test/testdata/configs/depguard.yml diff --git a/.golangci.yml b/.golangci.yml index 85b508e6c6c0..1c20e737a3a6 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -25,6 +25,8 @@ linters-settings: # logging is allowed only by logutils.Log, logrus # is allowed to use only in logutils package - github.com/sirupsen/logrus + packages-with-error-messages: + github.com/sirupsen/logrus: "logging is allowed only by logutils.Log" misspell: locale: US lll: diff --git a/README.md b/README.md index e7f4be6bbc51..72f324226ec6 100644 --- a/README.md +++ b/README.md @@ -837,6 +837,8 @@ linters-settings: # logging is allowed only by logutils.Log, logrus # is allowed to use only in logutils package - github.com/sirupsen/logrus + packages-with-error-messages: + github.com/sirupsen/logrus: "logging is allowed only by logutils.Log" misspell: locale: US lll: diff --git a/pkg/config/config.go b/pkg/config/config.go index 9a113fe6d892..e9dd3be2c2ca 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -153,9 +153,10 @@ type LintersSettings struct { MinOccurrencesCount int `mapstructure:"min-occurrences"` } Depguard struct { - ListType string `mapstructure:"list-type"` - Packages []string - IncludeGoRoot bool `mapstructure:"include-go-root"` + ListType string `mapstructure:"list-type"` + Packages []string + IncludeGoRoot bool `mapstructure:"include-go-root"` + PackagesWithErrorMessage map[string]string `mapstructure:"packages-with-error-message"` } Misspell struct { Locale string diff --git a/pkg/golinters/depguard.go b/pkg/golinters/depguard.go index a00af5c9f1ac..d3760bfbbbe9 100644 --- a/pkg/golinters/depguard.go +++ b/pkg/golinters/depguard.go @@ -36,6 +36,22 @@ func (d Depguard) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Is dg.ListType = depguardAPI.LTBlacklist } + if dg.ListType == depguardAPI.LTBlacklist { + // if the list type was a blacklist the packages with error messages should + // be included in the blacklist package list + + noMessagePackages := make(map[string]bool) + for _, pkg := range dg.Packages { + noMessagePackages[pkg] = true + } + + for pkg := range lintCtx.Settings().Depguard.PackagesWithErrorMessage { + if _, ok := noMessagePackages[pkg]; !ok { + dg.Packages = append(dg.Packages, pkg) + } + } + } + issues, err := dg.Run(lintCtx.LoaderConfig, lintCtx.Program) if err != nil { return nil, err @@ -49,9 +65,13 @@ func (d Depguard) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Is } res := make([]result.Issue, 0, len(issues)) for _, i := range issues { + userSuppliedMsgSuffix := lintCtx.Settings().Depguard.PackagesWithErrorMessage[i.PackageName] + if userSuppliedMsgSuffix != "" { + userSuppliedMsgSuffix = ": " + userSuppliedMsgSuffix + } res = append(res, result.Issue{ Pos: i.Position, - Text: fmt.Sprintf("%s %s", formatCode(i.PackageName, lintCtx.Cfg), msgSuffix), + Text: fmt.Sprintf("%s %s%s", formatCode(i.PackageName, lintCtx.Cfg), msgSuffix, userSuppliedMsgSuffix), FromLinter: d.Name(), }) } diff --git a/test/testdata/configs/depguard.yml b/test/testdata/configs/depguard.yml new file mode 100644 index 000000000000..aa06e3ee9e61 --- /dev/null +++ b/test/testdata/configs/depguard.yml @@ -0,0 +1,7 @@ +linters-settings: + depguard: + include-go-root: true + packages: + - compress/* + packages-with-error-message: + log: "don't use log" diff --git a/test/testdata/depguard.go b/test/testdata/depguard.go index 7613fc7a3267..e1ab70613be0 100644 --- a/test/testdata/depguard.go +++ b/test/testdata/depguard.go @@ -1,11 +1,10 @@ //args: -Edepguard -//config: linters-settings.depguard.include-go-root=true -//config: linters-settings.depguard.packages=compress/*,log +//config_path: testdata/configs/depguard.yml package testdata import ( "compress/gzip" // ERROR "`compress/gzip` is in the blacklist" - "log" // ERROR "`log` is in the blacklist" + "log" // ERROR "`log` is in the blacklist: don't use log" ) func SpewDebugInfo() {