From 598e062d92e8df8e4d71441b8819719d75ce7806 Mon Sep 17 00:00:00 2001 From: Luca Comellini Date: Tue, 3 Dec 2024 18:14:06 -0800 Subject: [PATCH] Add golangci-lint --- .github/workflows/lint.yml | 45 +++++++++++++++++++ .golangci.yml | 90 ++++++++++++++++++++++++++++++++++++++ log/log.go | 30 ++++++------- utils/fields.go | 1 - utils/fields_test.go | 2 - 5 files changed, 149 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/lint.yml create mode 100644 .golangci.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..e1f9f9e --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,45 @@ +name: Lint + +on: + push: + branches: + - master + pull_request: + branches: + - master + +defaults: + run: + shell: bash + +concurrency: + group: ${{ github.ref_name }}-lint + cancel-in-progress: true + +jobs: + lint: + name: Go Lint + runs-on: ubuntu-24.04 + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Setup Golang Environment + uses: actions/setup-go@v5 + with: + go-version: stable + + - name: Lint Go + uses: golangci/golangci-lint-action@v6 + + actionlint: + name: Actionlint + runs-on: ubuntu-24.04 + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Lint Actions + uses: reviewdog/action-actionlint@v1 + with: + actionlint_flags: -shellcheck "" diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..e2ef3c8 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,90 @@ +linters-settings: + misspell: + locale: US + revive: + ignore-generated-header: true + rules: + - name: blank-imports + - name: context-as-argument + - name: context-keys-type + - name: dot-imports + - name: empty-block + - name: error-naming + - name: error-return + - name: error-strings + - name: errorf + - name: exported + - name: increment-decrement + - name: indent-error-flow + - name: package-comments + - name: range + - name: receiver-naming + - name: redefines-builtin-id + - name: superfluous-else + - name: time-naming + - name: unexported-return + - name: unreachable-code + - name: var-declaration + - name: var-naming + govet: + enable-all: true +linters: + enable: + - asasalint + - asciicheck + - bidichk + - contextcheck + - copyloopvar + - dupword + - durationcheck + - errcheck + - errchkjson + - errname + - errorlint + - fatcontext + - forcetypeassert + - gocheckcompilerdirectives + - gochecksumtype + - gocritic + - godot + - gofmt + - gofumpt + - goimports + - gosec + - gosimple + - gosmopolitan + - govet + - ineffassign + - intrange + - makezero + - misspell + - musttag + - nilerr + - noctx + - nolintlint + - paralleltest + - perfsprint + - prealloc + - predeclared + - reassign + - revive + - staticcheck + - stylecheck + - tagalign + - tenv + - thelper + - tparallel + - typecheck + - unconvert + - unparam + - unused + - usestdlibvars + - wastedassign + - whitespace + # - wrapcheck + disable-all: true +issues: + max-issues-per-linter: 0 + max-same-issues: 0 +run: + timeout: 5m diff --git a/log/log.go b/log/log.go index ed6a9a0..3ebc3b2 100644 --- a/log/log.go +++ b/log/log.go @@ -12,7 +12,7 @@ import ( // DEBUG -// DebugWithContext logs on debug level and trace based on the context span if it exists +// DebugWithContext logs on debug level and trace based on the context span if it exists. func DebugWithContext(ctx context.Context, log string, fields ...zapcore.Field) { DebugWithSpan(opentracing.SpanFromContext(ctx), log, fields...) } @@ -23,14 +23,14 @@ func DebugWithSpan(span opentracing.Span, log string, fields ...zapcore.Field) { logSpan(span, log, fields...) } -// Debug logs on debug level +// Debug logs on debug level. func Debug(log string, fields ...zapcore.Field) { zap.L().Debug(log, fields...) } // INFO -// InfoWithContext logs on info level and trace based on the context span if it exists +// InfoWithContext logs on info level and trace based on the context span if it exists. func InfoWithContext(ctx context.Context, log string, fields ...zapcore.Field) { InfoWithSpan(opentracing.SpanFromContext(ctx), log, fields...) } @@ -39,17 +39,16 @@ func InfoWithContext(ctx context.Context, log string, fields ...zapcore.Field) { func InfoWithSpan(span opentracing.Span, log string, fields ...zapcore.Field) { Info(log, fields...) logSpan(span, log, fields...) - } -// Info logs on info level +// Info logs on info level. func Info(log string, fields ...zapcore.Field) { zap.L().Info(log, fields...) } // WARN -// WarnWithContext logs on warn level and trace based on the context span if it exists +// WarnWithContext logs on warn level and trace based on the context span if it exists. func WarnWithContext(ctx context.Context, log string, fields ...zapcore.Field) { WarnWithSpan(opentracing.SpanFromContext(ctx), log, fields...) } @@ -58,17 +57,16 @@ func WarnWithContext(ctx context.Context, log string, fields ...zapcore.Field) { func WarnWithSpan(span opentracing.Span, log string, fields ...zapcore.Field) { Warn(log, fields...) logSpan(span, log, fields...) - } -// Warn logs on warn level +// Warn logs on warn level. func Warn(log string, fields ...zapcore.Field) { zap.L().Warn(log, fields...) } // ERROR -// ErrorWithContext logs on error level and trace based on the context span if it exists +// ErrorWithContext logs on error level and trace based on the context span if it exists. func ErrorWithContext(ctx context.Context, log string, fields ...zapcore.Field) { ErrorWithSpan(opentracing.SpanFromContext(ctx), log, fields...) } @@ -79,14 +77,14 @@ func ErrorWithSpan(span opentracing.Span, log string, fields ...zapcore.Field) { logSpan(span, log, fields...) } -// Error logs on error level +// Error logs on error level. func Error(log string, fields ...zapcore.Field) { zap.L().Error(log, fields...) } // DPANIC -// DPanicWithContext logs on dPanic level and trace based on the context span if it exists +// DPanicWithContext logs on dPanic level and trace based on the context span if it exists. func DPanicWithContext(ctx context.Context, log string, fields ...zapcore.Field) { DPanicWithSpan(opentracing.SpanFromContext(ctx), log, fields...) } @@ -97,14 +95,14 @@ func DPanicWithSpan(span opentracing.Span, log string, fields ...zapcore.Field) DPanic(log, fields...) } -// DPanic logs on dPanic level +// DPanic logs on dPanic level. func DPanic(log string, fields ...zapcore.Field) { zap.L().DPanic(log, fields...) } // PANIC -// PanicWithContext logs on panic level and trace based on the context span if it exists +// PanicWithContext logs on panic level and trace based on the context span if it exists. func PanicWithContext(ctx context.Context, log string, fields ...zapcore.Field) { PanicWithSpan(opentracing.SpanFromContext(ctx), log, fields...) } @@ -115,12 +113,12 @@ func PanicWithSpan(span opentracing.Span, log string, fields ...zapcore.Field) { Panic(log, fields...) } -// Panic logs on panic level +// Panic logs on panic level. func Panic(log string, fields ...zapcore.Field) { zap.L().Panic(log, fields...) } -// FatalWithContext logs on fatal level and trace based on the context span if it exists +// FatalWithContext logs on fatal level and trace based on the context span if it exists. func FatalWithContext(ctx context.Context, log string, fields ...zapcore.Field) { FatalWithSpan(opentracing.SpanFromContext(ctx), log, fields...) } @@ -131,7 +129,7 @@ func FatalWithSpan(span opentracing.Span, log string, fields ...zapcore.Field) { Fatal(log, fields...) } -// Fatal logs on fatal level +// Fatal logs on fatal level. func Fatal(log string, fields ...zapcore.Field) { zap.L().Fatal(log, fields...) } diff --git a/utils/fields.go b/utils/fields.go index e9df068..3bce982 100644 --- a/utils/fields.go +++ b/utils/fields.go @@ -25,7 +25,6 @@ func ZapFieldsToOpentracing(zapFields ...zapcore.Field) []opentracinglog.Field { // input Zap field. func ZapFieldToOpentracing(zapField zapcore.Field) opentracinglog.Field { switch zapField.Type { - case zapcore.BoolType: val := false if zapField.Integer >= 1 { diff --git a/utils/fields_test.go b/utils/fields_test.go index c1d809a..a4bed7b 100644 --- a/utils/fields_test.go +++ b/utils/fields_test.go @@ -18,7 +18,6 @@ func (s stringer) String() string { } func TestFieldsConversion(t *testing.T) { - TestData := []struct { ZapField zapcore.Field OpenTracingField opentracinglog.Field @@ -97,5 +96,4 @@ func TestFieldsConversion(t *testing.T) { t.Errorf("Expected same value. Got %s but expected %s", result.Value(), data.OpenTracingField.Value()) } } - }