Skip to content

Commit 052a5f0

Browse files
authored
Modernize generate-stackbrew-library.sh (#94)
1 parent f7a6746 commit 052a5f0

File tree

1 file changed

+58
-25
lines changed

1 file changed

+58
-25
lines changed

generate-stackbrew-library.sh

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
#!/bin/bash
2-
set -euo pipefail
1+
#!/usr/bin/env bash
2+
set -Eeuo pipefail
33

44
self="$(basename "$BASH_SOURCE")"
55
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
66
defaultVariant='apache'
77

8-
# Get the most recent commit which modified any of "$@".
8+
# get the most recent commit which modified any of "$@"
99
fileCommit() {
1010
git log -1 --format='format:%H' HEAD -- "$@"
1111
}
1212

13-
# Get the most recent commit which modified "$1/Dockerfile" or any file that
14-
# the Dockerfile copies into the rootfs (with COPY).
15-
dockerfileCommit() {
13+
# get the most recent commit which modified "$1/Dockerfile" or any file COPY'd from "$1/Dockerfile"
14+
dirCommit() {
1615
local dir="$1"; shift
1716
(
18-
cd "$dir";
19-
fileCommit Dockerfile \
20-
$(git show HEAD:./Dockerfile | awk '
17+
cd "$dir"
18+
files="$(
19+
git show HEAD:./Dockerfile | awk '
2120
toupper($1) == "COPY" {
2221
for (i = 2; i < NF; i++) {
2322
if ($i ~ /^--from=/) {
@@ -26,27 +25,48 @@ dockerfileCommit() {
2625
print $i
2726
}
2827
}
29-
')
28+
'
29+
)"
30+
fileCommit Dockerfile $files
3031
)
3132
}
3233

34+
gawkParents='
35+
{ cmd = toupper($1) }
36+
cmd == "FROM" {
37+
print $2
38+
next
39+
}
40+
cmd == "COPY" {
41+
for (i = 2; i < NF; i++) {
42+
if ($i ~ /^--from=/) {
43+
gsub(/^--from=/, "", $i)
44+
print $i
45+
next
46+
}
47+
}
48+
}
49+
'
50+
3351
getArches() {
3452
local repo="$1"; shift
35-
local officialImagesUrl='https://github.com/docker-library/official-images/raw/master/library/'
53+
local officialImagesBase="${BASHBREW_LIBRARY:-https://github.com/docker-library/official-images/raw/HEAD/library}/"
3654

37-
eval "declare -g -A parentRepoToArches=( $(
38-
find -name 'Dockerfile' -exec awk '
39-
toupper($1) == "FROM" && $2 !~ /^('"$repo"'|scratch|microsoft\/[^:]+)(:|$)/ {
40-
print "'"$officialImagesUrl"'" $2
41-
}
42-
' '{}' + \
55+
local parentRepoToArchesStr
56+
parentRepoToArchesStr="$(
57+
find -name 'Dockerfile' -exec gawk "$gawkParents" '{}' + \
4358
| sort -u \
44-
| xargs bashbrew cat --format '[{{ .RepoName }}:{{ .TagName }}]="{{ join " " .TagEntry.Architectures }}"'
45-
) )"
59+
| gawk -v officialImagesBase="$officialImagesBase" '
60+
$1 !~ /^('"$repo"'|scratch|.*\/.*)(:|$)/ {
61+
printf "%s%s\n", officialImagesBase, $1
62+
}
63+
' \
64+
| xargs -r bashbrew cat --format '["{{ .RepoName }}:{{ .TagName }}"]="{{ join " " .TagEntry.Architectures }}"'
65+
)"
66+
eval "declare -g -A parentRepoToArches=( $parentRepoToArchesStr )"
4667
}
4768
getArches 'postfixadmin'
4869

49-
# Header.
5070
cat <<-EOH
5171
# This file is generated via https://github.com/postfixadmin/docker/blob/$(fileCommit "$self")/$self
5272
Maintainers: David Goodwin <david@codepoets.co.uk> (@DavidGoodwin)
@@ -60,14 +80,13 @@ join() {
6080
echo "${out#$sep}"
6181
}
6282

63-
SEARCHFOR="Upgrade available - the latest version is "
64-
latest=$(curl -fsSL http://postfixadmin.sourceforge.net/update-check.php | grep -iF "$SEARCHFOR" | sed -E "s@${SEARCHFOR}([0-9.]+)</div>@\1@")
83+
latest="$(curl -fsSL https://api.github.com/repos/postfixadmin/postfixadmin/releases/latest | jq -r '.tag_name' | tr -d 'v')"
6584

6685
variants=( */ )
6786
variants=( "${variants[@]%/}" )
6887

6988
for variant in "${variants[@]}"; do
70-
commit="$(dockerfileCommit "$variant")"
89+
commit="$(dirCommit "$variant")"
7190
fullversion="$(git show "$commit":"$variant/Dockerfile" | grep -iF "ARG POSTFIXADMIN_VERSION" | sed -E "s@ARG POSTFIXADMIN_VERSION=([0-9.]+)@\1@")"
7291

7392
versionAliases=( "$fullversion" "${fullversion%.*}" "${fullversion%.*.*}" )
@@ -82,8 +101,22 @@ for variant in "${variants[@]}"; do
82101
variantAliases+=( "${versionAliases[@]}" )
83102
fi
84103

85-
variantParent="$(awk 'toupper($1) == "FROM" { print $2 }' "$variant/Dockerfile")"
86-
variantArches="${parentRepoToArches[$variantParent]}"
104+
variantParents="$(gawk "$gawkParents" "$variant/Dockerfile")"
105+
variantArches=
106+
for variantParent in $variantParents; do
107+
parentArches="${parentRepoToArches[$variantParent]:-}"
108+
if [ -z "$parentArches" ]; then
109+
continue
110+
elif [ -z "$variantArches" ]; then
111+
variantArches="$parentArches"
112+
else
113+
variantArches="$(
114+
comm -12 \
115+
<(xargs -n1 <<<"$variantArches" | sort -u) \
116+
<(xargs -n1 <<<"$parentArches" | sort -u)
117+
)"
118+
fi
119+
done
87120

88121
cat <<-EOE
89122

0 commit comments

Comments
 (0)