Skip to content

Commit a8f2c27

Browse files
codyleyhanjirfag
authored andcommitted
Add user supplied error messages in depguard issues (#662)
1 parent a02e3f5 commit a8f2c27

File tree

6 files changed

+38
-7
lines changed

6 files changed

+38
-7
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ linters-settings:
2525
# logging is allowed only by logutils.Log, logrus
2626
# is allowed to use only in logutils package
2727
- github.com/sirupsen/logrus
28+
packages-with-error-messages:
29+
github.com/sirupsen/logrus: "logging is allowed only by logutils.Log"
2830
misspell:
2931
locale: US
3032
lll:

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,8 @@ linters-settings:
845845
# logging is allowed only by logutils.Log, logrus
846846
# is allowed to use only in logutils package
847847
- github.com/sirupsen/logrus
848+
packages-with-error-messages:
849+
github.com/sirupsen/logrus: "logging is allowed only by logutils.Log"
848850
misspell:
849851
locale: US
850852
lll:

pkg/config/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ type LintersSettings struct {
153153
MinOccurrencesCount int `mapstructure:"min-occurrences"`
154154
}
155155
Depguard struct {
156-
ListType string `mapstructure:"list-type"`
157-
Packages []string
158-
IncludeGoRoot bool `mapstructure:"include-go-root"`
156+
ListType string `mapstructure:"list-type"`
157+
Packages []string
158+
IncludeGoRoot bool `mapstructure:"include-go-root"`
159+
PackagesWithErrorMessage map[string]string `mapstructure:"packages-with-error-message"`
159160
}
160161
Misspell struct {
161162
Locale string

pkg/golinters/depguard.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ func (d Depguard) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Is
3636
dg.ListType = depguardAPI.LTBlacklist
3737
}
3838

39+
if dg.ListType == depguardAPI.LTBlacklist {
40+
// if the list type was a blacklist the packages with error messages should
41+
// be included in the blacklist package list
42+
43+
noMessagePackages := make(map[string]bool)
44+
for _, pkg := range dg.Packages {
45+
noMessagePackages[pkg] = true
46+
}
47+
48+
for pkg := range lintCtx.Settings().Depguard.PackagesWithErrorMessage {
49+
if _, ok := noMessagePackages[pkg]; !ok {
50+
dg.Packages = append(dg.Packages, pkg)
51+
}
52+
}
53+
}
54+
3955
issues, err := dg.Run(lintCtx.LoaderConfig, lintCtx.Program)
4056
if err != nil {
4157
return nil, err
@@ -49,9 +65,13 @@ func (d Depguard) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Is
4965
}
5066
res := make([]result.Issue, 0, len(issues))
5167
for _, i := range issues {
68+
userSuppliedMsgSuffix := lintCtx.Settings().Depguard.PackagesWithErrorMessage[i.PackageName]
69+
if userSuppliedMsgSuffix != "" {
70+
userSuppliedMsgSuffix = ": " + userSuppliedMsgSuffix
71+
}
5272
res = append(res, result.Issue{
5373
Pos: i.Position,
54-
Text: fmt.Sprintf("%s %s", formatCode(i.PackageName, lintCtx.Cfg), msgSuffix),
74+
Text: fmt.Sprintf("%s %s%s", formatCode(i.PackageName, lintCtx.Cfg), msgSuffix, userSuppliedMsgSuffix),
5575
FromLinter: d.Name(),
5676
})
5777
}

test/testdata/configs/depguard.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
linters-settings:
2+
depguard:
3+
include-go-root: true
4+
packages:
5+
- compress/*
6+
packages-with-error-message:
7+
log: "don't use log"

test/testdata/depguard.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
//args: -Edepguard
2-
//config: linters-settings.depguard.include-go-root=true
3-
//config: linters-settings.depguard.packages=compress/*,log
2+
//config_path: testdata/configs/depguard.yml
43
package testdata
54

65
import (
76
"compress/gzip" // ERROR "`compress/gzip` is in the blacklist"
8-
"log" // ERROR "`log` is in the blacklist"
7+
"log" // ERROR "`log` is in the blacklist: don't use log"
98
)
109

1110
func SpewDebugInfo() {

0 commit comments

Comments
 (0)