Skip to content

Commit

Permalink
Add golangci-lint
Browse files Browse the repository at this point in the history
- Add golangci.yaml config file
- Add Makefile lint target to golangci-lint
- Add GitHub action

Related to issue k8snetworkplumbingwg#288

Signed-off-by: Fred Rolland <frolland@nvidia.com>
  • Loading branch information
rollandf committed May 11, 2022
1 parent ca9bc8a commit ecaf1c9
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,19 @@ jobs:

- name: test bindata/scripts
run: make test-bindata-scripts

golangci:
name: Golangci-lint
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.17
uses: actions/setup-go@v2
with:
go-version: 1.17.8
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.37
119 changes: 119 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Tested with golangci-lint ver. 1.37
run:
timeout: 3m
skip-dirs:
- vendor/
- .github/
- deployment/
- doc/
- bindata/
linters-settings:
depguard:
list-type: blacklist
packages:
# logging is allowed only by logutils.Log, logrus
# is allowed to use only in logutils package
- github.com/sirupsen/logrus
packages-with-error-message:
- github.com/sirupsen/logrus: "logging is allowed only by logutils.Log"
dupl:
threshold: 100
funlen:
lines: 100
statements: 50
goconst:
min-len: 2
min-occurrences: 2
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- dupImport # https://github.com/go-critic/go-critic/issues/845
- ifElseChain
- octalLiteral
- whyNoLint
- wrapperFunc
- unnamedResult
settings:
hugeParam:
sizeThreshold: 512
rangeValCopy:
sizeThreshold: 512
gocyclo:
min-complexity: 15
goimports:
local-prefixes: github.com/k8snetworkplumbingwg/sriov-network-device-plugin
gomnd:
settings:
mnd:
# don't include the "operation" and "assign"
checks: argument,case,condition,return
lll:
line-length: 140
misspell:
locale: US
prealloc:
# 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

linters:
# please, do not use `enable-all`: it's deprecated and will be removed soon.
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
disable-all: true
enable:
- bodyclose
# TODO fix issues- deadcode
- depguard
- dogsled
# TODO fix issues- dupl
# TODO fix issues- errcheck
- exportloopref
- exhaustive
# TODO fix issues- funlen
#- gochecknoinits
# TODO fix issues- goconst
# TODO fix issues- gocritic
# TODO fix issues- gocyclo
- gofmt
# TODO fix issues- goimports
# TODO fix issues- gomnd
- goprintffuncname
# TODO fix issues- gosec
# TODO fix issues- gosimple
#- govet
# TODO fix issues- ineffassign
# TODO fix issues- lll
- misspell
# TODO fix issues- nakedret
# TODO fix issues- prealloc
- rowserrcheck
#- scopelint
# TODO fix issues- staticcheck
- structcheck
# TODO fix issues- stylecheck
- typecheck
# TODO fix issues- unconvert
# TODO fix issues- unparam
- unused
# TODO fix issues- varcheck
# TODO fix issues- whitespace

issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
- path: _test\.go
linters:
- gomnd
- gosec
- dupl
- lll
- stylecheck
- goconst

20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ else
GOBIN=$(shell go env GOBIN)
endif

GOLANGCI_LINT = $(GOBIN)/golangci-lint
# golangci-lint version should be updated periodically
# we keep it fixed to avoid it from unexpectedly failing on the project
# in case of a version bump
GOLANGCI_LINT_VER = v1.37.0
TIMEOUT = 15
Q = $(if $(filter 1,$V),,@)


.PHONY: all build clean gendeepcopy test test-e2e test-e2e-k8s run image fmt sync-manifests test-e2e-conformance manifests update-codegen

all: generate vet build plugins
Expand Down Expand Up @@ -235,3 +244,14 @@ undeploy-k8s: undeploy
deps-update:
go mod tidy && \
go mod vendor

$(GOLANGCI_LINT): $(info building golangci-lint...)
$Q curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) $(GOLANGCI_LINT_VER)

.PHONY: lint
lint: | $(GOLANGCI_LINT) ; $(info running golangci-lint...) @ ## Run golangci-lint
$Q mkdir -p $(CURPATH)/test-lint
$Q cd $(CURPATH) && ret=0 && \
test -z "$$($(GOLANGCI_LINT) run | tee $(CURPATH)/test-lint/lint.out)" || ret=1 ; \
cat $(CURPATH)/test-lint/lint.out ; rm -rf $(CURPATH)/test-lint ; \
exit $$ret

0 comments on commit ecaf1c9

Please sign in to comment.