Переглянути джерело

ci(lint): introduce golangci-lint (#1872)

* ci(lint): introduce golangci-lint

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* fix config

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* add nolint for exceptions

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* add timeout: 10m

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* add build-tags option

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* install dependencies

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* enable more linters

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* add a nolint:misspell exception

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* fix styles for lint

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* fix: add err check

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* fix: gci lint errors

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

* fix lint issues

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>

---------

Signed-off-by: xjasonlyu <xjasonlyu@gmail.com>
Jason Lyu 1 рік тому
батько
коміт
78e6303812

+ 11 - 7
.github/workflows/lint.yaml

@@ -6,7 +6,7 @@ on:
       - '**.go'
       - 'go.mod'
       - 'go.sum'
-      - 'tools/check/revive.toml'
+      - 'tools/check/.golangci.yaml'
       - '.github/workflows/lint.yaml'
 
 jobs:
@@ -29,10 +29,14 @@ jobs:
           go install github.com/google/go-licenses@latest
           $(go env GOPATH)/bin/go-licenses check ./... --disallowed_types forbidden,restricted,unknown --ignore modernc.org/mathutil
 
-      - name: Run Revive Action
-        uses: docker://morphy/revive-action:v2
-        with:
-          config: tools/check/revive.toml
+      - name: Install dependencies
+        env:
+          DEBIAN_FRONTEND: noninteractive
+        run: |
+          sudo apt-get install -y --no-install-recommends pkg-config libzmq3-dev
 
-      - name: Check formatting
-        run: test -z $(gofmt -l .) || (gofmt -l . && exit 1)
+      - name: golangci-lint
+        uses: golangci/golangci-lint-action@v3
+        with:
+          version: latest
+          args: --config tools/check/.golangci.yaml

+ 4 - 4
Makefile

@@ -143,9 +143,9 @@ tidy:
 	@echo "go mod tidy"
 	go mod tidy && git diff go.mod go.sum
 
-lint:tools/check/bin/revive
+lint:tools/check/bin/golangci-lint
 	@echo "linting"
-	@tools/check/bin/revive -formatter friendly -config tools/check/revive.toml ./...
+	@tools/check/bin/golangci-lint run -c tools/check/.golangci.yaml ./...
 
-tools/check/bin/revive:
-	GOBIN=$(shell pwd)/tools/check/bin $(GO) install github.com/mgechev/revive@v1.2.1
+tools/check/bin/golangci-lint:
+	curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell pwd)/tools/check/bin

+ 16 - 14
extensions/functions/geohash/geohash.go

@@ -22,20 +22,22 @@ import (
 	"github.com/lf-edge/ekuiper/pkg/api"
 )
 
