From dc3f9d2cfafc730ebdd0c36c836069289724869c Mon Sep 17 00:00:00 2001 From: ffranr Date: Tue, 13 Aug 2024 13:26:45 +0100 Subject: [PATCH] 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. --- Makefile | 4 +- scripts/check-go-version-dockerfile.sh | 64 ++++++++++++++++++++++ scripts/check-go-version-yaml.sh | 75 ++++++++++++++++++++++++++ tools/check-go-version-dockerfile.sh | 53 ------------------ tools/check-go-version-yaml.sh | 63 ---------------------- 5 files changed, 141 insertions(+), 118 deletions(-) create mode 100755 scripts/check-go-version-dockerfile.sh create mode 100755 scripts/check-go-version-yaml.sh delete mode 100755 tools/check-go-version-dockerfile.sh delete mode 100755 tools/check-go-version-yaml.sh diff --git a/Makefile b/Makefile index a2bae47c9..b55359b4d 100644 --- a/Makefile +++ b/Makefile @@ -275,11 +275,11 @@ fmt: $(GOIMPORTS_BIN) check-go-version-yaml: @$(call print, "Checking for target Go version (v$(GO_VERSION)) in YAML files (*.yaml, *.yml)") - ./tools/check-go-version-yaml.sh $(GO_VERSION) + ./scripts/check-go-version-yaml.sh $(GO_VERSION) check-go-version-dockerfile: @$(call print, "Checking for target Go version (v$(GO_VERSION)) in Dockerfile files (*Dockerfile)") - ./tools/check-go-version-dockerfile.sh $(GO_VERSION) + ./scripts/check-go-version-dockerfile.sh $(GO_VERSION) lint-source: docker-tools @$(call print, "Linting source.") diff --git a/scripts/check-go-version-dockerfile.sh b/scripts/check-go-version-dockerfile.sh new file mode 100755 index 000000000..303ee42ed --- /dev/null +++ b/scripts/check-go-version-dockerfile.sh @@ -0,0 +1,64 @@ +#!/bin/bash + +# Function to check if the Dockerfile contains only the specified Go version. +check_go_version() { + local dockerfile="$1" + local required_go_version="$2" + + # Use grep to find lines with 'FROM golang:' + local go_lines=$(grep -i '^FROM golang:' "$dockerfile") + + # Check if all lines have the required Go version. + if [ -z "$go_lines" ]; then + # No Go version found in the file. Skip the check. + return + elif echo "$go_lines" | grep -q -v "$required_go_version"; then + echo "$go_lines" + echo "Error: $dockerfile does not use Go version $required_go_version exclusively." + exit 1 + else + echo "$dockerfile is using Go version $required_go_version." + fi +} + +# Check if the target Go version argument is provided. +if [ $# -eq 0 ]; then + echo "Usage: $0 " + exit 1 +fi + +target_go_version="$1" + +# File paths to be excluded from the check. +exception_list=( + # Exclude the tools Dockerfile as otherwise the linter may need to be + # considered every time the Go version is updated. + "./tools/Dockerfile" +) + +# is_exception checks if a file is in the exception list. +is_exception() { + local file="$1" + for exception in "${exception_list[@]}"; do + if [ "$file" == "$exception" ]; then + return 0 + fi + done + return 1 +} + +# Search for Dockerfiles in the current directory and its subdirectories. +dockerfiles=$(find . -type f -name "*.Dockerfile" -o -name "Dockerfile") + +# Check each Dockerfile +for file in $dockerfiles; do + # Skip the file if it is in the exception list. + if is_exception "$file"; then + echo "Skipping $file" + continue + fi + + check_go_version "$file" "$target_go_version" +done + +echo "All Dockerfiles pass the Go version check for Go version $target_go_version." diff --git a/scripts/check-go-version-yaml.sh b/scripts/check-go-version-yaml.sh new file mode 100755 index 000000000..bd65ffa01 --- /dev/null +++ b/scripts/check-go-version-yaml.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +# Function to check if the YAML file contains the specified Go version after +# field 'go:'. +check_go_version_yaml() { + local yamlfile="$1" + local required_go_version="$2" + + # Use grep to find lines with 'go:'. The grep exist status is ignored. + local go_lines=$(grep -i '^\s*go:\s*"[0-9]\+\.[0-9]\+\(\.[0-9]\+\)\?"' "$yamlfile" || true) + + # Check if any lines specify the Go version. + if [ -n "$go_lines" ]; then + # Extract the Go version from the file's lines. Example matching strings: + # go: "1.21.0" + local extracted_go_version=$(echo "$go_lines" | sed -n 's/.*go: "\([^"]*\)".*/\1/p') + + # Check if the extracted Go version matches the required version. + if [ "$extracted_go_version" != "$required_go_version" ]; then + echo "Error finding pattern 'go:': $yamlfile specifies Go version '$extracted_go_version', but required version is '$required_go_version'." + exit 1 + else + echo "$yamlfile specifies Go version $required_go_version." + fi + fi +} + +# Function to check if the YAML file contains the specified Go version after +# environment variable 'GO_VERSION:'. +check_go_version_env_variable() { + local yamlfile="$1" + local required_go_version="$2" + + # Use grep to find lines with 'GO_VERSION:'. The grep exist status is + # ignored. + local go_lines=$(grep -i 'GO_VERSION:' "$yamlfile" || true) + + # Check if any lines specify the Go version. + if [ -n "$go_lines" ]; then + # Extract the Go version from the file's lines. Example matching strings: + # GO_VERSION: "1.21.0" + # GO_VERSION: '1.21.0' + # GO_VERSION: 1.21.0 + # GO_VERSION:1.21.0 + # GO_VERSION:1.21.0 + local extracted_go_version=$(echo "$go_lines" | sed -n 's/.*GO_VERSION[: ]*["'\'']*\([0-9.]*\).*/\1/p') + + # Check if the extracted Go version matches the required version. + if [ "$extracted_go_version" != "$required_go_version" ]; then + echo "Error finding pattern 'GO_VERSION:': $yamlfile specifies Go version '$extracted_go_version', but required version is '$required_go_version'." + exit 1 + else + echo "$yamlfile specifies Go version $required_go_version." + fi + fi +} + +# Check if the target Go version argument is provided. +if [ $# -eq 0 ]; then + echo "Usage: $0 " + exit 1 +fi + +target_go_version="$1" + +# Search for YAML files in the current directory and its subdirectories. +yaml_files=$(find . -type f \( -name "*.yaml" -o -name "*.yml" \)) + +# Check each YAML file. +for file in $yaml_files; do + check_go_version_yaml "$file" "$target_go_version" + check_go_version_env_variable "$file" "$target_go_version" +done + +echo "All YAML files pass the Go version check for Go version $target_go_version." diff --git a/tools/check-go-version-dockerfile.sh b/tools/check-go-version-dockerfile.sh deleted file mode 100755 index 4e4fe635d..000000000 --- a/tools/check-go-version-dockerfile.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/bash - -# Function to check if the Dockerfile contains only the specified Go version -check_go_version() { - local dockerfile="$1" - local required_go_version="$2" - - # Use grep to find lines with 'FROM golang:' - local go_lines=$(grep -i '^FROM golang:' "$dockerfile") - - # Check if all lines have the required Go version - if echo "$go_lines" | grep -q -v "$required_go_version"; then - echo "Error: $dockerfile does not use Go version $required_go_version exclusively." - exit 1 - else - echo "$dockerfile is using Go version $required_go_version." - fi -} - -# Check if the target Go version argument is provided -if [ $# -eq 0 ]; then - echo "Usage: $0 " - exit 1 -fi - -target_go_version="$1" - -# We find target files using the 'find' command in conjunction with the 'read' -# command. We exclude some directories from the search. -# -# We use the 'read' command to help ensure that we correctly handle filenames -# with spaces, newlines, and special characters. The '-print0' option in 'find' -# outputs filenames separated by a null character. This allows the 'read' -# command in the while loop to accurately distinguish each filename. The -# 'target_files' array is then populated, preserving the integrity of each -# filename. This approach ensures safe handling of filenames, regardless of -# their complexity. -while IFS= read -r -d '' file; do - target_files+=("$file") -done < <(find . \ - -path ".**/vendor" -prune -o \ - -type f \ - \( -name "*.Dockerfile" -o -name "Dockerfile" \) \ - -print0 \ -) - -# Check for the expected Go version in each file. -for file in "${target_files[@]}"; do - check_go_version "$file" "$target_go_version" -done - - -echo "All Dockerfiles pass the Go version check for Go version $target_go_version." diff --git a/tools/check-go-version-yaml.sh b/tools/check-go-version-yaml.sh deleted file mode 100755 index 6091dee42..000000000 --- a/tools/check-go-version-yaml.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/bin/bash - -# Function to check if the YAML file contains the specified Go version after 'GO_VERSION:' -check_go_version() { - local yamlfile="$1" - local required_go_version="$2" - - # Use grep to find lines with 'GO_VERSION:' - local go_lines=$(grep -i 'GO_VERSION:' "$yamlfile" || true) # Ignore grep exit status - - # Check if any lines specify the Go version - if [ -n "$go_lines" ]; then - # Extract the Go version from the file's lines. Example matching strings: - # GO_VERSION: "1.21.0" - # GO_VERSION: '1.21.0' - # GO_VERSION: 1.21.0 - # GO_VERSION:1.21.0 - # GO_VERSION:1.21.0 - local extracted_go_version=$(echo "$go_lines" | sed -n 's/^[[:space:]]*GO_VERSION:[[:space:]]*\(['\''"]\?\)\?\([0-9]\+\.[0-9]\+\.[0-9]\+\)\(['\''"]\?\)\?/\2/p') - - # Check if the extracted Go version matches the required version - if [ "$extracted_go_version" != "$required_go_version" ]; then - echo "Error: $yamlfile specifies Go version '$extracted_go_version', but not version '$required_go_version'." - exit 1 - else - echo "$yamlfile specifies Go version $required_go_version." - fi - fi -} - -# Check if the target Go version argument is provided -if [ $# -eq 0 ]; then - echo "Usage: $0 " - exit 1 -fi - -target_go_version="$1" - -# We find target files using the 'find' command in conjunction with the 'read' -# command. We exclude some directories from the search. -# -# We use the 'read' command to help ensure that we correctly handle filenames -# with spaces, newlines, and special characters. The '-print0' option in 'find' -# outputs filenames separated by a null character. This allows the 'read' -# command in the while loop to accurately distinguish each filename. The -# 'target_files' array is then populated, preserving the integrity of each -# filename. This approach ensures safe handling of filenames, regardless of -# their complexity. -while IFS= read -r -d '' file; do - target_files+=("$file") -done < <(find . \ - -path ".**/vendor" -prune -o \ - -type f \ - \( -name "*.yaml" -o -name "*.yml" \) \ - -print0 \ -) - -# Check for the expected Go version in each file. -for file in "${target_files[@]}"; do - check_go_version "$file" "$target_go_version" -done - -echo "All YAML files pass the Go version check for Go version $target_go_version."