Skip to content

Commit 8c01872

Browse files
committed
feat(config): rename domains to prefixes and deprecate domains
- Introduce 'prefixes' to replace 'domains' configuration key - Maintain backwards compatibility by parsing 'domains' but log a deprecation warning - Update IsAllowedModuleDomain check to IsAllowedModulePrefix and merge arrays for backward compatibility - Update README, examples, and tests to use 'prefixes'
1 parent c22ade2 commit 8c01872

10 files changed

Lines changed: 40 additions & 32 deletions

File tree

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Allow and block list linter for direct Go module dependencies. This is useful fo
1414

1515
Allowed and blocked modules are defined in a `./.gomodguard.yaml` or `~/.gomodguard.yaml` file.
1616

17-
Modules can be allowed by module or domain name. When allowed modules are specified any modules not in the allowed configuration are blocked.
17+
Modules can be allowed by module or prefix name. When allowed modules are specified any modules not in the allowed configuration are blocked.
1818

19-
If no allowed modules or domains are specified then all modules are allowed except for blocked ones.
19+
If no allowed modules or module prefixes are specified then all modules are allowed except for blocked ones.
2020

2121
The linter looks for blocked modules in `go.mod` and searches for imported packages where the imported packages module is blocked. Indirect modules are not considered.
2222

@@ -41,8 +41,11 @@ allowed:
4141
- github.com/go-xmlfmt/xmlfmt
4242
- github.com/phayes/checkstyle
4343
- github.com/mitchellh/go-homedir
44-
domains: # List of allowed module domains
45-
- golang.org
44+
- github.com/confluentinc/confluent-kafka-go/v2 # Allow v2 only
45+
prefixes: # List of allowed module prefixes (Replaced domains which is now deprecated)
46+
- golang.org # Allow all golang.org modules
47+
- github.com/kubernetes # Allow all Kubernetes modules
48+
- github.com/apache/arrow-go # Allow all Apache Arrow module major versions
4649

4750
blocked:
4851
modules: # List of blocked modules
@@ -89,7 +92,7 @@ Flags:
8992
╰─ ./gomodguard -r checkstyle -f gomodguard-checkstyle.xml ./...
9093
9194
info: allowed modules, [gopkg.in/yaml.v3 github.com/go-xmlfmt/xmlfmt github.com/phayes/checkstyle github.com/mitchellh/go-homedir]
92-
info: allowed module domains, [golang.org]
95+
info: allowed module prefixes, [golang.org]
9396
info: blocked modules, [github.com/uudashr/go-module]
9497
info: found `2` blocked modules in the go.mod file, [github.com/gofrs/uuid github.com/uudashr/go-module]
9598
blocked_example.go:6: import of package `github.com/gofrs/uuid` is blocked because the module is not in the allowed modules list.

_example/allOptions/.gomodguard.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ allowed:
44
- github.com/go-xmlfmt/xmlfmt
55
- github.com/Masterminds/semver
66
- github.com/ryancurrah/gomodguard
7-
domains: # List of allowed module domains
7+
prefixes: # List of allowed module prefixes
88
- golang.org
99

1010
blocked:

_example/indirectDep/.gomodguard.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ allowed:
44
- github.com/go-xmlfmt/xmlfmt
55
- github.com/Masterminds/semver
66
- github.com/ryancurrah/gomodguard
7-
domains: # List of allowed module domains
7+
prefixes: # List of allowed module prefixes
88
- golang.org
99

1010
blocked:

_example/invalidConstraint/.gomodguard.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
allowed:
22
modules:
33
- github.com/pborman/uuid
4-
domains:
4+
prefixes:
55
- golang.org
66
blocked:
77
modules:
@@ -24,5 +24,5 @@ blocked:
2424
version: "!= 1.3.0"
2525
- github.com/gin-gonic/gin:
2626
version: "== 1.9.1"
27-
domains: []
27+
prefixes: []
2828

allowed.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package gomodguard
33
import "strings"
44

55
// Allowed is a list of modules and module
6-
// domains that are allowed to be used.
6+
// prefixes that are allowed to be used.
77
type Allowed struct {
8-
Modules []string `yaml:"modules"`
9-
Domains []string `yaml:"domains"`
8+
Modules []string `yaml:"modules"`
9+
Domains []string `yaml:"domains"`
10+
Prefixes []string `yaml:"prefixes"`
1011
}
1112

1213
// IsAllowedModule returns true if the given module
@@ -23,14 +24,14 @@ func (a *Allowed) IsAllowedModule(moduleName string) bool {
2324
return false
2425
}
2526