-type geohashEncode struct{}
-type geohashEncodeInt struct{}
-type geohashDecode struct{}
-type geohashDecodeInt struct{}
-type geohashBoundingBox struct{}
-type geohashBoundingBoxInt struct{}
-type geohashNeighbor struct{}
-type geohashNeighborInt struct{}
-type geohashNeighbors struct{}
-type geohashNeighborsInt struct{}
-type position struct {
-	Longitude float64
-	Latitude  float64
-}
+type (
+	geohashEncode         struct{}
+	geohashEncodeInt      struct{}
+	geohashDecode         struct{}
+	geohashDecodeInt      struct{}
+	geohashBoundingBox    struct{}
+	geohashBoundingBoxInt struct{}
+	geohashNeighbor       struct{}
+	geohashNeighborInt    struct{}
+	geohashNeighbors      struct{}
+	geohashNeighborsInt   struct{}
+	position              struct {
+		Longitude float64
+		Latitude  float64
+	}
+)
 
 var (
 	GeohashEncode         geohashEncode

+ 1 - 1
internal/binder/function/funcs_array_test.go

@@ -30,7 +30,7 @@ func TestArrayFunctions(t *testing.T) {
 	ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
 	tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
 	fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
-	var tests = []struct {
+	tests := []struct {
 		name   string
 		args   []interface{}
 		result interface{}

+ 1 - 0
internal/binder/function/funcs_obj.go

@@ -16,6 +16,7 @@ package function
 
 import (
 	"fmt"
+
 	"github.com/lf-edge/ekuiper/pkg/api"
 	"github.com/lf-edge/ekuiper/pkg/ast"
 )

+ 1 - 1
internal/binder/function/funcs_obj_test.go

@@ -31,7 +31,7 @@ func TestObjectFunctions(t *testing.T) {
 	ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
 	tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
 	fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
-	var tests = []struct {
+	tests := []struct {
 		name   string
 		args   []interface{}
 		result interface{}

+ 2 - 1
internal/converter/protobuf/fieldConverterSingleton.go

@@ -17,7 +17,8 @@ package protobuf
 import (
 	"fmt"
 
-	"github.com/golang/protobuf/proto"
+	// TODO: replace with `google.golang.org/protobuf/proto` pkg.
+	"github.com/golang/protobuf/proto" //nolint:staticcheck
 	dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
 	"github.com/jhump/protoreflect/desc"
 	"github.com/jhump/protoreflect/dynamic"

+ 1 - 1
internal/io/memory/memory_test.go

@@ -85,7 +85,7 @@ func TestSharedInmemoryNode(t *testing.T) {
 				t.Errorf("result %s should be equal to %s", res, expected)
 			}
 			return
-		default:
+		default: //nolint
 		}
 	}
 }

+ 2 - 1
internal/plugin/wasm/manager.go

@@ -27,12 +27,13 @@ import (
 	"strings"
 	"sync"
 
+	"github.com/second-state/WasmEdge-go/wasmedge"
+
 	"github.com/lf-edge/ekuiper/internal/conf"
 	"github.com/lf-edge/ekuiper/internal/pkg/filex"
 	"github.com/lf-edge/ekuiper/internal/pkg/httpx"
 	"github.com/lf-edge/ekuiper/internal/plugin"
 	"github.com/lf-edge/ekuiper/internal/plugin/wasm/runtime"
-	"github.com/second-state/WasmEdge-go/wasmedge"
 )
 
 var manager *Manager

+ 2 - 1
internal/plugin/wasm/runtime/function.go

@@ -19,9 +19,10 @@ import (
 	"fmt"
 	"log"
 
+	"github.com/second-state/WasmEdge-go/wasmedge"
+
 	"github.com/lf-edge/ekuiper/internal/conf"
 	"github.com/lf-edge/ekuiper/pkg/api"
-	"github.com/second-state/WasmEdge-go/wasmedge"
 )
 
 type WasmFunc struct {

+ 1 - 1
internal/server/rest_test.go

@@ -285,7 +285,7 @@ func Test_rulesManageHandler(t *testing.T) {
 	req1, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/rules/rule1/status", bytes.NewBufferString("any"))
 	w1 = httptest.NewRecorder()
 	r.ServeHTTP(w1, req1)
-	returnVal, _ = io.ReadAll(w1.Result().Body)
+	returnVal, _ = io.ReadAll(w1.Result().Body) //nolint
 
 	// get rule topo
 	req1, _ = http.NewRequest(http.MethodGet, "http://localhost:8080/rules/rule1/topo", bytes.NewBufferString("any"))

+ 2 - 1
internal/server/rpc_plugin_wasm.go

@@ -19,8 +19,9 @@ package server
 import (
 	"encoding/json"
 	"fmt"
-	"github.com/lf-edge/ekuiper/internal/plugin"
 	"strings"
+
+	"github.com/lf-edge/ekuiper/internal/plugin"
 )
 
 func (t *Server) doRegister(pt plugin.PluginType, p plugin.Plugin) error {

+ 2 - 1
internal/service/executors.go

@@ -28,7 +28,8 @@ import (
 	"sync"
 	"time"
 
-	"github.com/golang/protobuf/proto"
+	// TODO: replace with `google.golang.org/protobuf/proto` pkg.
+	"github.com/golang/protobuf/proto" //nolint:staticcheck
 	"github.com/jhump/protoreflect/dynamic"
 	"github.com/jhump/protoreflect/dynamic/grpcdynamic"
 	"github.com/ugorji/go/codec"

+ 2 - 1
internal/topo/node/sink_node_test.go

@@ -27,6 +27,7 @@ import (
 	"time"
 
 	"github.com/benbjohnson/clock"
+
 	"github.com/lf-edge/ekuiper/internal/conf"
 	"github.com/lf-edge/ekuiper/internal/schema"
 	"github.com/lf-edge/ekuiper/internal/testx"
@@ -44,7 +45,7 @@ func TestBatchSink(t *testing.T) {
 	mc := conf.Clock.(*clock.Mock)
 	conf.InitConf()
 	transform.RegisterAdditionalFuncs()
-	var tests = []struct {
+	tests := []struct {
 		config map[string]interface{}
 		data   []map[string]interface{}
 		result [][]byte

+ 1 - 1
internal/topo/node/window_op_test.go

@@ -305,7 +305,7 @@ func TestCountWindow(t *testing.T) {
 				}
 				cw := tt.tuplelist.nextCountWindow()
 				if !reflect.DeepEqual(tt.winTupleSets[j].Content, cw.Content) {
-					t.Errorf("%d. \nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.winTupleSets[j], cw)
+					t.Errorf("%d. \nresult mismatch:\n\nexp=%#v\n\ngot=%#v", i, tt.winTupleSets[j], cw) //nolint:govet
 				}
 			}
 

+ 3 - 0
internal/topo/planner/planner_graph.go

@@ -111,6 +111,9 @@ func PlanByGraph(rule *api.Rule) (*topo.Topo, error) {
 					return nil, err
 				}
 				srcNode, err := transformSourceNode(p, nil, rule.Options)
+				if err != nil {
+					return nil, err
+				}
 				nodeMap[nodeName] = srcNode
 				tp.AddSrc(srcNode)
 			} else {

+ 1 - 1
internal/xsql/row.go

@@ -159,7 +159,7 @@ func (d *AffiliateRow) Clone() AffiliateRow {
 			nd.AliasMap[k] = v
 		}
 	}
-	return *nd
+	return *nd //nolint:govet
 }
 
 func (d *AffiliateRow) IsEmpty() bool {

+ 1 - 1
internal/xsql/valuer_test.go

@@ -427,7 +427,7 @@ func TestLike(t *testing.T) {
 	}
 	sqls := []string{
 		`select a LIKE "string" as t from src`,
-		`select b LIKE "an_ther" from src`,
+		`select b LIKE "an_ther" from src`, //nolint:misspell
 		`select a NOT LIKE "string1" as t from src`,
 		`select a LIKE "str%" as t from src`,
 		`select a LIKE "str\\_ng" as t from src`,

+ 26 - 0
tools/check/.golangci.yaml

@@ -0,0 +1,26 @@
+run:
+  timeout: 10m
+  skip-dirs:
+    - internal/plugin
+  build-tags:
+    - edgex
+
+linters:
+  disable-all: true
+  enable:
+    - gci
+    - gofumpt
+    - govet
+    - staticcheck
+    - misspell
+    - unconvert
+    - usestdlibvars
+
+linters-settings:
+  gci:
+    sections:
+      - standard
+      - default
+      - prefix(github.com/lf-edge/ekuiper)
+  staticcheck:
+    go: '1.20'

+ 0 - 51
tools/check/revive.toml

@@ -1,51 +0,0 @@
-ignoreGeneratedHeader = false
-severity = "error"
-confidence = 0.8
-errorCode = -1
-warningCode = -1
-
-[rule.blank-imports]
-[rule.context-as-argument]
-[rule.dot-imports]
-[rule.error-return]
-#[rule.error-strings]
-[rule.error-naming]
-#[rule.exported]
-[rule.if-return]
-#[rule.var-naming]
-#[rule.package-comments]
-[rule.range]
-[rule.receiver-naming]
-#[rule.indent-error-flow]
-[rule.superfluous-else]
-[rule.modifies-parameter]
-
-# This can be checked by other tools like megacheck
-[rule.unreachable-code]
-
-
-# Currently this makes too much noise, but should add it in
-# and perhaps ignore it in a few files
-#[rule.confusing-naming]
-#  severity = "warning"
-#[rule.confusing-results]
-#  severity = "warning"
-#[rule.unused-parameter]
-#  severity = "warning"
-#[rule.deep-exit]
-#  severity = "warning"
-#[rule.flag-parameter]
-#  severity = "warning"
-
-
-
-# Adding these will slow down the linter
-# They are already provided by megacheck
-# [rule.unexported-return]
-# [rule.time-naming]
-# [rule.errorf]
-
-# Adding these will slow down the linter
-# Not sure if they are already provided by megacheck
-# [rule.var-declaration]
-# [rule.context-keys-type]