From cea7a7e11ea81d41732fae0e82b693de9c691e13 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?=
 <charled.breteche@gmail.com>
Date: Mon, 9 May 2022 18:55:35 +0200
Subject: [PATCH] fix: golangci-lint warnings in cmd (#3843)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>

Co-authored-by: Vyankatesh Kudtarkar <vyankateshkd@gmail.com>
---
 cmd/cli/kubectl-kyverno/apply/report.go        |  2 +-
 cmd/cli/kubectl-kyverno/test/test_command.go   |  2 +-
 cmd/cli/kubectl-kyverno/utils/common/common.go | 10 ++++------
 cmd/cli/kubectl-kyverno/utils/common/fetch.go  | 14 ++++++--------
 cmd/initContainer/main.go                      |  1 -
 5 files changed, 12 insertions(+), 17 deletions(-)

diff --git a/cmd/cli/kubectl-kyverno/apply/report.go b/cmd/cli/kubectl-kyverno/apply/report.go
index 863d08eb46..732fec7600 100644
--- a/cmd/cli/kubectl-kyverno/apply/report.go
+++ b/cmd/cli/kubectl-kyverno/apply/report.go
@@ -83,7 +83,7 @@ func buildPolicyResults(infos []policyreport.Info) map[string][]report.PolicyRep
 		if ns != "" {
 			appname = fmt.Sprintf("policyreport-ns-%s", ns)
 		} else {
-			appname = fmt.Sprintf(clusterpolicyreport)
+			appname = clusterpolicyreport
 		}
 
 		for _, infoResult := range info.Results {
diff --git a/cmd/cli/kubectl-kyverno/test/test_command.go b/cmd/cli/kubectl-kyverno/test/test_command.go
index 4b1a56754e..1843f424f3 100644
--- a/cmd/cli/kubectl-kyverno/test/test_command.go
+++ b/cmd/cli/kubectl-kyverno/test/test_command.go
@@ -303,7 +303,7 @@ func testCommandExecute(dirPath []string, fileName string, gitBranch string, tes
 	}
 
 	if len(dirPath) == 0 {
-		return rc, sanitizederror.NewWithError(fmt.Sprintf("a directory is required"), err)
+		return rc, sanitizederror.NewWithError("a directory is required", err)
 	}
 
 	if len(testCase) != 0 {
diff --git a/cmd/cli/kubectl-kyverno/utils/common/common.go b/cmd/cli/kubectl-kyverno/utils/common/common.go
index 0ef5269fe1..89f5d24de8 100644
--- a/cmd/cli/kubectl-kyverno/utils/common/common.go
+++ b/cmd/cli/kubectl-kyverno/utils/common/common.go
@@ -588,7 +588,6 @@ func PrintMutatedOutput(mutateLogPath string, mutateLogPathIsDir bool, yaml stri
 
 // GetPoliciesFromPaths - get policies according to the resource path
 func GetPoliciesFromPaths(fs billy.Filesystem, dirPath []string, isGit bool, policyResourcePath string) (policies []v1.PolicyInterface, err error) {
-	var errors []error
 	if isGit {
 		for _, pp := range dirPath {
 			filep, err := fs.Open(filepath.Join(policyResourcePath, pp))
@@ -608,8 +607,7 @@ func GetPoliciesFromPaths(fs billy.Filesystem, dirPath []string, isGit bool, pol
 			}
 			policiesFromFile, errFromFile := utils.GetPolicy(policyBytes)
 			if errFromFile != nil {
-				err := fmt.Errorf("failed to process : %v", errFromFile.Error())
-				errors = append(errors, err)
+				fmt.Printf("failed to process : %v", errFromFile.Error())
 				continue
 			}
 			policies = append(policies, policiesFromFile...)
@@ -987,9 +985,9 @@ func GetPatchedResourceFromPath(fs billy.Filesystem, path string, isGit bool, po
 
 	if isGit {
 		if len(path) > 0 {
-			filep, err := fs.Open(filepath.Join(policyResourcePath, path))
-			if err != nil {
-				fmt.Printf("Unable to open patchedResource file: %s. \nerror: %s", path, err)
+			filep, fileErr := fs.Open(filepath.Join(policyResourcePath, path))
+			if fileErr != nil {
+				fmt.Printf("Unable to open patchedResource file: %s. \nerror: %s", path, fileErr)
 			}
 			patchedResourceBytes, err = ioutil.ReadAll(filep)
 		}
diff --git a/cmd/cli/kubectl-kyverno/utils/common/fetch.go b/cmd/cli/kubectl-kyverno/utils/common/fetch.go
index 6bee74e32e..3848ee9b21 100644
--- a/cmd/cli/kubectl-kyverno/utils/common/fetch.go
+++ b/cmd/cli/kubectl-kyverno/utils/common/fetch.go
@@ -119,7 +119,6 @@ func whenClusterIsFalse(resourcePaths []string, policyReport bool) ([]*unstructu
 func GetResourcesWithTest(fs billy.Filesystem, policies []v1.PolicyInterface, resourcePaths []string, isGit bool, policyResourcePath string) ([]*unstructured.Unstructured, error) {
 	resources := make([]*unstructured.Unstructured, 0)
 	var resourceTypesMap = make(map[string]bool)
-	var resourceTypes []string
 	for _, policy := range policies {
 		for _, rule := range autogen.ComputeRules(policy) {
 			for _, kind := range rule.MatchResources.Kinds {
@@ -127,9 +126,6 @@ func GetResourcesWithTest(fs billy.Filesystem, policies []v1.PolicyInterface, re
 			}
 		}
 	}
-	for kind := range resourceTypesMap {
-		resourceTypes = append(resourceTypes, kind)
-	}
 	if len(resourcePaths) > 0 {
 		for _, resourcePath := range resourcePaths {
 			var resourceBytes []byte
@@ -140,7 +136,7 @@ func GetResourcesWithTest(fs billy.Filesystem, policies []v1.PolicyInterface, re
 					fmt.Printf("Unable to open resource file: %s. error: %s", resourcePath, err)
 					continue
 				}
-				resourceBytes, err = ioutil.ReadAll(filep)
+				resourceBytes, _ = ioutil.ReadAll(filep)
 			} else {
 				resourceBytes, err = getFileBytes(resourcePath)
 			}
@@ -279,10 +275,12 @@ func convertResourceToUnstructured(resourceYaml []byte) (*unstructured.Unstructu
 }
 
 // GetPatchedResource converts raw bytes to unstructured object
-func GetPatchedResource(patchResourceBytes []byte) (patchedResource unstructured.Unstructured, err error) {
+func GetPatchedResource(patchResourceBytes []byte) (unstructured.Unstructured, error) {
 	getPatchedResource, err := GetResource(patchResourceBytes)
-	patchedResource = *getPatchedResource[0]
-
+	if err != nil {
+		return unstructured.Unstructured{}, err
+	}
+	patchedResource := *getPatchedResource[0]
 	return patchedResource, nil
 }
 
diff --git a/cmd/initContainer/main.go b/cmd/initContainer/main.go
index 98e5bf5bb8..fe2098abde 100644
--- a/cmd/initContainer/main.go
+++ b/cmd/initContainer/main.go
@@ -53,7 +53,6 @@ const (
 	clusterPolicyReportKind        string = "ClusterPolicyReport"
 	reportChangeRequestKind        string = "ReportChangeRequest"
 	clusterReportChangeRequestKind string = "ClusterReportChangeRequest"
-	clusterPolicyViolation         string = "ClusterPolicyViolation"
 	convertGenerateRequest         string = "ConvertGenerateRequest"
 )