From d15a960f93bdb557c577bb681da10ced55e18608 Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Fri, 7 May 2021 14:20:42 +0000 Subject: [PATCH] chore: add golangci-lint configuration Adapt it from Talos repo. Signed-off-by: Alexey Palazhchenko --- .dockerignore | 1 + .golangci.yml | 189 ++++++++++++++++++ Dockerfile | 8 +- .../api/v1alpha2/metalcluster_conversion.go | 2 +- .../api/v1alpha2/metalmachine_conversion.go | 2 +- .../metalmachinetemplate_conversion.go | 2 +- app/cluster-api-provider-sidero/main.go | 2 +- .../api/v1alpha1/server_types_test.go | 2 +- .../controllers/suite_test.go | 1 - .../internal/ipxe/ipxe_server.go | 2 +- .../internal/power/api/api.go | 4 +- app/metal-controller-manager/main.go | 2 +- sfyra/cmd/sfyra/cmd/test_integration.go | 4 +- sfyra/pkg/tests/server_class.go | 6 +- 14 files changed, 209 insertions(+), 18 deletions(-) create mode 100644 .golangci.yml diff --git a/.dockerignore b/.dockerignore index c7934b448..7da12e5eb 100644 --- a/.dockerignore +++ b/.dockerignore @@ -8,6 +8,7 @@ !pkg !sfyra !templates +!.golangci.yml !go.mod !go.sum !*.go diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 000000000..44a072545 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,189 @@ +# This file contains all available configuration options +# with their default values. + +# options for analysis running +run: + # default concurrency is a available CPU number + # concurrency: 4 + + # timeout for analysis, e.g. 30s, 5m, default is 1m + deadline: 10m + + # exit code when at least one issue was found, default is 1 + issues-exit-code: 1 + + # include test files or not, default is true + tests: true + + # which files to skip: they will be analyzed, but issues from them + # won't be reported. Default value is empty list, but there is + # no need to include all autogenerated files, we confidently recognize + # autogenerated files. If it's not please let us know. + skip-files: + - .*\\.pb\\.go$ + + # list of build tags, all linters use it. Default is empty list. + build-tags: [] + +# output configuration options +output: + # colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number" + format: line-number + +# all available settings of specific linters +linters-settings: + errcheck: + # report about not checking of errors in type assetions: `a := b.(MyStruct)`; + # default is false: such cases aren't reported by default. + check-type-assertions: true + + # report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`; + # default is false: such cases aren't reported by default. + check-blank: true + govet: + # report about shadowed variables + check-shadowing: true + 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 + gocyclo: + # minimal code complexity to report, 30 by default (but we recommend 10-20) + min-complexity: 10 + 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: {} + 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 + 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: 200 + # 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: + # call graph construction algorithm (cha, rta). In general, use cha for libraries, + # and rta for programs with main packages. Default is cha. + algo: cha + + # 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 + nolintlint: + allow-unused: false + allow-leading-space: false + allow-no-explanation: [] + require-explanation: false + require-specific: 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. + + # 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 + gci: + local-prefixes: github.com/talos-systems/sidero + cyclop: + # the maximal code complexity to report + max-complexity: 20 + +linters: + enable-all: true + disable: + # FIXME those linters should be enabled ASAP + - cyclop + - errcheck + - forcetypeassert + - gci + - gocyclo + - godot + - golint + - govet + - lll + - misspell + - nlreturn + - predeclared + - revive + - staticcheck + - stylecheck + + - gas + - typecheck + - gochecknoglobals + - gochecknoinits + - ifshort + - forbidigo + - funlen + - godox + - gocognit + - gomnd + - goerr113 + - nestif + - exhaustivestruct + - errorlint + - paralleltest + - thelper + - wrapcheck + # abandoned linters for which golangci shows the warning that the repo is archived by the owner + - interfacer + - maligned + disable-all: false + fast: false + +issues: + # List of regexps of issue texts to exclude, empty list by default. + # But independently from this option we use default exclude patterns, + # it can be disabled by `exclude-use-default: false`. To list all + # excluded by default patterns execute `golangci-lint run --help` + exclude: + - ^ST1000 # ST1000: package comment should be of the form "Package services ..." (stylecheck) + + exclude-rules: [] + + # Independently from option `exclude` we use default exclude patterns, + # it can be disabled by this option. To list all + # excluded by default patterns execute `golangci-lint run --help`. + # Default value for this option is true. + exclude-use-default: false + + # Maximum issues count per one linter. Set to 0 to disable. Default is 50. + max-per-linter: 0 + + # Maximum count of issues with the same text. Set to 0 to disable. Default is 3. + max-same: 0 + + # Show only new issues: if there are unstaged changes or untracked files, + # only those changes are analyzed, else only changes in HEAD~ are analyzed. + # It's a super-useful option for integration of golangci-lint into existing + # large codebase. It's not practical to fix all existing issues at the moment + # of integration: much better don't allow issues in new code. + # Default is false. + new: false diff --git a/Dockerfile b/Dockerfile index de2551c58..de4ed716a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,7 +38,7 @@ ENV GOMODCACHE /.cache/mod RUN --mount=type=cache,target=/.cache go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.5.0 RUN --mount=type=cache,target=/.cache go install k8s.io/code-generator/cmd/conversion-gen@v0.21.0 RUN --mount=type=cache,target=/.cache go install mvdan.cc/gofumpt/gofumports@v0.1.1 -RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b /usr/local/bin v1.28.0 +RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b /toolchain/bin v1.38.0 WORKDIR /src COPY ./go.mod ./ COPY ./go.sum ./ @@ -206,9 +206,10 @@ RUN --mount=type=cache,target=/.cache --mount=type=cache,target=/root/.cache/go- # The lint target performs linting on the source code. # FROM base AS lint-go +COPY .golangci.yml . ENV GOGC=50 ENV GOLANGCI_LINT_CACHE /.cache/lint -RUN --mount=type=cache,target=/.cache /usr/local/bin/golangci-lint run --enable-all --disable gochecknoglobals,gochecknoinits,lll,goerr113,funlen,nestif,maligned,gomnd,gocognit,gocyclo +RUN --mount=type=cache,target=/.cache golangci-lint run --config .golangci.yml ARG MODULE RUN --mount=type=cache,target=/.cache FILES="$(gofumports -l -local ${MODULE} .)" && test -z "${FILES}" || (echo -e "Source code is not formatted with 'gofumports -w -local ${MODULE} .':\n${FILES}"; exit 1) # @@ -243,9 +244,10 @@ RUN --mount=type=cache,target=/.cache go list -mod=readonly all >/dev/null RUN --mount=type=cache,target=/.cache ! go mod tidy -v 2>&1 | grep . FROM sfyra-base AS lint-sfyra +COPY .golangci.yml . ENV GOGC=50 ENV GOLANGCI_LINT_CACHE /.cache/lint -RUN --mount=type=cache,target=/.cache /usr/local/bin/golangci-lint run --enable-all --disable gochecknoglobals,gochecknoinits,lll,goerr113,funlen,nestif,maligned,gomnd,gocognit,gocyclo,godox +RUN --mount=type=cache,target=/.cache golangci-lint run --config .golangci.yml ARG MODULE RUN --mount=type=cache,target=/.cache FILES="$(gofumports -l -local ${MODULE} .)" && test -z "${FILES}" || (echo -e "Source code is not formatted with 'gofumports -w -local ${MODULE} .':\n${FILES}"; exit 1) diff --git a/app/cluster-api-provider-sidero/api/v1alpha2/metalcluster_conversion.go b/app/cluster-api-provider-sidero/api/v1alpha2/metalcluster_conversion.go index 28999e15e..823c4df33 100644 --- a/app/cluster-api-provider-sidero/api/v1alpha2/metalcluster_conversion.go +++ b/app/cluster-api-provider-sidero/api/v1alpha2/metalcluster_conversion.go @@ -2,7 +2,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. -// nolint: golint,stylecheck +//nolint:golint,stylecheck package v1alpha2 import ( diff --git a/app/cluster-api-provider-sidero/api/v1alpha2/metalmachine_conversion.go b/app/cluster-api-provider-sidero/api/v1alpha2/metalmachine_conversion.go index 35b2edd9c..6d206a56f 100644 --- a/app/cluster-api-provider-sidero/api/v1alpha2/metalmachine_conversion.go +++ b/app/cluster-api-provider-sidero/api/v1alpha2/metalmachine_conversion.go @@ -2,7 +2,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. -// nolint: golint,stylecheck +//nolint:golint,stylecheck package v1alpha2 import ( diff --git a/app/cluster-api-provider-sidero/api/v1alpha2/metalmachinetemplate_conversion.go b/app/cluster-api-provider-sidero/api/v1alpha2/metalmachinetemplate_conversion.go index c05414a05..7d3aff191 100644 --- a/app/cluster-api-provider-sidero/api/v1alpha2/metalmachinetemplate_conversion.go +++ b/app/cluster-api-provider-sidero/api/v1alpha2/metalmachinetemplate_conversion.go @@ -2,7 +2,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. -// nolint: golint,stylecheck +//nolint:golint,stylecheck package v1alpha2 import ( diff --git a/app/cluster-api-provider-sidero/main.go b/app/cluster-api-provider-sidero/main.go index 05b0ba9ee..be60181e6 100644 --- a/app/cluster-api-provider-sidero/main.go +++ b/app/cluster-api-provider-sidero/main.go @@ -31,7 +31,7 @@ var ( setupLog = ctrl.Log.WithName("setup") ) -// nolint: wsl +//nolint:wsl func init() { _ = clientgoscheme.AddToScheme(scheme) _ = capiv1.AddToScheme(scheme) diff --git a/app/metal-controller-manager/api/v1alpha1/server_types_test.go b/app/metal-controller-manager/api/v1alpha1/server_types_test.go index 65fdbb739..55c5fa9dc 100644 --- a/app/metal-controller-manager/api/v1alpha1/server_types_test.go +++ b/app/metal-controller-manager/api/v1alpha1/server_types_test.go @@ -2,7 +2,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. -// nolint: scopelint +//nolint:scopelint package v1alpha1_test import ( diff --git a/app/metal-controller-manager/controllers/suite_test.go b/app/metal-controller-manager/controllers/suite_test.go index 0bf1c2125..eaa0ecf39 100644 --- a/app/metal-controller-manager/controllers/suite_test.go +++ b/app/metal-controller-manager/controllers/suite_test.go @@ -10,7 +10,6 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client" diff --git a/app/metal-controller-manager/internal/ipxe/ipxe_server.go b/app/metal-controller-manager/internal/ipxe/ipxe_server.go index feffaf8e9..bee83198f 100644 --- a/app/metal-controller-manager/internal/ipxe/ipxe_server.go +++ b/app/metal-controller-manager/internal/ipxe/ipxe_server.go @@ -73,7 +73,7 @@ func bootFileHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, bootFile) } -//nolint: unparam +//nolint:unparam func bootFromDiskHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, ipxeBootFromDisk) } diff --git a/app/metal-controller-manager/internal/power/api/api.go b/app/metal-controller-manager/internal/power/api/api.go index ded80b0bd..12954c5eb 100644 --- a/app/metal-controller-manager/internal/power/api/api.go +++ b/app/metal-controller-manager/internal/power/api/api.go @@ -32,7 +32,7 @@ func NewClient(spec metalv1alpha1.ManagementAPI) (*Client, error) { func (c *Client) postRequest(path string) error { failureMode := DefaultDice.Roll() - switch failureMode { //nolint: exhaustive + switch failureMode { //nolint:exhaustive case ExplicitFailure: return fmt.Errorf("simulated failure from the power management") case SilentFailure: @@ -91,7 +91,7 @@ func (c *Client) SetPXE() error { func (c *Client) IsPoweredOn() (bool, error) { failureMode := DefaultDice.Roll() - switch failureMode { //nolint: exhaustive + switch failureMode { //nolint:exhaustive case ExplicitFailure: return false, fmt.Errorf("simulated failure from the power management") case SilentFailure: diff --git a/app/metal-controller-manager/main.go b/app/metal-controller-manager/main.go index c58b7ee51..c13ce70bb 100644 --- a/app/metal-controller-manager/main.go +++ b/app/metal-controller-manager/main.go @@ -43,7 +43,7 @@ var ( setupLog = ctrl.Log.WithName("setup") ) -// nolint: wsl +//nolint:wsl func init() { _ = clientgoscheme.AddToScheme(scheme) diff --git a/sfyra/cmd/sfyra/cmd/test_integration.go b/sfyra/cmd/sfyra/cmd/test_integration.go index 6a5eb7934..4f4c5f4ae 100644 --- a/sfyra/cmd/sfyra/cmd/test_integration.go +++ b/sfyra/cmd/sfyra/cmd/test_integration.go @@ -48,7 +48,7 @@ var testIntegrationCmd = &cobra.Command{ } if !options.SkipTeardown { - defer bootstrapCluster.TearDown(ctx) //nolint: errcheck + defer bootstrapCluster.TearDown(ctx) //nolint:errcheck } if err = bootstrapCluster.Setup(ctx); err != nil { @@ -73,7 +73,7 @@ var testIntegrationCmd = &cobra.Command{ } if !options.SkipTeardown { - defer managementSet.TearDown(ctx) //nolint: errcheck + defer managementSet.TearDown(ctx) //nolint:errcheck } if err = managementSet.Setup(ctx); err != nil { diff --git a/sfyra/pkg/tests/server_class.go b/sfyra/pkg/tests/server_class.go index ff5e8bdea..1af921aa0 100644 --- a/sfyra/pkg/tests/server_class.go +++ b/sfyra/pkg/tests/server_class.go @@ -121,7 +121,7 @@ func TestServerClassPatch(ctx context.Context, metalClient client.Client, cluste dummyServer, err := createDummyServer(ctx, metalClient, "dummyserver-0", dummySpec) require.NoError(t, err) - //nolint: errcheck + //nolint:errcheck defer metalClient.Delete(ctx, &dummyServer) installConfig := talosconfig.InstallConfig{ @@ -152,7 +152,7 @@ func TestServerClassPatch(ctx context.Context, metalClient client.Client, cluste dummyClass, err := createServerClass(ctx, metalClient, "dummyservers", classSpec) require.NoError(t, err) - //nolint: errcheck + //nolint:errcheck defer metalClient.Delete(ctx, &dummyClass) // Create "cluster" using serverclass above @@ -214,7 +214,7 @@ func TestServerClassPatch(ctx context.Context, metalClient client.Client, cluste require.NoError(t, err) if obj.GroupVersionKind().Kind == "Cluster" { - //nolint: errcheck + //nolint:errcheck defer dr.Delete(ctx, obj.GetName(), metav1.DeleteOptions{}) } }