https://github.com/sublime-security/go-import-name-validator
Enforces user defined semantics around import naming
https://github.com/sublime-security/go-import-name-validator
Last synced: 5 months ago
JSON representation
Enforces user defined semantics around import naming
- Host: GitHub
- URL: https://github.com/sublime-security/go-import-name-validator
- Owner: sublime-security
- License: mit
- Created: 2022-08-10T02:59:37.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-03-13T13:26:16.000Z (about 1 year ago)
- Last Synced: 2025-12-26T21:12:06.555Z (5 months ago)
- Language: Go
- Size: 16.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-import-name-validator
Enforces user defined semantics around import naming.
## Usage
```
# Install from local repo
go install $GOPATH/src/github.com/sublime-security/go-import-name-validator/cmd/inspect-imports
# Install from GitHub
go install github.com/sublime-security/go-import-name-validator/cmd/inspect-imports@main
```
`inspect-imports` should be available in `$GOPATH/bin`, which is assumed to be on your `PATH`.
Your working director must be the repo you're inspecting. Example to inspect a package within this repo (w/o any requirements specified):
```
cd $GOPATH/src/github.com/sublime-security/go-import-name-validator
inspect-imports $GOPATH/src/github.com/sublime-security/go-import-name-validator/imports_analyzer
```
Similarly to check all packages in the repo
```
cd $GOPATH/src/github.com/sublime-security/go-import-name-validator
go list ./... | xargs inspect-imports
```
All standard Go singlechecker flags are supported. Such as `-fix` to fix inline, where possible.
```
inspect-imports -fix $GOPATH/src/github.com/sublime-security/go-import-name-validator/imports_analyzer
```
### Required Package Names
The `-require-name` flag may be specified any number of times to require that:
* The given name is only used for the given path
* Whenever the given path is imported, the name is used.
`-require-name =`
Examples:
* `-require-name "github.com/sirupsen/logrus"=log`
* The logrus package must be imported as `log`
* If the name `log` is used, it must be for `"github.com/sirupsen/logrus"`
* `-require-name "errors"=`
* If the errors package is imported, it must be unnamed (it cannot be named `errors`)
### Forbidden Imports
The `-forbidden` flag prohibits an import path from being included, any may be specified any number of times.
Examples:
* `-forbidden github.com/RichardKnop/machinery/v2/log`
* `github.com/RichardKnop/machinery/v2/log` may not be imported
* No partial matching is supported, although this could be a good addition.
### Full Examples
```
cd $GOPATH/src/github.com/sublime-security/go-import-name-validator
go list ./... | xargs inspect-imports -fix -forbidden github.com/RichardKnop/machinery/v2/log -require-name "github.com/sirupsen/logrus"=log -require-name "errors"=
# Same but single package
inspect-imports -fix -forbidden github.com/RichardKnop/machinery/v2/log -require-name "github.com/sirupsen/logrus"=log -require-name "errors"= $GOPATH/src/github.com/sublime-security/go-import-name-validator/imports_analyzer
```