Repository navigation
Expand file tree
/
Copy pathgenerate_versions_json.sh
More file actions
executable file
·78 lines (67 loc) · 1.87 KB
/
Copy pathgenerate_versions_json.sh
File metadata and controls
executable file
·78 lines (67 loc) · 1.87 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env bash
set -euo pipefail
# Path to the Dockerfile passed as first arg
DOCKERFILE_PATH="$1"
# Check that path was provided
if [[ -z "$DOCKERFILE_PATH" ]]; then
echo "Error: no Dockerfile path provided." >&2
exit 1
fi
# Declare associative array: binary -> JSON array string
declare -A versions_map
# Find all unique binaries installed via install-tool
binaries=$(grep -E '^RUN install-tool ' "$DOCKERFILE_PATH" | awk '{print $3}' | sort -u)
for bin in $binaries; do
# 1) build the ARG name: replace hyphens with underscores, uppercase, then add _VERSION
arg_name="${bin//-/_}"
arg_name="${arg_name^^}_VERSION"
# collect versions from ARG declarations: ARG BIN_VERSION=...
mapfile -t arg_vals < <(
grep -E "^ARG ${arg_name}=" "$DOCKERFILE_PATH" \
| cut -d'=' -f2
)
# 2) collect inline versions from RUN install-tool bin VERSION
mapfile -t raw_inline_vals < <(
grep -E "^RUN install-tool ${bin} " "$DOCKERFILE_PATH" \
| awk '{print $4}'
)
# filter only those that look like a version (start with digit)
inline_vals=()
for v in "${raw_inline_vals[@]}"; do
if [[ $v =~ ^[0-9] ]]; then
inline_vals+=("$v")
fi
done
# merge and dedupe
all_vals=("${arg_vals[@]}" "${inline_vals[@]}")
unique_vals=()
declare -A seen
for v in "${all_vals[@]}"; do
[[ -z "$v" ]] && continue
if [[ -z "${seen[$v]:-}" ]]; then
unique_vals+=("$v")
seen[$v]=1
fi
done
# build JSON array
if [ "${#unique_vals[@]}" -eq 0 ]; then
json_array="[]"
else
json_items=$(printf '"%s",' "${unique_vals[@]}")
json_array="[${json_items%,}]"
fi
versions_map["$bin"]="$json_array"
done
# emit sorted JSON object
echo "{"
first=true
for bin in $(printf '%s\n' "${!versions_map[@]}" | sort); do
if $first; then
first=false
else
echo ","
fi
echo -n " \"${bin}\": ${versions_map[$bin]}"
done
echo
echo "}"