From 96380d79f4fb9dd472ac88ae5cff0f8b333e307c Mon Sep 17 00:00:00 2001
From: Fabian Seidl <fabian.b.seidl@stud.h-da.de>
Date: Mon, 1 Aug 2022 16:12:08 +0200
Subject: [PATCH] enabled error linting and fixed problems with wron error
 checks

---
 .golangci.yml                                | 4 +++-
 cli/cmd/deviceSubscribe.go                   | 4 +++-
 cli/cmd/root.go                              | 3 ++-
 controller/api/apiIntegration_test.go        | 5 ++---
 controller/api/device.go                     | 3 ++-
 controller/northbound/server/sbi.go          | 3 ++-
 controller/nucleus/principalNetworkDomain.go | 3 ++-
 csbi/grpc.go                                 | 3 ++-
 csbi/write.go                                | 2 +-
 9 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/.golangci.yml b/.golangci.yml
index df0060e12..66c0d5182 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -33,7 +33,7 @@ issues:
 linters:
     # enable the specific needed linters
     # see here for full list: https://golangci-lint.run/usage/linters/
-    # linters to consider: gosimple, containedctx, contextcheck, depguard
+    # linters to consider: gosimple, containedctx, contextcheck, depguard, errname
     disable-all: true
     enable:
         - gofmt
@@ -52,6 +52,8 @@ linters:
         - asciicheck
         - bidichk
         - durationcheck
+        - errchkjson
+        - errorlint
 
 # custom settings for linters
 linters-settings:
diff --git a/cli/cmd/deviceSubscribe.go b/cli/cmd/deviceSubscribe.go
index e3718790b..691fed159 100644
--- a/cli/cmd/deviceSubscribe.go
+++ b/cli/cmd/deviceSubscribe.go
@@ -32,6 +32,7 @@ POSSIBILITY OF SUCH DAMAGE.
 package cmd
 
 import (
+	"errors"
 	"io"
 
 	"code.fbi.h-da.de/danet/gosdn/api/go/gosdn/pnd"
@@ -79,7 +80,8 @@ The device UUID and requested paths must be specified as a positional arguments.
 			subscribeResponse, err := subClient.Recv()
 			if err != nil {
 				if err != nil {
-					if err == io.EOF {
+
+					if errors.Is(err, io.EOF) {
 						break
 					}
 					log.Error(err)
diff --git a/cli/cmd/root.go b/cli/cmd/root.go
index f64c57032..40ad5467d 100644
--- a/cli/cmd/root.go
+++ b/cli/cmd/root.go
@@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE.
 package cmd
 
 import (
+	"errors"
 	"fmt"
 	"os"
 
@@ -108,7 +109,7 @@ func initConfig() {
 
 	// If a config file is found, read it in.
 	if err := viper.ReadInConfig(); err != nil {
-		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
+		if ok := errors.As(err, &viper.ConfigFileNotFoundError{}); ok {
 			// create folder if it does not exist
 			if err := os.MkdirAll(defaultPath, 0777); err != nil {
 				log.Error("Config directory not found and was unable to create, error: ", err)
diff --git a/controller/api/apiIntegration_test.go b/controller/api/apiIntegration_test.go
index a97a54820..39829243f 100644
--- a/controller/api/apiIntegration_test.go
+++ b/controller/api/apiIntegration_test.go
@@ -2,6 +2,7 @@ package api
 
 import (
 	"context"
+	"errors"
 	"testing"
 
 	"code.fbi.h-da.de/danet/gosdn/api/go/gosdn/pnd"
@@ -31,9 +32,7 @@ func TestApiIntegration(t *testing.T) {
 		t.Run(tt.name, func(t *testing.T) {
 			defer viper.Reset()
 			if err := Init(context.TODO(), testAPIEndpoint); (err != nil) != tt.wantErr {
-				switch err.(type) {
-				case viper.ConfigFileNotFoundError:
-				default:
+				if errors.As(err, &viper.ConfigFileNotFoundError{}) {
 					t.Errorf("gosdn cli init error = %v, wantErr %v", err, tt.wantErr)
 					return
 				}
diff --git a/controller/api/device.go b/controller/api/device.go
index 35f211724..91cb3aa67 100644
--- a/controller/api/device.go
+++ b/controller/api/device.go
@@ -2,6 +2,7 @@ package api
 
 import (
 	"context"
+	"errors"
 	"io"
 	"time"
 
@@ -96,7 +97,7 @@ func GetSbiSchemaTree(ctx context.Context, addr string, pid, sid uuid.UUID) (map
 	for {
 		payload, err := sClient.Recv()
 		if err != nil {
-			if err == io.EOF {
+			if errors.Is(err, io.EOF) {
 				break
 			}
 			log.Error(err)
diff --git a/controller/northbound/server/sbi.go b/controller/northbound/server/sbi.go
index a8358dd23..aefdc38ba 100644
--- a/controller/northbound/server/sbi.go
+++ b/controller/northbound/server/sbi.go
@@ -2,6 +2,7 @@ package server
 
 import (
 	"bytes"
+	"errors"
 	"io"
 
 	spb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/southbound"
@@ -68,7 +69,7 @@ func (s SbiServer) GetSchema(request *spb.GetSchemaRequest, stream spb.SbiServic
 	for {
 		n, err := schema.Read(buffer)
 		if err != nil {
-			if err != io.EOF {
+			if errors.Is(err, io.EOF) {
 				log.Println(err)
 			}
 			break
diff --git a/controller/nucleus/principalNetworkDomain.go b/controller/nucleus/principalNetworkDomain.go
index 566d6623b..fc369e836 100644
--- a/controller/nucleus/principalNetworkDomain.go
+++ b/controller/nucleus/principalNetworkDomain.go
@@ -3,6 +3,7 @@ package nucleus
 import (
 	"context"
 	"encoding/json"
+	goErrors "errors"
 	"fmt"
 	"io"
 	"os"
@@ -884,7 +885,7 @@ func saveStreamToFile[T StreamClient](sc T, filename string, id uuid.UUID) error
 	for {
 		payload, err := sc.Recv()
 		if err != nil {
-			if err == io.EOF {
+			if goErrors.Is(err, io.EOF) {
 				break
 			}
 			closeErr := sc.CloseSend()
diff --git a/csbi/grpc.go b/csbi/grpc.go
index 438cc5ca8..80b772cfd 100644
--- a/csbi/grpc.go
+++ b/csbi/grpc.go
@@ -2,6 +2,7 @@ package csbi
 
 import (
 	"context"
+	"errors"
 	"fmt"
 	"io"
 	"os"
@@ -136,7 +137,7 @@ func (s server) GetFile(req *pb.GetPayloadRequest, stream pb.CsbiService_GetFile
 	for {
 		n, err := file.Read(buffer)
 		if err != nil {
-			if err != io.EOF {
+			if errors.Is(err, io.EOF) {
 				fmt.Println(err)
 			}
 			break
diff --git a/csbi/write.go b/csbi/write.go
index ed6396c36..78dab546d 100644
--- a/csbi/write.go
+++ b/csbi/write.go
@@ -28,7 +28,7 @@ import (
 // The output includes a package header which is generated.
 func write(ctx context.Context, code *ygen.GeneratedGoCode, path string, sbiType spb.Type) error {
 	if err := os.Mkdir(path, 0755); err != nil {
-		if err.(*fs.PathError).Err.Error() != "file exists" {
+		if err.(*fs.PathError).Err.Error() != "file exists" { //nolint:errorlint
 			return err
 		}
 	}
-- 
GitLab