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