Skip to content

Commit

Permalink
Add linters
Browse files Browse the repository at this point in the history
  • Loading branch information
NiJeTi committed Dec 26, 2024
1 parent 7cf5afa commit 1494108
Show file tree
Hide file tree
Showing 15 changed files with 375 additions and 23 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
max_line_length = 80
charset = utf-8
end_of_line = lf

[*.go]
indent_style = tab
indent_size = 4
6 changes: 4 additions & 2 deletions .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ jobs:
- name: 'Install dependencies'
run: go mod download

- name: 'Vet'
run: go vet ./...
- name: 'Run linters'
uses: golangci/golangci-lint-action@v6
with:
version: v1.62.2

- name: 'Build migrator'
run: go build ${{ env.MIGRATOR }}
Expand Down
172 changes: 172 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
linters:
disable-all: true
enable:
- asciicheck
- copyloopvar
- decorder
- dupl
- errcheck
- errname
- errorlint
- exhaustive
- gci
- gochecknoglobals
- gochecknoinits
- gocognit
- gocritic
- gocyclo
- godot
- gofmt
- gofumpt
- goimports
- gosec
- gosimple
- govet
- inamedparam
- ineffassign
- intrange
- ireturn
- lll
- makezero
- mnd
- nilerr
- nilnil
- nolintlint
- paralleltest
- perfsprint
# - prealloc # Use in case of performance problems
- revive
- rowserrcheck
- sloglint # todo: configure
- sqlclosecheck
- staticcheck # todo: configure
- stylecheck # todo: configure
- tagalign
- tagliatelle
- testifylint # todo: configure
- testpackage
- tparallel
- unconvert
- unparam
- unused
- usestdlibvars
- wastedassign
- whitespace
- wrapcheck
linters-settings:
copyloopvar:
check-alias: true
dupl:
threshold: 100
errcheck:
check-type-assertions: true
exhaustive:
default-signifies-exhaustive: true
default-case-required: true
gci:
sections:
- standard
- default
- localmodule
custom-order: true
gocognit:
min-complexity: 20
gocritic:
enable-all: true
disabled-checks:
- deferInLoop
- hugeParam
- paramTypeCombine
- tooManyResultsChecker
gocyclo:
min-complexity: 20
gofmt:
rewrite-rules:
- pattern: 'interface{}'
replacement: 'any'
gofumpt:
extra-rules: true
govet:
enable-all: true
ireturn:
reject:
- anon
lll:
line-length: 80
nolintlint:
require-explanation: true
require-specific: true
paralleltest:
ignore-missing-subtests: true
revive:
rules:
- name: add-constant
- name: atomic
- name: blank-imports
- name: bool-literal-in-expr
- name: call-to-gc
- name: cognitive-complexity
arguments: [20]
- name: comment-spacings
- name: confusing-naming
- name: confusing-results
- name: constant-logical-expr
- name: context-as-argument
- name: context-keys-type
- name: cyclomatic
arguments: [20]
- name: datarace
- name: deep-exit
- name: defer
- name: dot-imports
- name: duplicated-imports
- name: early-return
- name: empty-block
- name: empty-lines
- name: enforce-map-style
arguments: ["make"]
- name: enforce-slice-style
arguments: ["make"]
- name: error-naming
- name: error-return
- name: error-strings
- name: errorf
- name: exported
- name: flag-parameter
- name: get-return
- name: identical-branches
- name: if-return
- name: import-alias-naming
- name: import-shadowing
- name: increment-decrement
- name: indent-error-flow
- name: modifies-parameter
- name: modifies-value-receiver
- name: range-val-address
- name: range-val-in-closure
- name: redefines-builtin-id
- name: redundant-import-alias
- name: string-of-int
- name: superfluous-else
- name: time-equal
- name: time-naming
- name: unconditional-recursion
- name: unexported-naming
- name: unexported-return
- name: unhandled-error
- name: unnecessary-stmt
- name: unreachable-code
- name: unused-parameter
- name: unused-receiver
- name: use-any
- name: use-errors-new
- name: useless-break
- name: var-declaration
- name: var-naming
- name: waitgroup-by-value
tagliatelle:
case:
use-field-name: true
rules:
json: snake
yaml: kebab
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
MOCKERY_IMAGE=vektra/mockery:v2.50
GOLANGCI_LINT_IMAGE=golangci/golangci-lint:v1.62-alpine

