From 5fb1ee8b62e38b26beba4011ba8c1cfdf3e61b17 Mon Sep 17 00:00:00 2001 From: ahkui Date: Wed, 12 Jul 2023 10:08:51 +0800 Subject: [PATCH 1/4] chore: add golangci-lint Signed-off-by: ahkui --- .github/workflows/golangci-lint.yml | 25 ++++ .golangci.yml | 207 ++++++++++++++++++++++++++++ .vscode/settings.json | 6 + 3 files changed, 238 insertions(+) create mode 100644 .github/workflows/golangci-lint.yml create mode 100644 .golangci.yml create mode 100644 .vscode/settings.json diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml new file mode 100644 index 0000000..ba8dc39 --- /dev/null +++ b/.github/workflows/golangci-lint.yml @@ -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" + 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 diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..a8997d7 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,207 @@ +output: + # 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. + gosec: + exclude: + - "G104" + + funlen: + lines: 60 + 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 + golint: + # minimal confidence for issues, default is 0.8 + min-confidence: 0.8 + gofmt: + # simplify code: gofmt with `-s` option, true by default + simplify: true + goimports: + # put imports beginning with prefix after 3rd-party packages; + # it's a comma-separated list of prefixes + # local-prefixes: github.com/org/project + gocyclo: + # minimal code complexity to report, 30 by default (but we recommend 10-20) + min-complexity: 30 + gocognit: + # minimal code complexity to report, 30 by default (but we recommend 10-20) + min-complexity: 20 + maligned: + # print struct with more effective memory layout or not, false by default + suggest-new: true + dupl: + # tokens count to trigger issue, 150 by default + threshold: 100 + goconst: + # minimal length of string constant, 3 by default + min-len: 3 + # minimal occurrences count to trigger, 3 by default + min-occurrences: 3 + # depguard: + # list-type: blacklist + # include-go-root: false + # packages: + # - github.com/sirupsen/logrus + # packages-with-error-messages: + # # specify an error message to output when a blacklisted package is used + # github.com/sirupsen/logrus: "logging is allowed only by logutils.Log" + misspell: + # 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 + lll: + # max line length, lines longer will be reported. Default is 120. + # '\t' is counted as 1 character by default, and can be changed with the tab-width option + line-length: 120 + # tab width in spaces. Default to 1. + tab-width: 1 + unused: + # 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: + # 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: + # 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 + prealloc: + # 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: + # 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 + + settings: # settings passed to gocritic + captLocal: # must be valid enabled check name + paramsOnly: true + # rangeValCopy: + # sizeThreshold: 32 + godox: + # 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: + # checks assignments with too many blank identifiers; default is 2 + max-blank-identifiers: 2 + + whitespace: + multi-if: false # Enforces newlines (or comments) after every multi-line if statement + multi-func: false # Enforces newlines (or comments) after every multi-line function signature + wsl: + # 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 + # 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 + +linters: + disable-all: true + enable: + - bodyclose + # - depguard + - dogsled + - dupl + - errcheck + - funlen + - gocognit + - goconst + - gocritic + - godox + - gofmt + - goimports + - golint + - gosec + - gosimple + - govet + - ineffassign + - interfacer + - misspell + - nakedret + - scopelint + - staticcheck + - structcheck + - stylecheck + - typecheck + - unconvert + - unparam + - unused + - whitespace + +issues: + exclude-rules: + - path: _test\.go + linters: + - gocyclo + - errcheck + - dupl + - gosec + - funlen + - linters: + - lll + source: "^//go:generate " + exclude-use-default: false + max-issues-per-linter: 0 + max-same-issues: 0 + new: false diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..95db8ef --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "go.lintTool": "golangci-lint", + "go.lintFlags": [ + "--fast" + ], +} From 60187d06d6b005b7e65839e382825c12795335fb Mon Sep 17 00:00:00 2001 From: ahkui Date: Wed, 12 Jul 2023 10:08:51 +0800 Subject: [PATCH 2/4] chore: Apply suggestions from code review Co-authored-by: TCCinCY <41672399+TCCinCY@users.noreply.github.com> Signed-off-by: ahkui --- .golangci.yml | 84 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 35 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index a8997d7..2823e51 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -9,9 +9,10 @@ linters-settings: check-blank: false # Disable error checking, as errorcheck detects more errors and is more configurable. + # See https://github.com/securego/gosec#available-rules gosec: exclude: - - "G104" + - G104 funlen: lines: 60 @@ -29,41 +30,31 @@ linters-settings: - (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 - golint: - # minimal confidence for issues, default is 0.8 - min-confidence: 0.8 + gofmt: # simplify code: gofmt with `-s` option, true by default simplify: true + goimports: # put imports beginning with prefix after 3rd-party packages; # it's a comma-separated list of prefixes # local-prefixes: github.com/org/project - gocyclo: - # minimal code complexity to report, 30 by default (but we recommend 10-20) - min-complexity: 30 + gocognit: # minimal code complexity to report, 30 by default (but we recommend 10-20) min-complexity: 20 - maligned: - # print struct with more effective memory layout or not, false by default - suggest-new: true + + # See https://github.com/mibk/dupl dupl: # tokens count to trigger issue, 150 by default threshold: 100 + goconst: # minimal length of string constant, 3 by default min-len: 3 # minimal occurrences count to trigger, 3 by default min-occurrences: 3 - # depguard: - # list-type: blacklist - # include-go-root: false - # packages: - # - github.com/sirupsen/logrus - # packages-with-error-messages: - # # specify an error message to output when a blacklisted package is used - # github.com/sirupsen/logrus: "logging is allowed only by logutils.Log" + misspell: # Correct spellings using locale preferences for US or UK. # Default is to use a neutral variety of English. @@ -71,27 +62,28 @@ linters-settings: locale: US ignore-words: - GitLab - lll: - # max line length, lines longer will be reported. Default is 120. - # '\t' is counted as 1 character by default, and can be changed with the tab-width option - line-length: 120 - # tab width in spaces. Default to 1. - tab-width: 1 + unused: # 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: # 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: # 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: # XXX: we don't recommend using this linter before doing performance profiling. # For most programs usage of prealloc will be a premature optimization. @@ -101,6 +93,7 @@ linters-settings: 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: # Which checks should be enabled; can't be combined with 'disabled-checks'; # See https://go-critic.github.io/overview#checks-overview @@ -115,14 +108,24 @@ linters-settings: # 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 + enabled-tags: + - performance + disabled-tags: + - experimental - settings: # settings passed to gocritic + # 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: # 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 @@ -133,13 +136,15 @@ linters-settings: - NOTE - OPTIMIZE # marks code that should be optimized before merging - HACK # marks hack-arounds that should be removed before merging + dogsled: # checks assignments with too many blank identifiers; default is 2 max-blank-identifiers: 2 whitespace: - multi-if: false # Enforces newlines (or comments) after every multi-line if statement - multi-func: false # Enforces newlines (or comments) after every multi-line function signature + 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: # If true append is only allowed to be cuddled if appending value is # matching variables, fields or types on line above. Default is true. @@ -149,6 +154,8 @@ linters-settings: allow-assign-and-call: true # Allow multiline assignments to be cuddled. Default is true. allow-multiline-assign: true + # 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 @@ -156,44 +163,51 @@ linters-settings: # Force newlines in end of case at this limit (0 = never). force-case-trailing-whitespace: 0 +# See https://golangci-lint.run/usage/linters/ linters: + # Disable all linters. + # Default: false disable-all: true + # Enable specific linter + # https://golangci-lint.run/usage/linters/#enabled-by-default enable: - bodyclose - # - depguard - dogsled - dupl - errcheck + - exhaustive + - exportloopref + - forbidigo - funlen + - gci - gocognit - goconst - gocritic - godox - gofmt - goimports - - golint - gosec - gosimple - govet - ineffassign - - interfacer - misspell - nakedret - - scopelint + - prealloc + - revive - staticcheck - - structcheck - stylecheck - typecheck - unconvert - unparam - unused - whitespace + - wsl issues: exclude-rules: - path: _test\.go linters: - - gocyclo + - gocognit - errcheck - dupl - gosec From 1c00ee46bae5e520ff87ae66f5db076a746d05d6 Mon Sep 17 00:00:00 2001 From: ahkui Date: Wed, 12 Jul 2023 14:54:15 +0800 Subject: [PATCH 3/4] chore: add lint task to makefile Signed-off-by: ahkui --- Makefile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1f885b6..4e564e3 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ BIN=cypt -all: check build +all: lint check build build: go build -o "${BIN}" @@ -10,6 +10,12 @@ build: 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 ./... From 4036afaaa916e95fece769d1e00a79c504eb2f4e Mon Sep 17 00:00:00 2001 From: ahkui Date: Wed, 12 Jul 2023 14:54:25 +0800 Subject: [PATCH 4/4] chore: add gci config Signed-off-by: ahkui --- .golangci.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index 2823e51..c68aa26 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -163,6 +163,17 @@ linters-settings: # Force newlines in end of case at this limit (0 = never). force-case-trailing-whitespace: 0 + 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: # Disable all linters.