[#4] linters: add disable-packages option

Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
Alexander Chuprov 2023-08-07 17:36:41 +03:00
parent 7450953e5f
commit 1c1b1596a8
4 changed files with 60 additions and 33 deletions

View file

@ -13,7 +13,7 @@ jobs:
with: with:
go-version: '1.20' go-version: '1.20'
cache: true cache: true
- name: Run staticcheck - name: Build lib
run: make lib run: make lib
lint: lint:

View file

@ -4,37 +4,6 @@
`linters` is a project that enables the integration of custom linting rules into the [golangci-lint](https://github.com/golangci/golangci-lint) framework. `linters` is a project that enables the integration of custom linting rules into the [golangci-lint](https://github.com/golangci/golangci-lint) framework.
## Available linters
| Name | Description |
| ----------------------- | --------------------------------------------------------------------------- |
| [noliteral](#noliteral) | The tool prohibits the use of literal string arguments in logging functions |
## Linters Configuration
The settings for linters are available if golangci-lint >= 1.5.4 is used.
### noliteral
```yml
linters-settings:
custom:
noliteral:
path: .bin/external_linters.so
original-url: git.frostfs.info/TrueCloudLab/linters.git
settings:
target-methods : ["reportFlushError", "reportError"] #optional. Enabled by default "Debug", "Info", "Warn", "Error"
constants-package: "git.frostfs.info/rep/logs" #if not set, then the check is disabled
```
## Installation
```bash
git clone git.frostfs.info/TrueCloudLab/linters
cd linters
make lib OUT_DIR=<Path to the directory with libraries>
```
## Usage ## Usage
Add to .golangci.yml Add to .golangci.yml
@ -52,3 +21,51 @@ Add to .golangci.yml
enable: enable:
custom-linters custom-linters
``` ```
## Installation
```bash
git clone git.frostfs.info/TrueCloudLab/linters
cd linters
make lib OUT_DIR=<Path to the directory with libraries>
```
## Available linters
| Name | Description |
| ----------------------- | --------------------------------------------------------------------------- |
| [noliteral](#noliteral) | The tool prohibits the use of literal string arguments in logging functions |
## Linters Configuration
Settings via a configuration file are available if golangci-lint >= 1.5.4 is used
### noliteral
##### File Configuration
```yml
linters-settings:
custom:
noliteral:
path: .bin/external_linters.so
original-url: git.frostfs.info/TrueCloudLab/linters.git
settings:
target-methods: ["reportFlushError", "reportError"] # optional. Enabled by default "Debug", "Info", "Warn", "Error"
disable-packages: ["pkg1", "pkg2"] # List of packages for which the check should be disabled.
constants-package: "git.frostfs.info/rep/logs" # if not set, then the check is disabled
```
##### ENV Configuration
| Variable | Description |
| ----------------------------- | ------------------------------------------------------- |
| `NOLITERAL_TARGET_METHODS` | List of methods to analyze |
| `NOLITERAL_DISABLE_PACKAGES` | List of packages for which the check should be disabled |
| `NOLITERAL_CONSTANTS_PACKAGE` | Path to the package with constants |
**Note:** You may need to clear the golangci-lint cache when configuring through ENV. More details can be found [here](https://golangci-lint.run/usage/configuration/#cache).

View file

@ -23,6 +23,7 @@ var (
type Configuration struct { type Configuration struct {
TargetMethods []string `mapstructure:"target-methods"` TargetMethods []string `mapstructure:"target-methods"`
DisablePackages []string `mapstructure:"disable-packages"`
ConstantsPackage string `mapstructure:"constants-package"` ConstantsPackage string `mapstructure:"constants-package"`
} }
@ -49,6 +50,12 @@ func run(pass *analysis.Pass) (interface{}, error) {
return true return true
} }
for _, pkgName := range Config.DisablePackages {
if pkgName == getPackageName(expr.Args[0]) {
return true
}
}
pass.Report(analysis.Diagnostic{ pass.Report(analysis.Diagnostic{
Pos: expr.Pos(), Pos: expr.Pos(),
End: expr.End(), End: expr.End(),

View file

@ -22,11 +22,13 @@ func (*analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {
// for version ci-lint >= '1.5.4'. // for version ci-lint >= '1.5.4'.
func New(conf any) ([]*analysis.Analyzer, error) { func New(conf any) ([]*analysis.Analyzer, error) {
targetMethods := strings.Split(os.Getenv("NOLITERAL_TARGET_METHODS"), ",") targetMethods := strings.Split(os.Getenv("NOLITERAL_TARGET_METHODS"), ",")
disablePackages := strings.Split(os.Getenv("NOLITERAL_DISABLE_PACKAGES"), ",")
constantsPackage := os.Getenv("NOLITERAL_CONSTANTS_PACKAGE") constantsPackage := os.Getenv("NOLITERAL_CONSTANTS_PACKAGE")
configMap := map[string]any{ configMap := map[string]any{
"target-methods": targetMethods, "target-methods": targetMethods,
"constants-package": constantsPackage, "constants-package": constantsPackage,
"disable-packages": disablePackages,
} }
if confMap, ok := conf.(map[string]any); ok { if confMap, ok := conf.(map[string]any); ok {
@ -43,6 +45,7 @@ func New(conf any) ([]*analysis.Analyzer, error) {
noliteral.Config.TargetMethods = append(noliteral.Config.TargetMethods, config.TargetMethods...) noliteral.Config.TargetMethods = append(noliteral.Config.TargetMethods, config.TargetMethods...)
noliteral.Config.ConstantsPackage = config.ConstantsPackage noliteral.Config.ConstantsPackage = config.ConstantsPackage
noliteral.Config.DisablePackages = config.DisablePackages
return []*analysis.Analyzer{noliteral.LogsAnalyzer}, nil return []*analysis.Analyzer{noliteral.LogsAnalyzer}, nil
} }