.PHONY: deps
deps:
go install github.com/vektra/mockery/[email protected]
docker pull $(MOCKERY_IMAGE)
docker pull $(GOLANGCI_LINT_IMAGE)

.PHONY: mocks
mocks:
$(MAKE) deps

rm -rf ./internal/generated/mocks
mockery
docker run -t --rm -v $(PWD):/src -w /src $(MOCKERY_IMAGE)

.PHONY: lint
lint:
$(MAKE) deps

docker run -t --rm -v $(PWD):/src -w /src $(GOLANGCI_LINT_IMAGE) golangci-lint run -v

.PHONY: devenv
devenv:
Expand Down
7 changes: 7 additions & 0 deletions cmd/migrator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ type config struct {
}

func main() {
code := run()
os.Exit(code)
}

func run() int {
log.Println("starting")

cfg := cfgPkg.ReadConfig[config]()
Expand All @@ -35,7 +40,9 @@ func main() {
err := dbMigrator.Migrate()
if err != nil {
log.Fatalln("failed to migrate db:", err)

Check failure on line 42 in cmd/migrator/main.go

View workflow job for this annotation

GitHub Actions / validate

exitAfterDefer: log.Fatalln will exit, and `defer dbConn.Close()` will not run (gocritic)
return 1
}

log.Println("database migration complete")
return 0
}
2 changes: 1 addition & 1 deletion internal/db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"database/sql"
"log"

_ "github.com/lib/pq"
_ "github.com/lib/pq" // postgres driver
)

type Config struct {
Expand Down
6 changes: 4 additions & 2 deletions internal/discord/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package discord

import (
"github.com/bwmarrin/discordgo"

"github.com/nijeti/cinema-keeper/internal/pkg/utils"
)

const (
LockName = "lock"
LockOptionLimit = "limit"
)

var (
const (
lockOptionLimitMinValue float64 = 1
lockOptionLimitMaxValue float64 = 99
)
Expand All @@ -22,7 +24,7 @@ var Lock = &discordgo.ApplicationCommand{
Name: LockOptionLimit,
Description: "Custom limit value",
Type: discordgo.ApplicationCommandOptionInteger,
MinValue: &lockOptionLimitMinValue,
MinValue: utils.Ptr(lockOptionLimitMinValue),
MaxValue: lockOptionLimitMaxValue,
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/discord/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ func NewProbe(

func (p *Probe) Check(_ context.Context) error {
_, err := p.session.User("@me")
return err
return err //nolint:wrapcheck

Check failure on line 23 in internal/discord/probe.go

View workflow job for this annotation

GitHub Actions / validate

whyNoLint: include an explanation for nolint directive (gocritic)
}
5 changes: 3 additions & 2 deletions internal/discord/roll.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/bwmarrin/discordgo"

"github.com/nijeti/cinema-keeper/internal/pkg/die"
"github.com/nijeti/cinema-keeper/internal/pkg/utils"
)

const (
Expand All @@ -19,7 +20,7 @@ const (
RollOptionCountDefault = 1
)

var (
const (
rollOptionCountMinValue float64 = 1
rollOptionCountMaxValue float64 = 10
)
Expand Down Expand Up @@ -58,7 +59,7 @@ func buildRoll() *discordgo.ApplicationCommand {
Name: RollOptionCount,
Description: "Number of dice",
Type: discordgo.ApplicationCommandOptionInteger,
MinValue: &rollOptionCountMinValue,
MinValue: utils.Ptr(rollOptionCountMinValue),
MaxValue: rollOptionCountMaxValue,
},
},
Expand Down
17 changes: 8 additions & 9 deletions internal/generated/mocks/discord/handler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1494108

Please sign in to comment.