Skip to content

Commit dc3f9d2

Browse files
committed
linter: copy over Go version check scripts from LND
Replaced the existing Go version check scripts with those from the LND repository. These updated scripts are confirmed to work on macOS. The commit also relocates the scripts to the `scripts` directory from the `tools` directory.
1 parent 39c1f27 commit dc3f9d2

File tree

5 files changed

+141
-118
lines changed

5 files changed

+141
-118
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,11 @@ fmt: $(GOIMPORTS_BIN)
275275

276276
check-go-version-yaml:
277277
@$(call print, "Checking for target Go version (v$(GO_VERSION)) in YAML files (*.yaml, *.yml)")
278-
./tools/check-go-version-yaml.sh $(GO_VERSION)
278+
./scripts/check-go-version-yaml.sh $(GO_VERSION)
279279

280280
check-go-version-dockerfile:
281281
@$(call print, "Checking for target Go version (v$(GO_VERSION)) in Dockerfile files (*Dockerfile)")
282-
./tools/check-go-version-dockerfile.sh $(GO_VERSION)
282+
./scripts/check-go-version-dockerfile.sh $(GO_VERSION)
283283

284284
lint-source: docker-tools
285285
@$(call print, "Linting source.")
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."

tools/check-go-version-dockerfile.sh

Lines changed: 0 additions & 53 deletions
This file was deleted.

tools/check-go-version-yaml.sh

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)