From cdcb9b983ad7fd48c1ad6b1e8183c8cee021a455 Mon Sep 17 00:00:00 2001 From: Ville Vesilehto Date: Fri, 30 Jun 2023 15:30:42 +0300 Subject: [PATCH 1/6] chore(ci): add golangci-lint --- .github/workflows/lint.yml | 22 +++++++++++ .golangci.yml | 78 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 .github/workflows/lint.yml create mode 100644 .golangci.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 000000000..b3bd8a73a --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,22 @@ +name: lint + +on: + pull_request: + +jobs: + run-linters: + name: Run linters + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 + + - name: Set up Go + uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1 + with: + go-version-file: 'go.mod' + + - name: Run linters + uses: golangci/golangci-lint-action@639cd343e1d3b897ff35927a75193d57cfcba299 # v3.6.0 + with: + version: latest diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 000000000..187199a61 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,78 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +linters: + disable-all: true + enable: + - gofmt + - govet + - unconvert + - staticcheck + - ineffassign + - unparam + - forbidigo + - gomodguard + - gosimple + +issues: + # Disable the default exclude list so that all excludes are explicitly + # defined in this file. + exclude-use-default: false + + exclude-rules: + # Temp Ignore SA9004: only the first constant in this group has an explicit type + # https://staticcheck.io/docs/checks#SA9004 + - linters: [staticcheck] + text: "SA9004:" + + - linters: [staticcheck] + text: 'SA1019: "io/ioutil" has been deprecated since Go 1.16' + + # An argument that always receives the same value is often not a problem. + - linters: [unparam] + text: "always receives" + + # Often functions will implement an interface that returns an error without + # needing to return an error. Sometimes the error return value is unnecessary + # but a linter can not tell the difference. + - linters: [unparam] + text: 'result \d+ \(error\) is always nil' + + # Allow unused parameters to start with an underscore. Arguments with a name + # of '_' are already ignored. + # Ignoring longer names that start with underscore allow for better + # self-documentation than a single underscore by itself. Underscore arguments + # should generally only be used when a function is implementing an interface. + - linters: [unparam] + text: "`_[^`]*` is unused" + + # Temp ignore some common unused parameters so that unparam can be added + # incrementally. + - linters: [unparam] + text: "`(t|resp|req|entMeta)` is unused" + +linters-settings: + govet: + check-shadowing: true + enable-all: true + disable: + - fieldalignment + - nilness + - shadow + - unusedwrite + forbidigo: + # Forbid the following identifiers (list of regexp). + forbid: + - '\bioutil\b(# Use io and os packages instead of ioutil)?' + - '\brequire\.New\b(# Use package-level functions with explicit TestingT)?' + - '\bassert\.New\b(# Use package-level functions with explicit TestingT)?' + # Exclude godoc examples from forbidigo checks. + # Default: true + exclude_godoc_examples: false + gofmt: + simplify: true + +run: + timeout: 10m + concurrency: 4 + skip-dirs-use-default: false From bb34a26dfbf1facfcc992a0518035156f527eaca Mon Sep 17 00:00:00 2001 From: Ville Vesilehto Date: Wed, 16 Aug 2023 08:07:44 +0300 Subject: [PATCH 2/6] add depguard Co-authored-by: Ronald --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 187199a61..d6cd91246 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -13,6 +13,7 @@ linters: - forbidigo - gomodguard - gosimple + - depguard issues: # Disable the default exclude list so that all excludes are explicitly From 3e80de014cb99499943932f7de541cb11b2149f5 Mon Sep 17 00:00:00 2001 From: Ville Vesilehto Date: Wed, 16 Aug 2023 08:10:35 +0300 Subject: [PATCH 3/6] remove exclude rules Co-authored-by: Ronald --- .golangci.yml | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index d6cd91246..17ea3793e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -20,37 +20,6 @@ issues: # defined in this file. exclude-use-default: false - exclude-rules: - # Temp Ignore SA9004: only the first constant in this group has an explicit type - # https://staticcheck.io/docs/checks#SA9004 - - linters: [staticcheck] - text: "SA9004:" - - - linters: [staticcheck] - text: 'SA1019: "io/ioutil" has been deprecated since Go 1.16' - - # An argument that always receives the same value is often not a problem. - - linters: [unparam] - text: "always receives" - - # Often functions will implement an interface that returns an error without - # needing to return an error. Sometimes the error return value is unnecessary - # but a linter can not tell the difference. - - linters: [unparam] - text: 'result \d+ \(error\) is always nil' - - # Allow unused parameters to start with an underscore. Arguments with a name - # of '_' are already ignored. - # Ignoring longer names that start with underscore allow for better - # self-documentation than a single underscore by itself. Underscore arguments - # should generally only be used when a function is implementing an interface. - - linters: [unparam] - text: "`_[^`]*` is unused" - - # Temp ignore some common unused parameters so that unparam can be added - # incrementally. - - linters: [unparam] - text: "`(t|resp|req|entMeta)` is unused" linters-settings: govet: From a69bc2e7c85e7e6d4bd840a42ccbe3af8f75d542 Mon Sep 17 00:00:00 2001 From: Ville Vesilehto Date: Wed, 16 Aug 2023 08:11:39 +0300 Subject: [PATCH 4/6] add gomodguard + depguard settings Co-authored-by: Ronald --- .golangci.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index 17ea3793e..9c96b0aef 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -41,6 +41,20 @@ linters-settings: exclude_godoc_examples: false gofmt: simplify: true + gomodguard: + blocked: + # List of blocked modules. + modules: + # Blocked module. + - github.com/hashicorp/go-msgpack: + recommendations: + - github.com/hashicorp/consul-net-rpc/go-msgpack + - github.com/golang/protobuf: + recommendations: + - google.golang.org/protobuf + depguard: + list-type: denylist + include-go-root: true run: timeout: 10m From 29ebe765d15d4489a874bc562fc5ea32f856aa7f Mon Sep 17 00:00:00 2001 From: Ville Vesilehto Date: Wed, 16 Aug 2023 08:14:42 +0300 Subject: [PATCH 5/6] Align setup-go GHA version Co-authored-by: Ronald --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index b3bd8a73a..c12545486 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -12,7 +12,7 @@ jobs: - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 - name: Set up Go - uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1 + uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0 with: go-version-file: 'go.mod' From df3080d3542d74d0d3fe1ad310650afcc39b326d Mon Sep 17 00:00:00 2001 From: Ville Vesilehto Date: Wed, 16 Aug 2023 08:44:56 +0300 Subject: [PATCH 6/6] fix depguard v2 config --- .golangci.yml | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 9c96b0aef..505a33161 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -53,8 +53,38 @@ linters-settings: recommendations: - google.golang.org/protobuf depguard: - list-type: denylist - include-go-root: true + rules: + main: + # List of file globs that will match this list of settings to compare against. + # Default: $all + files: + - $all + # List of allowed packages. + allow: + - $gostd + - github.com/BurntSushi/toml + - github.com/Masterminds/sprig/v3 + - github.com/davecgh/go-spew/spew + - github.com/hashicorp/consul-template + - github.com/hashicorp/consul/api + - github.com/hashicorp/consul/sdk/testutil + - github.com/hashicorp/go-gatedio + - github.com/hashicorp/go-hclog + - github.com/hashicorp/go-multierror + - github.com/hashicorp/go-rootcerts + - github.com/hashicorp/go-sockaddr/template + - github.com/hashicorp/go-syslog + - github.com/hashicorp/hcl + - github.com/hashicorp/logutils + - github.com/hashicorp/nomad/api + - github.com/hashicorp/vault/api + - github.com/imdario/mergo + - github.com/mitchellh/go-homedir + - github.com/mitchellh/hashstructure + - github.com/mitchellh/mapstructure + - github.com/pkg/errors + - github.com/stretchr/testify/assert + - github.com/stretchr/testify/require run: timeout: 10m