26-
// IsAllowedModuleDomain returns true if the given modules domain is
27-
// in the allowed module domains list.
28-
func (a *Allowed) IsAllowedModuleDomain(moduleName string) bool {
29-
allowedDomains := a.Domains
27+
// IsAllowedModulePrefix returns true if the given modules prefix is
28+
// in the allowed module prefixes list.
29+
func (a *Allowed) IsAllowedModulePrefix(moduleName string) bool {
30+
allowedPrefixes := append(a.Prefixes, a.Domains...)
3031

31-
for i := range allowedDomains {
32+
for i := range allowedPrefixes {
3233
if strings.HasPrefix(strings.TrimSpace(strings.ToLower(moduleName)),
33-
strings.TrimSpace(strings.ToLower(allowedDomains[i]))) {
34+
strings.TrimSpace(strings.ToLower(allowedPrefixes[i]))) {
3435
return true
3536
}
3637
}

allowed_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ func TestAllowedIsAllowedModule(t *testing.T) {
3838
}
3939
}
4040

41-
func TestAllowedIsAllowedModuleDomain(t *testing.T) {
41+
func TestAllowedIsAllowedModulePrefix(t *testing.T) {
4242
var tests = []struct {
4343
testName string
4444
allowedModules gomodguard.Allowed
4545
lintedModuleName string
46-
wantIsAllowedModuleDomain bool
46+
wantIsAllowedModulePrefix bool
4747
}{
4848
{
4949
"module is allowed",
50-
gomodguard.Allowed{Domains: []string{"github.com"}},
50+
gomodguard.Allowed{Prefixes: []string{"github.com"}},
5151
"github.com/someallowed/module",
5252
true,
5353
},
@@ -61,9 +61,9 @@ func TestAllowedIsAllowedModuleDomain(t *testing.T) {
6161

6262
for _, tt := range tests {
6363
t.Run(tt.testName, func(t *testing.T) {
64-
isAllowedModuleDomain := tt.allowedModules.IsAllowedModuleDomain(tt.lintedModuleName)
65-
if !reflect.DeepEqual(isAllowedModuleDomain, tt.wantIsAllowedModuleDomain) {
66-
t.Errorf("got '%v' want '%v'", isAllowedModuleDomain, tt.wantIsAllowedModuleDomain)
64+
isAllowedModulePrefix := tt.allowedModules.IsAllowedModulePrefix(tt.lintedModuleName)
65+
if !reflect.DeepEqual(isAllowedModulePrefix, tt.wantIsAllowedModulePrefix) {
66+
t.Errorf("got '%v' want '%v'", isAllowedModulePrefix, tt.wantIsAllowedModulePrefix)
6767
}
6868
})
6969
}

internal/cli/cli.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ func Run() int {
9494
}
9595

9696
logger.Printf("info: allowed modules, %+v", config.Allowed.Modules)
97-
logger.Printf("info: allowed module domains, %+v", config.Allowed.Domains)
97+
logger.Printf("info: allowed module prefixes, %+v", config.Allowed.Prefixes)
98+
if len(config.Allowed.Domains) > 0 {
99+
logger.Printf("info: allowed module domains, %+v", config.Allowed.Domains)
100+
logger.Printf("warning: 'domains' is deprecated, please use 'prefixes' instead")
101+
}
98102
logger.Printf("info: blocked modules, %+v", config.Blocked.Modules.Get())
99103
logger.Printf("info: blocked modules with version constraints, %+v", config.Blocked.Versions.Get())
100104

processor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (p *Processor) ProcessFiles(filenames []string) (issues []Issue) {
9494
// the go.mod file of the module that is being linted.
9595
//
9696
// It works by iterating over the dependant modules specified in the require
97-
// directive, checking if the module domain or full name is in the allowed list.
97+
// directive, checking if the module prefix or full name is in the allowed list.
9898
func (p *Processor) SetBlockedModules() { //nolint:funlen
9999
blockedModules := make(map[string][]string, len(p.Modfile.Require))
100100
currentModuleName := p.Modfile.Module.Mod.Path
@@ -108,9 +108,9 @@ func (p *Processor) SetBlockedModules() { //nolint:funlen
108108
var isAllowed bool
109109

110110
switch {
111-
case len(p.Config.Allowed.Modules) == 0 && len(p.Config.Allowed.Domains) == 0:
111+
case len(p.Config.Allowed.Modules) == 0 && len(p.Config.Allowed.Prefixes) == 0 && len(p.Config.Allowed.Domains) == 0:
112112
isAllowed = true
113-
case p.Config.Allowed.IsAllowedModuleDomain(lintedModuleName):
113+
case p.Config.Allowed.IsAllowedModulePrefix(lintedModuleName):
114114
isAllowed = true
115115
case p.Config.Allowed.IsAllowedModule(lintedModuleName):
116116
isAllowed = true

processor_internal_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func TestProcessorSetBlockedModulesWithAllowList(t *testing.T) {
182182
"github.com/Masterminds/semver/v3",
183183
"github.com/ryancurrah/gomodguard",
184184
},
185-
Domains: []string{
185+
Prefixes: []string{
186186
"golang.org",
187187
},
188188
},
@@ -403,7 +403,7 @@ func TestProcessorSetBlockedModulesWithIndirectDependency(t *testing.T) {
403403
"github.com/Masterminds/semver/v3",
404404
"github.com/ryancurrah/gomodguard",
405405
},
406-
Domains: []string{
406+
Prefixes: []string{
407407
"golang.org",
408408
},
409409
},
@@ -452,7 +452,7 @@ func TestProcessorSetBlockedModulesWithIndirectDependency(t *testing.T) {
452452
"github.com/Masterminds/semver/v3",
453453
"github.com/ryancurrah/gomodguard",
454454
},
455-
Domains: []string{
455+
Prefixes: []string{
456456
"golang.org",
457457
},
458458
},

processor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestProcessorProcessFiles(t *testing.T) { //nolint:funlen
3939
"github.com/Masterminds/semver/v3",
4040
"github.com/ryancurrah/gomodguard",
4141
},
42-
Domains: []string{
42+
Prefixes: []string{
4343
"golang.org",
4444
},
4545
},

0 commit comments

Comments
 (0)