Skip to content

Commit 00a5463

Browse files
committed
Move linter config into a file, add new linters (#50)
This moves the linting configuration into a file and adds a few new linters that were released since the initial design. One of the nicest ones is goheader, which checks that the Apache license header is added to new Go files. Turns out, we had a slight typo that has been copy-pasted around, so I fixed that too.
1 parent 9833c53 commit 00a5463

File tree

11 files changed

+246
-97
lines changed

11 files changed

+246
-97
lines changed

.github/workflows/go-lint.yml

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ on:
77
description: 'The version of Go to install and use.'
88
type: 'string'
99
required: true
10+
golangci_url:
11+
description: 'The URL to a golangci file. This is only used if no file is found in the local directory.'
12+
type: 'string'
13+
default: 'https://raw.githubusercontent.com/abcxyz/pkg/main/.golangci.yml'
1014
directory:
1115
description: 'Directory in which Go files reside.'
1216
type: 'string'
@@ -60,57 +64,36 @@ jobs:
6064
with:
6165
go-version: '${{ inputs.go_version }}'
6266

67+
- name: 'Lint (download default configuration)'
68+
id: 'load-default-config'
69+
if: ${{ hashFiles('.golangci.yml', '.golangci.yaml') == '' }}
70+
run: |-
71+
# Create a unique output file outside of the checkout.
72+
GOLANGCI_YML="${RUNNER_TEMP}/${GITHUB_SHA:0:7}.golangci.yml"
73+
74+
# Download the file, passing in authentication to get a higher rate
75+
# limit: https://docs.github.com/en/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#rate-limits-for-requests-from-github-actions
76+
curl "${{ inputs.golangci_url }}" \
77+
--silent \
78+
--fail \
79+
--location \
80+
--header "Authorization: Token ${{ github.token }}" \
81+
--output "${GOLANGCI_YML}"
82+
83+
# Save the result to an output.
84+
echo "::notice::Wrote configuration file to ${GOLANGCI_YML}"
85+
echo "output-file=${GOLANGCI_YML}" >> "${GITHUB_OUTPUT}"
86+
6387
- name: 'Lint (default configuration)'
64-
if: ${{ hashFiles('.golangci.yml') == '' && hashFiles('.golangci.yaml') == '' }}
88+
if: ${{ hashFiles('.golangci.yml', '.golangci.yaml') == '' }}
6589
uses: 'golangci/golangci-lint-action@07db5389c99593f11ad7b44463c2d4233066a9b1' # ratchet:golangci/golangci-lint-action@v3
6690
with:
6791
args: |-
68-
--enable=${{ join(fromJson('[
69-
"asciicheck",
70-
"bidichk",
71-
"bodyclose",
72-
"containedctx",
73-
"depguard",
74-
"dogsled",
75-
"errcheck",
76-
"errchkjson",
77-
"errname",
78-
"errorlint",
79-
"exhaustive",
80-
"exportloopref",
81-
"forcetypeassert",
82-
"godot",
83-
"gofumpt",
84-
"goheader",
85-
"goimports",
86-
"gomodguard",
87-
"goprintffuncname",
88-
"gosec",
89-
"gosimple",
90-
"govet",
91-
"ifshort",
92-
"ineffassign",
93-
"makezero",
94-
"noctx",
95-
"nolintlint",
96-
"prealloc",
97-
"predeclared",
98-
"revive",
99-
"sqlclosecheck",
100-
"staticcheck",
101-
"stylecheck",
102-
"tenv",
103-
"thelper",
104-
"tparallel",
105-
"typecheck",
106-
"unconvert",
107-
"unused",
108-
"whitespace",
109-
]'), ',') }} --max-issues-per-linter=0 --max-same-issues=0 --timeout=5m
92+
--config "${{ steps.load-default-config.outputs.output-file }}"
11093
working-directory: '${{ inputs.directory }}'
11194

11295
- name: 'Lint (custom configuration)'
113-
if: ${{ hashFiles('.golangci.yml') != '' && hashFiles('.golangci.yaml') != '' }}
96+
if: ${{ hashFiles('.golangci.yml', '.golangci.yaml') != '' }}
11497
uses: 'golangci/golangci-lint-action@07db5389c99593f11ad7b44463c2d4233066a9b1' # ratchet:golangci/golangci-lint-action@v3
11598
with:
11699
working-directory: '${{ inputs.directory }}'

.golangci.yml

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Copyright 2023 The Authors (see AUTHORS file)
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
run:
16+
# default: '1m'
17+
timeout: '5m'
18+
19+
# default: []
20+
build-tags:
21+
- 'all'
22+
23+
# default: []
24+
skip-dirs:
25+
- 'internal/pb'
26+
- 'third_party'
27+
28+
# default: true
29+
skip-dirs-use-default: false
30+
31+
# default: ''
32+
modules-download-mode: 'readonly'
33+
34+
# default: false
35+
allow-parallel-runners: true
36+
37+
linters:
38+
enable:
39+
- 'asasalint'
40+
- 'asciicheck'
41+
- 'bidichk'
42+
- 'bodyclose'
43+
- 'containedctx'
44+
- 'depguard'
45+
- 'dupword'
46+
- 'durationcheck'
47+
- 'errcheck'
48+
- 'errchkjson'
49+
- 'errname'
50+
- 'errorlint'
51+
- 'execinquery'
52+
- 'exhaustive'
53+
- 'exportloopref'
54+
- 'forcetypeassert'
55+
- 'godot'
56+
- 'gofmt'
57+
- 'gofumpt'
58+
- 'goheader'
59+
- 'goimports'
60+
- 'gomodguard'
61+
- 'goprintffuncname'
62+
- 'gosec'
63+
- 'gosimple'
64+
- 'govet'
65+
- 'importas'
66+
- 'ineffassign'
67+
- 'loggercheck'
68+
- 'makezero'
69+
- 'misspell'
70+
- 'noctx'
71+
- 'nolintlint'
72+
- 'nosprintfhostport'
73+
- 'paralleltest'
74+
- 'prealloc'
75+
- 'predeclared'
76+
- 'revive'
77+
- 'rowserrcheck'
78+
- 'sqlclosecheck'
79+
- 'staticcheck'
80+
- 'stylecheck'
81+
- 'tenv'
82+
- 'thelper'
83+
- 'typecheck'
84+
- 'unconvert'
85+
- 'unused'
86+
- 'whitespace'
87+
- 'wrapcheck'
88+
89+
issues:
90+
# default: []
91+
exclude:
92+
- '^G102:' # gosec: we have to bind to all ifaces in Cloud Run services
93+
94+
# default: []
95+
exclude-rules:
96+
# Exclude test files from certain linters
97+
- path: '_test.go'
98+
linters:
99+
- 'wrapcheck'
100+
101+
# default: 50
102+
max-issues-per-linter: 0
103+
104+
# default: 3
105+
max-same-issues: 0
106+
107+
linters-settings:
108+
gofumpt:
109+
# default: false
110+
extra-rules: true
111+
112+
goheader:
113+
values:
114+
regexp:
115+
YEAR_AUTHOR: '\d{4} .*'
116+
INDENTATION: '[\t\f ]{2,}'
117+
# default: ""
118+
template: |-
119+
Copyright {{ YEAR_AUTHOR }}
120+
121+
Licensed under the Apache License, Version 2.0 (the "License");
122+
you may not use this file except in compliance with the License.
123+
You may obtain a copy of the License at
124+
125+
{{ INDENTATION }}http://www.apache.org/licenses/LICENSE-2.0
126+
127+
Unless required by applicable law or agreed to in writing, software
128+
distributed under the License is distributed on an "AS IS" BASIS,
129+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130+
See the License for the specific language governing permissions and
131+
limitations under the License.
132+
133+
wrapcheck:
134+
ignoreSigRegexps:
135+
- '.ErrorOrNil\('
136+
- 'retry\.RetryableError\('
137+
- 'status\.Error\('
138+
139+
severity:
140+
# default: ''
141+
default-severity: error

grpcutil/jwt_auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type JWTAuthenticationHandler struct {
4444
}
4545

4646
// NewJWTAuthenticationHandler returns a JWTAuthenticationHandler with a verifier initialized. Uses defaults
47-
// for JWT related fields that will retreive a user email when using IAM on GCP.
47+
// for JWT related fields that will retrieve a user email when using IAM on GCP.
4848
func NewJWTAuthenticationHandler(ctx context.Context, opts ...JWTAuthOption) (*JWTAuthenticationHandler, error) {
4949
j := &JWTAuthenticationHandler{
5050
JWTPrefix: "bearer ",
@@ -68,7 +68,7 @@ func NewJWTAuthenticationHandler(ctx context.Context, opts ...JWTAuthOption) (*J
6868
}
6969
verifier, err := jwtutil.NewVerifier(ctx, j.Endpoint)
7070
if err != nil {
71-
return nil, err
71+
return nil, fmt.Errorf("failed to create verifier: %w", err)
7272
}
7373
j.Verifier = verifier
7474
return j, nil

mysqltest/docker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func runContainer(conf *config, pool *dockertest.Pool) (*dockertest.Resource, er
110110
var extraMsg string
111111
switch {
112112
case strings.Contains(err.Error(), "no such file"):
113-
extraMsg = `. Please install docker:
113+
extraMsg = `. Please install docker:
114114
Instructions for Debian: https://docs.docker.com/engine/install/debian/
115115
Instructions for Mac: https://docs.docker.com/desktop/mac/install/`
116116
case strings.Contains(err.Error(), "permission denied"):

mysqltest/mysqltest_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2022 The Authors (see AUTHORS file)
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package mysqltest
216

317
import (
@@ -48,6 +62,8 @@ func TestMustStart_NonexistentVersion(t *testing.T) {
4862
}
4963

5064
func TestBuildConfig(t *testing.T) {
65+
t.Parallel()
66+
5167
logger := &testLogger{t}
5268
conf := buildConfig(
5369
WithKillAfterSeconds(1),

terraform/modules/workload-identity-federation/main.tf

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
/**
2-
* Copyright 2022 Google LLC
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
1+
# Copyright 2022 The Authors (see AUTHORS file)
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
1615
resource "google_project_service" "serviceusage" {
1716
project = var.project_id
1817
service = "serviceusage.googleapis.com"

terraform/modules/workload-identity-federation/outputs.tf

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
/**
2-
* Copyright 2022 Google LLC
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
1+
# Copyright 2022 The Authors (see AUTHORS file)
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
1615
output "pool_name" {
1716
value = google_iam_workload_identity_pool.pool.name
1817
}

terraform/modules/workload-identity-federation/variables.tf

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
/**
2-
* Copyright 2022 Google LLC
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
1+
# Copyright 2022 The Authors (see AUTHORS file)
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
1614

1715
variable "project_id" {
1816
type = string

0 commit comments

Comments
 (0)