From 7ceda9eddfdf34ec2b07f61685364ca58f8364be Mon Sep 17 00:00:00 2001
From: Alexander Chuprov <a.chuprov@yadro.com>
Date: Mon, 7 Aug 2023 17:36:41 +0300
Subject: [PATCH] [#4] linters: add disable-packages option

Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
---
 .forgejo/workflows/tests.yml           |  2 +-
 README.md                              | 81 ++++++++++++++++----------
 internal/analyzers/noliteral/linter.go |  7 +++
 main.go                                |  3 +
 4 files changed, 60 insertions(+), 33 deletions(-)

diff --git a/.forgejo/workflows/tests.yml b/.forgejo/workflows/tests.yml
index 107d960..7883a2f 100644
--- a/.forgejo/workflows/tests.yml
+++ b/.forgejo/workflows/tests.yml
@@ -13,7 +13,7 @@ jobs:
         with:
           go-version: '1.20'
           cache: true
-      - name: Run staticcheck
+      - name: Build lib
         run: make lib
 
   lint:
diff --git a/README.md b/README.md
index e75a2c9..ed1c8e3 100644
--- a/README.md
+++ b/README.md
@@ -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. 
 
-## 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
 
 Add to .golangci.yml 
@@ -51,4 +20,52 @@ Add to .golangci.yml
     linters:
         enable:
             custom-linters
-```
\ No newline at end of file
+```
+
+
+## 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).
+
diff --git a/internal/analyzers/noliteral/linter.go b/internal/analyzers/noliteral/linter.go
index ab2012c..1e88b89 100644
--- a/internal/analyzers/noliteral/linter.go
+++ b/internal/analyzers/noliteral/linter.go
@@ -23,6 +23,7 @@ var (
 
 type Configuration struct {
 	TargetMethods    []string `mapstructure:"target-methods"`
+	DisablePackages  []string `mapstructure:"disable-packages"`
 	ConstantsPackage string   `mapstructure:"constants-package"`
 }
 
@@ -49,6 +50,12 @@ func run(pass *analysis.Pass) (interface{}, error) {
 					return true
 				}
 
+				for _, pkgName := range Config.DisablePackages {
+					if pkgName == getPackageName(expr.Args[0]) {
+						return true
+					}
+				}
+
 				pass.Report(analysis.Diagnostic{
 					Pos:            expr.Pos(),
 					End:            expr.End(),
diff --git a/main.go b/main.go
index d549a58..633c6c3 100644
--- a/main.go
+++ b/main.go
@@ -22,11 +22,13 @@ func (*analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {
 // for version ci-lint >= '1.5.4'.
 func New(conf any) ([]*analysis.Analyzer, error) {
 	targetMethods := strings.Split(os.Getenv("NOLITERAL_TARGET_METHODS"), ",")
+	disablePackages := strings.Split(os.Getenv("NOLITERAL_DISABLE_PACKAGES"), ",")
 	constantsPackage := os.Getenv("NOLITERAL_CONSTANTS_PACKAGE")
 
 	configMap := map[string]any{
 		"target-methods":    targetMethods,
 		"constants-package": constantsPackage,
+		"disable-packages":  disablePackages,
 	}
 
 	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.ConstantsPackage = config.ConstantsPackage
+	noliteral.Config.DisablePackages = config.DisablePackages
 
 	return []*analysis.Analyzer{noliteral.LogsAnalyzer}, nil
 }