Skip to content

Commit

Permalink
Bump Go and linter
Browse files Browse the repository at this point in the history
  • Loading branch information
grongor committed Dec 12, 2022
1 parent f93de04 commit 261fc96
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 65 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/bin
/dist

/config.toml
Expand Down
48 changes: 33 additions & 15 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
run:
modules-download-mode: readonly

linters:
linters: # 2022-07-21 modified
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- contextcheck
- cyclop
- dupl
- durationcheck
Expand All @@ -21,19 +24,21 @@ linters:
- goerr113
- gofumpt
- gosec
- ifshort
# - ifshort # not yet mature, has some bugs
- lll
- makezero
- misspell
- nakedret
- nilerr
- nilnil
- nlreturn
- nolintlint
# - paralleltest # @todo check later, maybe enable
# - paralleltest # @todo check later, maybe enable
- prealloc
- predeclared
- revive
- sqlclosecheck
- tenv
- testpackage
- thelper
- unconvert
Expand All @@ -45,16 +50,16 @@ linters-settings:
exhaustive:
default-signifies-exhaustive: true

goimports:
local-prefixes: gitlab.cdn77.eu

govet:
check-shadowing: false
enable-all: true
disable:
- shadow
- fieldalignment # @todo fix later

revive:
gosec:
excludes: [G501, G502, G503, G504, G505] # ignore imports, usages are still reported

revive: # v1.2.3
ignoreGeneratedHeader: true
confidence: 0.8
errorCode: 0
Expand All @@ -64,18 +69,23 @@ linters-settings:
- name: bare-return
- name: blank-imports
- name: bool-literal-in-expr
- name: confusing-naming
- name: confusing-results
- name: constant-logical-expr
- name: context-as-argument
- name: context-keys-type
#- name: datarace # add when golangci 1.49 lands
- name: deep-exit
- name: defer
- name: dot-imports
- name: duplicated-imports
- name: early-return
- name: empty-block
- name: empty-lines
- name: error-naming
- name: error-return
- name: error-strings
- name: errorf
- name: flag-parameter
- name: get-return
- name: identical-branches
Expand All @@ -93,13 +103,16 @@ linters-settings:
- name: string-of-int
- name: struct-tag
- name: superfluous-else
- name: time-equal
- name: time-naming
- name: unconditional-recursion
- name: unexported-naming
- name: unexported-return
- name: unnecessary-stmt
- name: unused-parameter
- name: unused-receiver
- name: use-any
- name: useless-break
- name: var-declaration
- name: waitgroup-by-value

Expand All @@ -113,27 +126,32 @@ issues:
max-issues-per-linter: 0
max-same-issues: 0
exclude-rules:
# ignore unchecked errors, missing Close(), code complexity, global variables, line lengths and code duplicity in tests
# ignore unchecked errors, missing Close(), code complexity, constant enforcement, global variables,
# line lengths and code duplicity in tests
- path: _test\.go
linters: [errcheck, bodyclose, cyclop, gocognit, gochecknoglobals, lll, dupl]
linters: [errcheck, bodyclose, cyclop, gocognit, goconst, gochecknoglobals, lll, dupl]
# ignore control flags in tests
- path: _test\.go
text: "seems to be a control flag, avoid control coupling"
text: seems to be a control flag, avoid control coupling
linters: [revive]
# ignore unchecked errors in defer statements
- source: "^\t+defer "
linters: [errcheck]
# ignore default Revive linters which we don't want
- text: "^(errorf|exported|package-comments|unreachable-code|var-naming): "
linters: [revive]
# ignore defer cuddle in tests
# ignore cuddle in tests
- path: _test\.go
text: only one cuddle assignment allowed before defer statement
linters: [wsl]
# ignore expressions after assignment in tests
- path: _test\.go
text: only cuddled expressions if assigning variable or using from line above
linters: [wsl]
# ignore expressions and assignments cuddling in tests
- path: _test\.go
text: assignments should only be cuddled with other assignments
linters: [wsl]
# ignore goerr113 dynamic errors definition error...not sure how to approach this correctly now
- text: do not define dynamic errors, use wrapped static errors instead
linters: [goerr113]

output:
sort-results: true
17 changes: 7 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GO_ACC ?= go-acc
export BIN = ${PWD}/bin
export GOBIN = $(BIN)
GOLANGCI_LINT ?= go run github.com/golangci/golangci-lint/cmd/golangci-lint@latest
GO_ACC ?= go run github.com/golangci/golangci-lint/cmd/golangci-lint@latest
GO_ACC ?= go run github.com/ory/go-acc@latest

.PHONY: build
build:
Expand All @@ -10,12 +10,12 @@ build:
check: lint test

.PHONY: lint
lint: $(BIN)/golangci-lint
$(BIN)/golangci-lint run
lint:
$(GOLANGCI_LINT) run

.PHONY: fix
fix: $(BIN)/golangci-lint
$(BIN)/golangci-lint run --fix
fix:
$(GOLANGCI_LINT) run --fix

.PHONY: test
test:
Expand All @@ -35,6 +35,3 @@ coverage:
clean:
rm -rf bin
rm -rf dist

$(BIN)/golangci-lint:
curl --retry 5 -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/grongor/go-snmp-proxy

go 1.17
go 1.18

require (
github.com/TheZeroSlave/zapsentry v1.7.0
Expand Down
3 changes: 2 additions & 1 deletion metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"errors"
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -24,7 +25,7 @@ func Start(logger *zap.SugaredLogger, listen string) {
)

go func() {
err = http.ListenAndServe(listen, nil)
err = (&http.Server{Addr: listen, ReadHeaderTimeout: time.Second}).ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) {
logger.Fatalw("failed to start metrics listener", zap.Error(err))
}
Expand Down
6 changes: 3 additions & 3 deletions snmpproxy/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"io"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -73,7 +73,7 @@ func (l *ApiListener) ServeHTTP(writer http.ResponseWriter, request *http.Reques
_, _ = writer.Write(response.Bytes())
}()

body, err := ioutil.ReadAll(request.Body)
body, err := io.ReadAll(request.Body)
if err != nil {
l.logger.Debugw("failed to read request body", zap.Error(err))
writer.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -141,7 +141,7 @@ func NewApiListener(
validator: validator,
requester: requester,
logger: logger,
server: &http.Server{Addr: listen, Handler: mux},
server: &http.Server{Addr: listen, Handler: mux, ReadHeaderTimeout: time.Second},
socketPermissions: socketPermissions,
}

Expand Down
17 changes: 8 additions & 9 deletions snmpproxy/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand All @@ -24,15 +23,15 @@ type mockRequester struct {
mock.Mock
}

func (r *mockRequester) ExecuteRequest(apiRequest *snmpproxy.ApiRequest) ([][]interface{}, error) {
func (r *mockRequester) ExecuteRequest(apiRequest *snmpproxy.ApiRequest) ([][]any, error) {
args := r.Mock.Called(apiRequest)

result := args.Get(0)
if result == nil {
return nil, args.Error(1)
}

return result.([][]interface{}), args.Error(1)
return result.([][]any), args.Error(1)
}

type errReader struct{}
Expand Down Expand Up @@ -191,7 +190,7 @@ func TestListenerNoError(t *testing.T) {
requester := &mockRequester{}
defer requester.AssertExpectations(t)

requester.On("ExecuteRequest", mock.Anything).Once().Return([][]interface{}{{".1.2.3", 123}}, nil)
requester.On("ExecuteRequest", mock.Anything).Once().Return([][]any{{".1.2.3", 123}}, nil)

listener := snmpproxy.NewApiListener(newValidator(), requester, zap.NewNop().Sugar(), "", 0)

Expand All @@ -214,7 +213,7 @@ func TestStartAndClose(t *testing.T) {
requester := &mockRequester{}
defer requester.AssertExpectations(t)

requester.On("ExecuteRequest", mock.Anything).Once().Return([][]interface{}{{".1.2.3", 123}}, nil)
requester.On("ExecuteRequest", mock.Anything).Once().Return([][]any{{".1.2.3", 123}}, nil)

listener := snmpproxy.NewApiListener(newValidator(), requester, zap.NewNop().Sugar(), "localhost:15721", 0)
listener.Start()
Expand All @@ -241,9 +240,9 @@ func TestStartAndCloseOnSocket(t *testing.T) {
requester := &mockRequester{}
defer requester.AssertExpectations(t)

requester.On("ExecuteRequest", mock.Anything).Once().Return([][]interface{}{{".1.2.3", 123}}, nil)
requester.On("ExecuteRequest", mock.Anything).Once().Return([][]any{{".1.2.3", 123}}, nil)

f, err := ioutil.TempFile("", "snmp-proxy-test-*.sock")
f, err := os.CreateTemp("", "snmp-proxy-test-*.sock")
assert.NoError(err)
assert.NoError(f.Close())
assert.NoError(os.Remove(f.Name()))
Expand Down Expand Up @@ -281,7 +280,7 @@ func TestStartSocketWithCorrectPermissions(t *testing.T) {

requester := &mockRequester{}

f, err := ioutil.TempFile("", "snmp-proxy-test-*.sock")
f, err := os.CreateTemp("", "snmp-proxy-test-*.sock")
assert.NoError(err)
assert.NoError(f.Close())
assert.NoError(os.Remove(f.Name()))
Expand All @@ -308,7 +307,7 @@ func TestStartError(t *testing.T) {
}

func read(r io.Reader) string {
b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions snmpproxy/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func (r *ApiRequest) UnmarshalJSON(data []byte) error {
}

type Response struct {
Error string `json:"error,omitempty"`
Result [][]interface{} `json:"result,omitempty"`
Error string `json:"error,omitempty"`
Result [][]any `json:"result,omitempty"`
}

func (r *Response) Bytes() []byte {
Expand Down
4 changes: 2 additions & 2 deletions snmpproxy/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func TestMarshalResponse(t *testing.T) {
}{
{
name: "success",
response: snmpproxy.Response{Result: [][]interface{}{{".1.2.3", 123, ".4.5.6", "lorem"}}},
response: snmpproxy.Response{Result: [][]any{{".1.2.3", 123, ".4.5.6", "lorem"}}},
expected: `{"result":[[".1.2.3",123,".4.5.6","lorem"]]}`,
},
{
Expand All @@ -307,5 +307,5 @@ func TestMarshalResponseWithError(t *testing.T) {
}
}()

(&snmpproxy.Response{Result: [][]interface{}{{make(chan struct{})}}}).Bytes()
(&snmpproxy.Response{Result: [][]any{{make(chan struct{})}}}).Bytes()
}
2 changes: 1 addition & 1 deletion snmpproxy/mib/netsnmp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !nonetsnmp
//go:build !nonetsnmp

package mib

Expand Down
1 change: 0 additions & 1 deletion snmpproxy/mib/netsnmp_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !nonetsnmp
// +build !nonetsnmp

package mib_test

Expand Down
2 changes: 1 addition & 1 deletion snmpproxy/mib/nonetsnmp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build nonetsnmp
//go:build nonetsnmp

package mib

Expand Down
2 changes: 1 addition & 1 deletion snmpproxy/mib/nonetsnmp_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build nonetsnmp
//go:build nonetsnmp

package mib_test

Expand Down
Loading

0 comments on commit 261fc96

Please sign in to comment.