banimport
This tool detects prohibited imports. You can control "import dependencies" among go packages, for example, in your monorepo.
Install
go install github.com/nrnrk/banimport@latest
How to use
- Install command
go install github.com/nrnrk/banimport@latest
- Prepare config file
- Edit a config file to prohibit "import dependencies"
cd <your repository root>
vi .banimport.json
example: .banimport.json
{
"pattern": {
"github.com/example/monorepo/svca": [
"github.com/example/monorepo/svcb",
"github.com/example/monorepo/svcc",
],
"github.com/example/monorepo/svcb": [
"github.com/example/monorepo/svcc",
"github.com/example/monorepo/svca",
],
"github.com/example/monorepo/svcc": [
"github.com/example/monorepo/svca",
"github.com/example/monorepo/svcb",
]
}
}
- Execute by
go vet
go vet vettool=$(which banimport) -banimport.config=$(cat .banimport.json) ./...
Usecase
├── libA
├── libB
├── svcA
├── svcB
└── svcC
libA
andlibB
are intended to be used by other packages.
svcA
, svcB
, and svcC
sholud not be used by other packaes even in the module.
In the above case, you can prohibit "import dependencies" on svcA
, svcB
, and svcC
to use the following config file.
.banimport.json
{
"pattern": {
"github.com/nrnrk/monorepo/libA": [
"github.com/nrnrk/monorepo/svcA",
"github.com/nrnrk/monorepo/svcB",
"github.com/nrnrk/monorepo/svcC"
],
"github.com/nrnrk/monorepo/libB": [
"github.com/nrnrk/monorepo/svcA",
"github.com/nrnrk/monorepo/svcB",
"github.com/nrnrk/monorepo/svcC"
],
"github.com/nrnrk/monorepo/svcA": [
"github.com/nrnrk/monorepo/svcB",
"github.com/nrnrk/monorepo/svcC"
],
"github.com/nrnrk/monorepo/svcB": [
"github.com/nrnrk/monorepo/svcC",
"github.com/nrnrk/monorepo/svcA"
],
"github.com/nrnrk/monorepo/svcC": [
"github.com/nrnrk/monorepo/svcA",
"github.com/nrnrk/monorepo/svcB"
]
}
}
Hints
- If your service packages include some packages used by other external packages, you might want to use
internal
package. This checker is useful when your service packages do not have any packages supposed to be used by external packages.
- You can check the details about
internal
package here.
This repository is originally created using skeleton.