You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: implement v2.0 with layered matching strategy and configuration schema
- Introduce a map-based configuration schema for Allowed and Blocked modules.
- Implement a layered evaluation strategy (Exact > Prefix > Regex) for deterministic results.
- Add support for regex matching and longest-prefix matching.
- Refactor internal core to use a new Matcher interface and semver/v3 for version constraints.
- Restructure project to move binary source to cmd/gomodguard.
- Update CI/CD with a GitHub Action for automated releases using GoReleaser.
- Add a migration guide and updated documentation to the README.
- Modernize Makefile with improved linting, testing, and tagging targets.
BREAKING CHANGE: The configuration schema has changed from lists to maps, and the matching logic has been overhauled. Existing v1 configurations require migration using the `gomodguard migrate` command.
Copy file name to clipboardExpand all lines: README.md
+63-35Lines changed: 63 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,37 +26,66 @@ If the linted module imports a blocked module but the linted module is in the re
26
26
27
27
Version constraints can be specified for modules as well which lets you block new or old versions of modules or specific versions.
28
28
29
+
When multiple rules can match the same module (e.g., overlapping exact, prefix, and regex rules), they are evaluated using a layered strategy for deterministic results:
30
+
31
+
1.**Exact match** — highest priority; wins over prefix and regex.
32
+
2.**Prefix match** — next priority; longest matching prefix wins.
33
+
3.**Regex match** — lowest priority; evaluated in alphabetical key order; first match wins.
34
+
29
35
Results are printed to `stdout`.
30
36
31
37
Logging statements are printed to `stderr`.
32
38
33
39
Results can be exported to different report formats. Which can be imported into CI tools. See the help section for more information.
34
40
35
-
##Configuration
41
+
# Configuration
36
42
37
43
```yaml
38
44
allowed:
39
-
modules: # List of allowed modules
40
-
- gopkg.in/yaml.v3
41
-
- github.com/go-xmlfmt/xmlfmt
42
-
- github.com/phayes/checkstyle
43
-
- github.com/mitchellh/go-homedir
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
45
+
go.yaml.in/yaml/v4:
46
+
github.com/go-xmlfmt/xmlfmt:
47
+
github.com/confluentinc/confluent-kafka-go/v2:
48
+
version: "== 2.5.0"
49
+
github.com/kubernetes:
50
+
match_type: prefix
51
+
github.com/apache/arrow-go:
52
+
match_type: prefix
53
+
"github.com/somecompany/.*":
54
+
match_type: regex
49
55
50
56
blocked:
51
-
modules: # List of blocked modules
52
-
- github.com/uudashr/go-module: # Blocked module
53
-
recommendations: # Recommended modules that should be used instead (Optional)
54
-
- golang.org/x/mod
55
-
reason: "`mod` is the official go.mod parser library."# Reason why the recommended module should be used (Optional)
56
-
versions: # List of blocked module version constraints.
57
-
- github.com/mitchellh/go-homedir: # Blocked module with version constraint.
58
-
version: "<= 1.1.0"# Version constraint, see https://github.com/Masterminds/semver#basic-comparisons.
59
-
reason: "testing if blocked version constraint works."# Reason why the version constraint exists.
57
+
github.com/uudashr/go-module:
58
+
match_type: exact # or regex, prefix
59
+
recommendations:
60
+
- golang.org/x/mod
61
+
reason: "`mod` is the official go.mod parser library."
62
+
github.com/mitchellh/go-homedir:
63
+
version: "<= 1.1.0"
64
+
reason: "testing if blocked version constraint works."
65
+
"github.com/badcompany/.*":
66
+
match_type: regex
67
+
reason: "No badcompany packages are permitted."
68
+
```
69
+
70
+
## Example .gomodguard.yaml Files
71
+
72
+
The following example configuration files are available:
info: found `2` blocked modules in the go.mod file, [github.com/gofrs/uuid github.com/uudashr/go-module]
98
-
blocked_example.go:6: import of package `github.com/gofrs/uuid` is blocked because the module is not in the allowed modules list.
99
-
blocked_example.go:7: import of package `github.com/uudashr/go-module` is blocked because the module is in the blocked modules list. `golang.org/x/mod` is a recommended module. `mod` is the official go.mod parser library.
blocked_example.go:6:1 import of package `github.com/gofrs/uuid` is blocked because the module is in the blocked modules list. `github.com/ryancurrah/gomodguard` is a recommended module. testing if module is not blocked when it is recommended.
127
+
blocked_example.go:7:1 import of package `github.com/mitchellh/go-homedir` is blocked because the module is in the blocked modules list. version `v1.1.0` is blocked because it does not meet the version constraint `<=1.1.0`. testing if blocked version constraint works.
128
+
blocked_example.go:8:1 import of package `github.com/uudashr/go-module` is blocked because the module is in the blocked modules list. `golang.org/x/mod` is a recommended module. `mod` is the official go.mod parser library.
100
129
```
101
130
102
131
Resulting checkstyle file
@@ -107,26 +136,25 @@ Resulting checkstyle file
107
136
<?xml version="1.0" encoding="UTF-8"?>
108
137
<checkstyleversion="1.0.0">
109
138
<filename="blocked_example.go">
110
-
<error line="6" column="1" severity="error" message="import of package `github.com/gofrs/uuid` is blocked because the module is not in the allowed modules list." source="gomodguard">
111
-
</error>
112
-
<error line="7" column="1" severity="error" message="import of package `github.com/uudashr/go-module` is blocked because the module is in the blocked modules list. `golang.org/x/mod` is a recommended module. `mod` is the official go.mod parser library." source="gomodguard">
113
-
</error>
139
+
<error line="6" column="1" severity="error" message="import of package `github.com/gofrs/uuid` is blocked because the module is in the blocked modules list. `github.com/ryancurrah/gomodguard` is a recommended module. testing if module is not blocked when it is recommended." source="gomodguard"></error>
140
+
<error line="7" column="1" severity="error" message="import of package `github.com/mitchellh/go-homedir` is blocked because the module is in the blocked modules list. version `v1.1.0` is blocked because it does not meet the version constraint `<=1.1.0`. testing if blocked version constraint works." source="gomodguard"></error>
141
+
<error line="8" column="1" severity="error" message="import of package `github.com/uudashr/go-module` is blocked because the module is in the blocked modules list. `golang.org/x/mod` is a recommended module. `mod` is the official go.mod parser library." source="gomodguard"></error>
114
142
</file>
115
143
</checkstyle>
116
144
```
117
145
118
146
## Install
119
147
120
148
```
121
-
go install github.com/ryancurrah/gomodguard/cmd/gomodguard
149
+
go install github.com/ryancurrah/gomodguard/v2/cmd/gomodguard@latest
122
150
```
123
151
124
152
## Develop
125
153
126
154
```
127
-
git clone https://github.com/ryancurrah/gomodguard.git && cd gomodguard
155
+
git clone https://github.com/ryancurrah/gomodguard.git && cd gomodguard/cmd/gomodguard
0 commit comments