Skip to content

Commit e315323

Browse files
committed
make: add Go version linting for Dockerfile and YAML files
1 parent fec4a40 commit e315323

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ DOCKER_TOOLS = docker run \
5757
-v $(shell bash -c "go env GOMODCACHE || (mkdir -p /tmp/go-modcache; echo /tmp/go-modcache)"):/tmp/build/.modcache \
5858
-v $$(pwd):/build taproot-assets-tools
5959

60+
GO_VERSION = 1.21.0
61+
6062
GREEN := "\\033[0;32m"
6163
NC := "\\033[0m"
6264
define print
@@ -251,10 +253,20 @@ fmt: $(GOIMPORTS_BIN)
251253
@$(call print, "Formatting source.")
252254
gofmt -l -w -s $(GOFILES_NOVENDOR)
253255

254-
lint: docker-tools
256+
check-go-version-yaml:
257+
@$(call print, "Checking for target Go version (v$(GO_VERSION)) in YAML files (*.yaml, *.yml)")
258+
./tools/check-go-version-yaml.sh $(GO_VERSION)
259+
260+
check-go-version-dockerfile:
261+
@$(call print, "Checking for target Go version (v$(GO_VERSION)) in Dockerfile files (*Dockerfile)")
262+
./tools/check-go-version-dockerfile.sh $(GO_VERSION)
263+
264+
lint-source: docker-tools
255265
@$(call print, "Linting source.")
256266
$(DOCKER_TOOLS) golangci-lint run -v $(LINT_WORKERS)
257267

268+
lint: lint-source check-go-version-dockerfile check-go-version-yaml
269+
258270
list:
259271
@$(call print, "Listing commands.")
260272
@$(MAKE) -qp | \
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 echo "$go_lines" | grep -q -v "$required_go_version"; then
13+
echo "Error: $dockerfile does not use Go version $required_go_version exclusively."
14+
exit 1
15+
else
16+
echo "$dockerfile is using Go version $required_go_version."
17+
fi
18+
}
19+
20+
# Check if the target Go version argument is provided
21+
if [ $# -eq 0 ]; then
22+
echo "Usage: $0 <target_go_version>"
23+
exit 1
24+
fi
25+
26+
target_go_version="$1"
27+
28+
# Search for Dockerfiles in the current directory and its subdirectories
29+
dockerfiles=$(find . -type f -name "*.Dockerfile" -o -name "Dockerfile")
30+
31+
# Check each Dockerfile
32+
for file in $dockerfiles; do
33+
check_go_version "$file" "$target_go_version"
34+
done
35+
36+
echo "All Dockerfiles pass the Go version check for Go version $target_go_version."

tools/check-go-version-yaml.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
# Function to check if the YAML file contains the specified Go version after 'GO_VERSION:'
4+
check_go_version() {
5+
local yamlfile="$1"
6+
local required_go_version="$2"
7+
8+
# Use grep to find lines with 'GO_VERSION:'
9+
local go_lines=$(grep -i 'GO_VERSION:' "$yamlfile" || true) # Ignore grep exit status
10+
11+
# Check if any lines specify the Go version
12+
if [ -n "$go_lines" ]; then
13+
# Extract the Go version from the file's lines. Example matching strings:
14+
# GO_VERSION: "1.21.0"
15+
# GO_VERSION: '1.21.0'
16+
# GO_VERSION: 1.21.0
17+
# GO_VERSION:1.21.0
18+
# GO_VERSION:1.21.0
19+
local extracted_go_version=$(echo "$go_lines" | sed -n 's/^[[:space:]]*GO_VERSION:[[:space:]]*\(['\''"]\?\)\?\([0-9]\+\.[0-9]\+\.[0-9]\+\)\(['\''"]\?\)\?/\2/p')
20+
21+
# Check if the extracted Go version matches the required version
22+
if [ "$extracted_go_version" != "$required_go_version" ]; then
23+
echo "Error: $yamlfile specifies Go version '$extracted_go_version', but not version '$required_go_version'."
24+
exit 1
25+
else
26+
echo "$yamlfile specifies Go version $required_go_version."
27+
fi
28+
fi
29+
}
30+
31+
# Check if the target Go version argument is provided
32+
if [ $# -eq 0 ]; then
33+
echo "Usage: $0 <target_go_version>"
34+
exit 1
35+
fi
36+
37+
target_go_version="$1"
38+
39+
# Search for YAML files in the current directory and its subdirectories
40+
yaml_files=$(find . -type f -name "*.yaml" -o -name "*.yml")
41+
42+
# Check each YAML file
43+
for file in $yaml_files; do
44+
check_go_version "$file" "$target_go_version"
45+
done
46+
47+
echo "All YAML files pass the Go version check for Go version $target_go_version."

0 commit comments

Comments
 (0)