-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathcheck-go-version-dockerfile.sh
executable file
·53 lines (44 loc) · 1.75 KB
/
check-go-version-dockerfile.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/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 <target_go_version>"
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."