Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add golangci-lint #23

Merged
merged 4 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: golangci-lint
on:
push:
branches:
- main
pull_request:

permissions:
contents: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.20"
ahkui marked this conversation as resolved.
Show resolved Hide resolved
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.53
args: --issues-exit-code 0 --print-issued-lines=false --out-format code-climate:gl-code-quality-report.json,line-number
232 changes: 232 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
output:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
format: colored-line-number

# all available settings of specific linters
linters-settings:
errcheck:
check-type-assertions: false
check-blank: false

# Disable error checking, as errorcheck detects more errors and is more configurable.
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# See https://github.com/securego/gosec#available-rules
gosec:
gohiei marked this conversation as resolved.
Show resolved Hide resolved
exclude:
- G104

funlen:
lines: 60
gohiei marked this conversation as resolved.
Show resolved Hide resolved
statements: 40

govet:
# report about shadowed variables
check-shadowing: true

# settings per analyzer
settings:
printf: # analyzer name, run `go tool vet help` to see all analyzers
funcs: # run `go tool vet help printf` to see available settings for `printf` analyzer
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf

gofmt:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# simplify code: gofmt with `-s` option, true by default
simplify: true

goimports:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# put imports beginning with prefix after 3rd-party packages;
# it's a comma-separated list of prefixes
# local-prefixes: github.com/org/project

gocognit:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# minimal code complexity to report, 30 by default (but we recommend 10-20)
min-complexity: 20

# See https://github.com/mibk/dupl
dupl:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# tokens count to trigger issue, 150 by default
threshold: 100

goconst:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# minimal length of string constant, 3 by default
min-len: 3
# minimal occurrences count to trigger, 3 by default
min-occurrences: 3

misspell:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
locale: US
ignore-words:
- GitLab

unused:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# treat code as a program (not a library) and report unused exported identifiers; default is false.
# XXX: if you enable this setting, unused will report a lot of false-positives in text editors:
# if it's called for subdir of a project it can't find funcs usages. All text editor integrations
# with golangci-lint call it on a directory with the changed file.
check-exported: false

unparam:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
# if it's called for subdir of a project it can't find external interfaces. All text editor integrations
# with golangci-lint call it on a directory with the changed file.
check-exported: false

nakedret:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# make an issue if func has more lines of code than this setting and it has naked returns; default is 30
max-func-lines: 30

revive:
ignore-generated-header: true

prealloc:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# XXX: we don't recommend using this linter before doing performance profiling.
# For most programs usage of prealloc will be a premature optimization.

# Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them.
# True by default.
simple: true
range-loops: true # Report preallocation suggestions on range loops, true by default
for-loops: false # Report preallocation suggestions on for loops, false by default

gocritic:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# Which checks should be enabled; can't be combined with 'disabled-checks';
# See https://go-critic.github.io/overview#checks-overview
# To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run`
# By default list of stable checks is used.
# enabled-checks:
# - rangeValCopy

# Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty
# disabled-checks:
# - regexpMust

# Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks.
# Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags".
enabled-tags:
- performance
disabled-tags:
- experimental

# Settings passed to gocritic.
# The settings key is the name of a supported gocritic checker.
# The list of supported checkers can be find in https://go-critic.github.io/overview.
settings:
captLocal: # must be valid enabled check name
# whether to restrict checker to params only (default true)
paramsOnly: true
elseif:
# whether to skip balanced if-else pairs (default true)
skipBalanced: true
# rangeValCopy:
# sizeThreshold: 32

godox:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# report any comments starting with keywords, this is useful for TODO or FIXME comments that
# might be left in the code accidentally and should be resolved before merging
keywords: # default keywords are TODO, BUG, and FIXME, these can be overwritten by this setting
- TODO
- BUG
- FIXME
- NOTE
- OPTIMIZE # marks code that should be optimized before merging
- HACK # marks hack-arounds that should be removed before merging

dogsled:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# checks assignments with too many blank identifiers; default is 2
max-blank-identifiers: 2

whitespace:
multi-if: true # Enforces newlines (or comments) after every multi-line if statement
multi-func: true # Enforces newlines (or comments) after every multi-line function signature

wsl:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# If true append is only allowed to be cuddled if appending value is
# matching variables, fields or types on line above. Default is true.
strict-append: true
# Allow calls and assignments to be cuddled as long as the lines have any
# matching variables, fields or types. Default is true.
allow-assign-and-call: true
# Allow multiline assignments to be cuddled. Default is true.
allow-multiline-assign: true
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# Allow multiple comments in the beginning of a block separated with newline.
allow-separated-leading-comment: false
# Allow declarations (var) to be cuddled.
allow-cuddle-declarations: false
# Allow trailing comments in ending of blocks
allow-trailing-comment: false
# Force newlines in end of case at this limit (0 = never).
force-case-trailing-whitespace: 0
ahkui marked this conversation as resolved.
Show resolved Hide resolved

gohiei marked this conversation as resolved.
Show resolved Hide resolved
gci:
sections:
- standard
- blank
- prefix(cypt) # this module name
- blank
- default
# If `true`, make the section order the same as the order of `sections`.
# Default: false
custom-order: true

# See https://golangci-lint.run/usage/linters/
linters:
ahkui marked this conversation as resolved.
Show resolved Hide resolved
# Disable all linters.
# Default: false
disable-all: true
# Enable specific linter
# https://golangci-lint.run/usage/linters/#enabled-by-default
enable:
- bodyclose
- dogsled
- dupl
- errcheck
ahkui marked this conversation as resolved.
Show resolved Hide resolved
ahkui marked this conversation as resolved.
Show resolved Hide resolved
ahkui marked this conversation as resolved.
Show resolved Hide resolved
- exhaustive
- exportloopref
- forbidigo
- funlen
ahkui marked this conversation as resolved.
Show resolved Hide resolved
- gci
- gocognit
- goconst
- gocritic
- godox
- gofmt
- goimports
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- prealloc
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- whitespace
- wsl

issues:
gohiei marked this conversation as resolved.
Show resolved Hide resolved
exclude-rules:
- path: _test\.go
gohiei marked this conversation as resolved.
Show resolved Hide resolved
linters:
- gocognit
- errcheck
- dupl
- gosec
- funlen
- linters:
- lll
source: "^//go:generate "
ahkui marked this conversation as resolved.
Show resolved Hide resolved
exclude-use-default: false
max-issues-per-linter: 0
max-same-issues: 0
new: false
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"go.lintTool": "golangci-lint",
"go.lintFlags": [
"--fast"
ahkui marked this conversation as resolved.
Show resolved Hide resolved
],
}
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

BIN=cypt

all: check build
all: lint check build

build:
go build -o "${BIN}"

test:
go test ./...

lint-install:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

lint:
golangci-lint run -v

check:
go fmt ./...
go vet ./...
Expand Down
Loading