From d49c3ed78debd95c939ef81b6f64ce5aa1fd8c26 Mon Sep 17 00:00:00 2001 From: Luca Comellini Date: Thu, 8 Feb 2024 08:28:11 -0800 Subject: [PATCH] Enable more linters (#217) --- .golangci.yml | 15 +++++- .pre-commit-config.yaml | 68 +++++++++++------------- client/nginx.go | 112 ++++++++++++++++++++-------------------- tests/client_test.go | 7 --- 4 files changed, 98 insertions(+), 104 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 5b59549..8474d33 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -28,10 +28,15 @@ linters-settings: - name: unused-parameter - name: var-declaration - name: var-naming + govet: + check-shadowing: true + enable-all: true linters: - enable: + enable: - asciicheck + - bidichk + - dupword - errcheck - errorlint - gofmt @@ -45,15 +50,21 @@ linters: - misspell - nilerr - noctx + - perfsprint - predeclared + - reassign - revive - staticcheck + - tagalign + - tparallel - typecheck - unconvert - unparam - unused + - usestdlibvars - wastedassign - disable-all: true + - whitespace + disable-all: true issues: max-issues-per-linter: 0 max-same-issues: 0 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a860e1d..8fd4c48 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,47 +1,37 @@ # See https://pre-commit.com for more information # See https://pre-commit.com/hooks.html for more hooks repos: -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 - hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml - args: [--allow-multiple-documents] - - id: check-added-large-files - - id: check-merge-conflict - - id: check-shebang-scripts-are-executable - - id: check-case-conflict - - id: check-vcs-permalinks - - id: mixed-line-ending - args: [--fix=lf] - - id: no-commit-to-branch - - id: fix-byte-order-marker + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + args: [--allow-multiple-documents] + - id: check-added-large-files + - id: check-merge-conflict + - id: check-shebang-scripts-are-executable + - id: check-case-conflict + - id: check-vcs-permalinks + - id: mixed-line-ending + args: [--fix=lf] + - id: no-commit-to-branch + - id: fix-byte-order-marker -- repo: local - hooks: - - id: golang-diff - name: create-go-diff - entry: bash -c 'git diff -p origin/main > /tmp/diff.patch' - language: system - types: [go] - pass_filenames: false + - repo: https://github.com/golangci/golangci-lint + rev: v1.55.2 + hooks: + - id: golangci-lint-full -- repo: https://github.com/golangci/golangci-lint - rev: v1.55.2 - hooks: - - id: golangci-lint - args: [--new-from-patch=/tmp/diff.patch] + - repo: https://github.com/gitleaks/gitleaks + rev: v8.18.2 + hooks: + - id: gitleaks -- repo: https://github.com/gitleaks/gitleaks - rev: v8.18.2 - hooks: - - id: gitleaks - -- repo: https://github.com/DavidAnson/markdownlint-cli2 - rev: v0.12.1 - hooks: - - id: markdownlint-cli2 + - repo: https://github.com/DavidAnson/markdownlint-cli2 + rev: v0.12.1 + hooks: + - id: markdownlint-cli2 ci: - skip: [golang-diff, golangci-lint] + skip: [golangci-lint-full] diff --git a/client/nginx.go b/client/nginx.go index 5c01a40..7b06d65 100644 --- a/client/nginx.go +++ b/client/nginx.go @@ -42,9 +42,9 @@ var ErrUnsupportedVer = errors.New("API version of the client is not supported b // NginxClient lets you access NGINX Plus API. type NginxClient struct { - apiVersion int - apiEndpoint string httpClient *http.Client + apiEndpoint string + apiVersion int checkAPI bool } @@ -54,38 +54,38 @@ type versions []int // UpstreamServer lets you configure HTTP upstreams. type UpstreamServer struct { - ID int `json:"id,omitempty"` - Server string `json:"server"` MaxConns *int `json:"max_conns,omitempty"` MaxFails *int `json:"max_fails,omitempty"` - FailTimeout string `json:"fail_timeout,omitempty"` - SlowStart string `json:"slow_start,omitempty"` - Route string `json:"route,omitempty"` Backup *bool `json:"backup,omitempty"` Down *bool `json:"down,omitempty"` - Drain bool `json:"drain,omitempty"` Weight *int `json:"weight,omitempty"` + Server string `json:"server"` + FailTimeout string `json:"fail_timeout,omitempty"` + SlowStart string `json:"slow_start,omitempty"` + Route string `json:"route,omitempty"` Service string `json:"service,omitempty"` + ID int `json:"id,omitempty"` + Drain bool `json:"drain,omitempty"` } // StreamUpstreamServer lets you configure Stream upstreams. type StreamUpstreamServer struct { - ID int `json:"id,omitempty"` - Server string `json:"server"` MaxConns *int `json:"max_conns,omitempty"` MaxFails *int `json:"max_fails,omitempty"` - FailTimeout string `json:"fail_timeout,omitempty"` - SlowStart string `json:"slow_start,omitempty"` Backup *bool `json:"backup,omitempty"` Down *bool `json:"down,omitempty"` Weight *int `json:"weight,omitempty"` + Server string `json:"server"` + FailTimeout string `json:"fail_timeout,omitempty"` + SlowStart string `json:"slow_start,omitempty"` Service string `json:"service,omitempty"` + ID int `json:"id,omitempty"` } type apiErrorResponse struct { - Error apiError RequestID string `json:"request_id"` Href string + Error apiError } func (resp *apiErrorResponse) toString() string { @@ -94,14 +94,14 @@ func (resp *apiErrorResponse) toString() string { } type apiError struct { - Status int Text string Code string + Status int } type internalError struct { - apiError err string + apiError } // Error allows internalError to match the Error interface. @@ -119,24 +119,24 @@ func (internalError *internalError) Wrap(err string) *internalError { // Stats represents NGINX Plus stats fetched from the NGINX Plus API. // https://nginx.org/en/docs/http/ngx_http_api_module.html type Stats struct { - NginxInfo NginxInfo - Caches Caches - Processes Processes - Connections Connections - Slabs Slabs - HTTPRequests HTTPRequests - SSL SSL - ServerZones ServerZones Upstreams Upstreams + ServerZones ServerZones StreamServerZones StreamServerZones StreamUpstreams StreamUpstreams - StreamZoneSync *StreamZoneSync - LocationZones LocationZones - Resolvers Resolvers - HTTPLimitRequests HTTPLimitRequests + Slabs Slabs + Caches Caches HTTPLimitConnections HTTPLimitConnections StreamLimitConnections StreamLimitConnections + HTTPLimitRequests HTTPLimitRequests + Resolvers Resolvers + LocationZones LocationZones + StreamZoneSync *StreamZoneSync Workers []*Workers + NginxInfo NginxInfo + SSL SSL + Connections Connections + HTTPRequests HTTPRequests + Processes Processes } // NginxInfo contains general information about NGINX Plus. @@ -144,9 +144,9 @@ type NginxInfo struct { Version string Build string Address string - Generation uint64 LoadTimestamp string `json:"load_timestamp"` Timestamp string + Generation uint64 ProcessID uint64 `json:"pid"` ParentProcessID uint64 `json:"ppid"` } @@ -194,8 +194,8 @@ type Slabs map[string]Slab // Slab represents slab related stats. type Slab struct { - Pages Pages Slots Slots + Pages Pages } // Pages represents the slab memory usage stats. @@ -358,11 +358,11 @@ type Upstreams map[string]Upstream // Upstream represents upstream related stats. type Upstream struct { + Zone string Peers []Peer + Queue Queue Keepalives int Zombies int - Zone string - Queue Queue } // StreamUpstreams is a map of stream upstream stats by upstream name. @@ -370,9 +370,9 @@ type StreamUpstreams map[string]StreamUpstream // StreamUpstream represents stream upstream related stats. type StreamUpstream struct { + Zone string Peers []StreamPeer Zombies int - Zone string } // Queue represents queue related stats for an upstream. @@ -384,54 +384,54 @@ type Queue struct { // Peer represents peer (upstream server) related stats. type Peer struct { - ID int Server string Service string Name string - Backup bool - Weight int + Selected string + Downstart string State string - Active uint64 + Responses Responses SSL SSL - MaxConns int `json:"max_conns"` + HealthChecks HealthChecks `json:"health_checks"` Requests uint64 - Responses Responses + ID int + MaxConns int `json:"max_conns"` Sent uint64 Received uint64 Fails uint64 Unavail uint64 - HealthChecks HealthChecks `json:"health_checks"` + Active uint64 Downtime uint64 - Downstart string - Selected string + Weight int HeaderTime uint64 `json:"header_time"` ResponseTime uint64 `json:"response_time"` + Backup bool } // StreamPeer represents peer (stream upstream server) related stats. type StreamPeer struct { - ID int Server string Service string Name string - Backup bool - Weight int + Selected string + Downstart string State string - Active uint64 SSL SSL - MaxConns int `json:"max_conns"` + HealthChecks HealthChecks `json:"health_checks"` Connections uint64 + Received uint64 + ID int ConnectTime int `json:"connect_time"` FirstByteTime int `json:"first_byte_time"` ResponseTime uint64 `json:"response_time"` Sent uint64 - Received uint64 + MaxConns int `json:"max_conns"` Fails uint64 Unavail uint64 - HealthChecks HealthChecks `json:"health_checks"` + Active uint64 Downtime uint64 - Downstart string - Selected string + Weight int + Backup bool } // HealthChecks represents health check related stats for a peer. @@ -560,7 +560,7 @@ func NewNginxClient(apiEndpoint string, opts ...Option) (*NginxClient, error) { } if c.httpClient == nil { - return nil, fmt.Errorf("http client is not set") + return nil, errors.New("http client is not set") } if !versionSupported(c.apiVersion) { @@ -1510,7 +1510,7 @@ func (client *NginxClient) getKeyValPairs(zone string, stream bool) (KeyValPairs base = "stream" } if zone == "" { - return nil, fmt.Errorf("zone required") + return nil, errors.New("zone required") } path := fmt.Sprintf("%v/keyvals/%v", base, zone) @@ -1563,7 +1563,7 @@ func (client *NginxClient) addKeyValPair(zone string, key string, val string, st base = "stream" } if zone == "" { - return fmt.Errorf("zone required") + return errors.New("zone required") } path := fmt.Sprintf("%v/keyvals/%v", base, zone) @@ -1591,7 +1591,7 @@ func (client *NginxClient) modifyKeyValPair(zone string, key string, val string, base = "stream" } if zone == "" { - return fmt.Errorf("zone required") + return errors.New("zone required") } path := fmt.Sprintf("%v/keyvals/%v", base, zone) @@ -1621,7 +1621,7 @@ func (client *NginxClient) deleteKeyValuePair(zone string, key string, stream bo base = "stream" } if zone == "" { - return fmt.Errorf("zone required") + return errors.New("zone required") } // map[string]string can't have a nil value so we use a different type here. @@ -1652,7 +1652,7 @@ func (client *NginxClient) deleteKeyValPairs(zone string, stream bool) error { base = "stream" } if zone == "" { - return fmt.Errorf("zone required") + return errors.New("zone required") } path := fmt.Sprintf("%v/keyvals/%v", base, zone) diff --git a/tests/client_test.go b/tests/client_test.go index f580ad7..5a65a53 100644 --- a/tests/client_test.go +++ b/tests/client_test.go @@ -48,7 +48,6 @@ func TestStreamClient(t *testing.T) { // test adding a stream server err = c.AddStreamServer(streamUpstream, streamServer) - if err != nil { t.Fatalf("Error when adding a server: %v", err) } @@ -211,7 +210,6 @@ func TestStreamClient(t *testing.T) { // updating with 2 new servers, 1 existing added, deleted, updated, err = c.UpdateStreamServers(streamUpstream, streamServers2) - if err != nil { t.Fatalf("Error when updating servers: %v", err) } @@ -228,7 +226,6 @@ func TestStreamClient(t *testing.T) { // updating with zero servers - removing added, deleted, updated, err = c.UpdateStreamServers(streamUpstream, []client.StreamUpstreamServer{}) - if err != nil { t.Fatalf("Error when updating servers: %v", err) } @@ -326,7 +323,6 @@ func TestClient(t *testing.T) { // test adding a http server err = c.AddHTTPServer(upstream, server) - if err != nil { t.Fatalf("Error when adding a server: %v", err) } @@ -391,7 +387,6 @@ func TestClient(t *testing.T) { // updating with the same servers added, deleted, updated, err = c.UpdateHTTPServers(upstream, servers1) - if err != nil { t.Fatalf("Error when updating servers: %v", err) } @@ -484,7 +479,6 @@ func TestClient(t *testing.T) { // updating with 2 new servers, 1 existing added, deleted, updated, err = c.UpdateHTTPServers(upstream, servers2) - if err != nil { t.Fatalf("Error when updating servers: %v", err) } @@ -501,7 +495,6 @@ func TestClient(t *testing.T) { // updating with zero servers - removing added, deleted, updated, err = c.UpdateHTTPServers(upstream, []client.UpstreamServer{}) - if err != nil { t.Fatalf("Error when updating servers: %v", err) }