Skip to content

Commit 1338cfa

Browse files
committed
make+scripts: add lint check for Golang version
To make sure we use the same Golang version everywhere, we add a new linter step with scripts copied from lnd.
1 parent 4485eae commit 1338cfa

File tree

3 files changed

+155
-1
lines changed

3 files changed

+155
-1
lines changed

Makefile

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ COMMIT := $(shell git describe --abbrev=40 --dirty --tags)
1818
COMMIT_HASH := $(shell git rev-parse HEAD)
1919
PUBLIC_URL :=
2020

21+
# GO_VERSION is the Go version used for the release build, docker files, and
22+
# GitHub Actions. This is the reference version for the project. All other Go
23+
# versions are checked against this version.
24+
GO_VERSION = 1.22.6
25+
2126
LOOP_COMMIT := $(shell cat go.mod | \
2227
grep $(LOOP_PKG) | \
2328
head -n1 | \
@@ -238,7 +243,17 @@ fmt: $(GOIMPORTS_BIN)
238243
@$(call print, "Formatting source.")
239244
gofmt -l -w -s $(GOFILES_NOVENDOR)
240245

241-
lint: docker-tools
246+
check-go-version-yaml:
247+
@$(call print, "Checking for target Go version (v$(GO_VERSION)) in YAML files (*.yaml, *.yml)")
248+
./scripts/check-go-version-yaml.sh $(GO_VERSION)
249+
250+
check-go-version-dockerfile:
251+
@$(call print, "Checking for target Go version (v$(GO_VERSION)) in Dockerfile files (*Dockerfile)")
252+
./scripts/check-go-version-dockerfile.sh $(GO_VERSION)
253+
254+
check-go-version: check-go-version-dockerfile check-go-version-yaml
255+
256+
lint: check-go-version docker-tools
242257
@$(call print, "Linting source.")
243258
$(DOCKER_TOOLS) golangci-lint run -v $(LINT_WORKERS)
244259

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/bash
2+
3+
# Function to check if the Dockerfile contains only the specified Go version.
4+
check_go_version() {
5+
local dockerfile="$1"
6+
local required_go_version="$2"
7+
8+
# Use grep to find lines with 'FROM golang:'
9+
local go_lines=$(grep -i '^FROM golang:' "$dockerfile")
10+
11+
# Check if all lines have the required Go version.
12+
if [ -z "$go_lines" ]; then
13+
# No Go version found in the file. Skip the check.
14+
return
15+
elif echo "$go_lines" | grep -q -v "$required_go_version"; then
16+
echo "$go_lines"
17+
echo "Error: $dockerfile does not use Go version $required_go_version exclusively."
18+
exit 1
19+
else
20+
echo "$dockerfile is using Go version $required_go_version."
21+
fi
22+
}
23+
24+
# Check if the target Go version argument is provided.
25+
if [ $# -eq 0 ]; then
26+
echo "Usage: $0 <target_go_version>"
27+
exit 1
28+
fi
29+
30+
target_go_version="$1"
31+
32+
# File paths to be excluded from the check.
33+
exception_list=(
34+
# Exclude the tools Dockerfile as otherwise the linter may need to be
35+
# considered every time the Go version is updated.
36+
"./tools/Dockerfile"
37+
)
38+
39+
# is_exception checks if a file is in the exception list.
40+
is_exception() {
41+
local file="$1"
42+
for exception in "${exception_list[@]}"; do
43+
if [ "$file" == "$exception" ]; then
44+
return 0
45+
fi
46+
done
47+
return 1
48+
}
49+
50+
# Search for Dockerfiles in the current directory and its subdirectories.
51+
dockerfiles=$(find . -type f -name "*.Dockerfile" -o -name "Dockerfile")
52+
53+
# Check each Dockerfile
54+
for file in $dockerfiles; do
55+
# Skip the file if it is in the exception list.
56+
if is_exception "$file"; then
57+
echo "Skipping $file"
58+
continue
59+
fi
60+
61+
check_go_version "$file" "$target_go_version"
62+
done
63+
64+
echo "All Dockerfiles pass the Go version check for Go version $target_go_version."

scripts/check-go-version-yaml.sh

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
3+
# Function to check if the YAML file contains the specified Go version after
4+
# field 'go:'.
5+
check_go_version_yaml() {
6+
local yamlfile="$1"
7+
local required_go_version="$2"
8+
9+
# Use grep to find lines with 'go:'. The grep exist status is ignored.
10+
local go_lines=$(grep -i '^\s*go:\s*"[0-9]\+\.[0-9]\+\(\.[0-9]\+\)\?"' "$yamlfile" || true)
11+
12+
# Check if any lines specify the Go version.
13+
if [ -n "$go_lines" ]; then
14+
# Extract the Go version from the file's lines. Example matching strings:
15+
# go: "1.21.0"
16+
local extracted_go_version=$(echo "$go_lines" | sed -n 's/.*go: "\([^"]*\)".*/\1/p')
17+
18+
# Check if the extracted Go version matches the required version.
19+
if [ "$extracted_go_version" != "$required_go_version" ]; then
20+
echo "Error finding pattern 'go:': $yamlfile specifies Go version '$extracted_go_version', but required version is '$required_go_version'."
21+
exit 1
22+
else
23+
echo "$yamlfile specifies Go version $required_go_version."
24+
fi
25+
fi
26+
}
27+
28+
# Function to check if the YAML file contains the specified Go version after
29+
# environment variable 'GO_VERSION:'.
30+
check_go_version_env_variable() {
31+
local yamlfile="$1"
32+
local required_go_version="$2"
33+
34+
# Use grep to find lines with 'GO_VERSION:'. The grep exist status is
35+
# ignored.
36+
local go_lines=$(grep -i 'GO_VERSION:' "$yamlfile" || true)
37+
38+
# Check if any lines specify the Go version.
39+
if [ -n "$go_lines" ]; then
40+
# Extract the Go version from the file's lines. Example matching strings:
41+
# GO_VERSION: "1.21.0"
42+
# GO_VERSION: '1.21.0'
43+
# GO_VERSION: 1.21.0
44+
# GO_VERSION:1.21.0
45+
# GO_VERSION:1.21.0
46+
local extracted_go_version=$(echo "$go_lines" | sed -n 's/.*GO_VERSION[: ]*["'\'']*\([0-9.]*\).*/\1/p')
47+
48+
# Check if the extracted Go version matches the required version.
49+
if [ "$extracted_go_version" != "$required_go_version" ]; then
50+
echo "Error finding pattern 'GO_VERSION:': $yamlfile specifies Go version '$extracted_go_version', but required version is '$required_go_version'."
51+
exit 1
52+
else
53+
echo "$yamlfile specifies Go version $required_go_version."
54+
fi
55+
fi
56+
}
57+
58+
# Check if the target Go version argument is provided.
59+
if [ $# -eq 0 ]; then
60+
echo "Usage: $0 <target_go_version>"
61+
exit 1
62+
fi
63+
64+
target_go_version="$1"
65+
66+
# Search for YAML files in the current directory and its subdirectories.
67+
yaml_files=$(find . -type f \( -name "*.yaml" -o -name "*.yml" \))
68+
69+
# Check each YAML file.
70+
for file in $yaml_files; do
71+
check_go_version_yaml "$file" "$target_go_version"
72+
check_go_version_env_variable "$file" "$target_go_version"
73+
done
74+
75+
echo "All YAML files pass the Go version check for Go version $target_go_version."

0 commit comments

Comments
 (0)