GitHub - caarlos0/bandep: enforce banned dependency imports in Go code
source link: https://github.com/caarlos0/bandep
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
bandep
Bandep is a tool meant to be used in the CI and/or git commit hooks to prevent users from adding banned deps.
Use Cases
- you have multiple packages with the same name in GOPATH (maybe even the same methods) and your editor sometimes import the wrong import in some files (example)
- you have explicit rules in your codebase regarding which packages can import which packages
Usage
A basic usage would be to ban some deps in the current project, recursively:
bandep --pkg ./... --ban foo/bar,github.com/foo/bar
Or, for example, on DigitalOcean, they forbid the exp package to be imported from anywhere but the exp package itself:
go list -f '{{ .Dir }}' -e ./... | grep -v do/exp | while read -r pkg; do
bandep --pkg $pkg --ban do/exp/foo
done
Composing it with shell (and go list
), you can do basically anything you need.
You can, of course, add it to your git pre-commit hook. Check out this example.
Install
go get github.com/caarlos0/bandep
brew install caarlos0/tap/bandep
Or download one from the releases tab and install manually.
How it works
It is quite simple, really: we scan the dirs into an AST, and use that AST to check if a banned import is being used.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK