From 78893a70a295fc9ec5d76b0645eef39c7690a50c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Sun, 20 Aug 2023 07:17:36 +0200 Subject: [PATCH] feat: migrate from drone to woodpecker --- .drone.yml | 36 ------------- .golangci.yml | 124 +++++++++++++++++++++++++++++++++++++++++++ .woodpecker/test.yml | 31 +++++++++++ logger.go | 21 +++++--- 4 files changed, 169 insertions(+), 43 deletions(-) delete mode 100644 .drone.yml create mode 100644 .golangci.yml create mode 100644 .woodpecker/test.yml diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index 250a6ea..0000000 --- a/.drone.yml +++ /dev/null @@ -1,36 +0,0 @@ ---- -kind: pipeline -type: docker -name: test - -steps: - - name: test - image: golang:1.20 - commands: - - go test -race -coverprofile=coverage.txt -covermode=atomic ./... - -trigger: - event: - - push - - pull_request - branch: - - master - ---- -kind: pipeline -type: docker -name: check-go-mod - -steps: - - name: check go.mod - image: golang:1.20 - commands: - - go mod tidy - - git diff --exit-code go.mod - -trigger: - event: - - push - - pull_request - branch: - - master diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..dbf9e15 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,124 @@ +run: + tests: true + timeout: 5m + +linters: + disable-all: true + enable: + - asasalint + - asciicheck + - bodyclose + - bidichk + - exportloopref + - depguard + - errcheck + - gocritic + - gosec + - gofmt + - goimports + - gosimple + - govet + - ineffassign + - misspell + - nakedret + - prealloc + - staticcheck + - typecheck + - unconvert + - unused + - lll + - nestif + - thelper + - nonamedreturns + - gocyclo + - gomnd + - tenv + - testpackage + - noctx + - tparallel + - usestdlibvars + - unconvert + - makezero + - grouper + - errname + - exhaustive + - tagliatelle + - contextcheck + - gocheckcompilerdirectives + - errname + - forcetypeassert + - durationcheck + - predeclared + - promlinter + - wastedassign + +linters-settings: + lll: + line-length: 150 + gocyclo: + min-complexity: 10 + depguard: + rules: + main: + files: + - "$all" + deny: + - pkg: reflect + desc: Please don't use reflect package + - pkg: github.com/pkg/errors + desc: Should be replaced by standard lib errors package + govet: + enable: + - asmdecl + - assign + - atomic + - atomicalign + - bools + - buildtag + - cgocall + - composites + - copylocks + - deepequalerrors + - errorsas + - findcall + - framepointer + - httpresponse + - ifaceassert + - loopclosure + - lostcancel + - nilfunc + - nilness + - printf + - reflectvaluecompare + - shadow + - shift + - sigchanyzer + - sortslice + - stdmethods + - stringintconv + - structtag + - testinggoroutine + - tests + - unmarshal + - unreachable + - unsafeptr + - unusedresult + - unusedwrite + gomnd: + ignored-functions: + - strconv.FormatInt + - strconv.ParseInt + +issues: + exclude-rules: + # Exclude some linters from running on tests files. + - path: _test\.go + linters: + - dupl + - gocyclo + - linters: + - lll + source: "^//go:generate " + - linters: + - lll + source: "^// @Param" diff --git a/.woodpecker/test.yml b/.woodpecker/test.yml new file mode 100644 index 0000000..444a1d8 --- /dev/null +++ b/.woodpecker/test.yml @@ -0,0 +1,31 @@ +when: + - event: [pull_request] + - event: push + branch: + - ${CI_REPO_DEFAULT_BRANCH} + +variables: + - &go_image 'go:1.21' + +steps: + test: + image: *go_image + group: test + pull: true + commands: + - go test -race -coverprofile=coverage.txt -covermode=atomic ./... + + lint: + image: golangci/golangci-lint:v1.54 + pull: true + group: test + commands: + - golangci-lint run + + check-go-mod: + image: *go_image + group: test + pull: true + commands: + - go mod tidy + - git diff --exit-code go.mod diff --git a/logger.go b/logger.go index 6387a1f..4552095 100644 --- a/logger.go +++ b/logger.go @@ -7,6 +7,7 @@ import ( "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "go.uber.org/zap" + "go.uber.org/zap/zapcore" ) // Logger returns a go-chi middleware that logs requests using go.uber.org/zap. @@ -49,13 +50,19 @@ func Logger(logger *zap.Logger, opts ...Option) func(next http.Handler) http.Han fields = append(fields, f(r)...) } - if statusCode >= http.StatusInternalServerError { - logger.Error(path, fields...) - } else if statusCode >= http.StatusBadRequest { - logger.Warn(path, fields...) - } else { - logger.Info(path, fields...) - } + logger.Log(statusCodeToLogLvl(statusCode), path, fields...) }) } } + +func statusCodeToLogLvl(statusCode int) zapcore.Level { + if statusCode >= http.StatusInternalServerError { + return zap.ErrorLevel + } + + if statusCode >= http.StatusBadRequest { + return zap.WarnLevel + } + + return zap.InfoLevel +}