Willy Tarreau | f1c6ccf | 2022-05-30 15:19:06 +0200 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Scan a branch directory for source tarballs and rebuild the releases.json |
| 4 | # file for that branch. md5 and sha256 are added if present. The highest |
| 5 | # numberred version is referenced as the latest release. |
| 6 | # |
| 7 | # Usage: $0 [-b branch] [-o outfile] /path/to/download/branch |
| 8 | # |
| 9 | |
| 10 | USAGE="Usage: ${0##*/} [-b branch] [-o outfile] DIR" |
| 11 | OUTPUT= |
| 12 | BRANCH= |
| 13 | DIR= |
| 14 | |
| 15 | die() { |
| 16 | [ "$#" -eq 0 ] || echo "$*" >&2 |
| 17 | exit 1 |
| 18 | } |
| 19 | |
| 20 | err() { |
| 21 | echo "$*" >&2 |
| 22 | } |
| 23 | |
| 24 | quit() { |
| 25 | [ "$#" -eq 0 -o -n "$QUIET" ] || echo "$*" |
| 26 | exit 0 |
| 27 | } |
| 28 | |
| 29 | emit_json() { |
| 30 | printf '{\n "branch": "%s",\n' ${BRANCH} |
| 31 | latest="" |
| 32 | for file in $(find "$DIR/src" -name 'haproxy-[0-9]*.gz' -printf "%P\n" |grep -v '[0-9]-patches*' | sort -rV ); do |
| 33 | rel="${file##*haproxy-}" |
| 34 | rel="${rel%%.tar.*}" |
| 35 | if [ -z "$latest" ]; then |
| 36 | latest="$rel"; |
| 37 | printf ' "latest_release": "%s",\n' ${latest} |
| 38 | printf ' "releases": {\n' |
| 39 | else |
| 40 | printf ",\n" |
| 41 | fi |
| 42 | printf ' "%s": {\n' ${rel} |
| 43 | printf ' "file": "%s"' ${file} |
| 44 | if [ -s "$DIR/src/$file.md5" ]; then |
| 45 | printf ',\n "md5": "%s"' $(awk '{print $1}' "$DIR/src/$file.md5") |
| 46 | fi |
| 47 | if [ -s "$DIR/src/$file.sha256" ]; then |
| 48 | printf ',\n "sha256": "%s"' $(awk '{print $1}' "$DIR/src/$file.sha256") |
| 49 | fi |
| 50 | printf '\n }' |
| 51 | done |
| 52 | |
| 53 | if [ -n "$latest" ]; then |
| 54 | printf "\n }" ## "releases" |
| 55 | fi |
| 56 | |
| 57 | printf '\n}\n' |
| 58 | } |
| 59 | |
| 60 | |
| 61 | ### main |
| 62 | |
| 63 | while [ -n "$1" -a -z "${1##-*}" ]; do |
| 64 | case "$1" in |
| 65 | -b) BRANCH="$2" ; shift 2 ;; |
| 66 | -o) OUTPUT="$2" ; shift 2 ;; |
| 67 | -h|--help) quit "$USAGE" ;; |
| 68 | *) die "$USAGE" ;; |
| 69 | esac |
| 70 | done |
| 71 | |
| 72 | if [ $# -ne 1 ]; then |
| 73 | die "$USAGE" |
| 74 | fi |
| 75 | |
| 76 | DIR="$1" ; shift |
| 77 | if [ -z "$DIR" ]; then |
| 78 | die "Missing download directory name." |
| 79 | fi |
| 80 | |
| 81 | if [ ! -d "$DIR/." ]; then |
| 82 | die "Download directory doesn't exist : $DIR" |
| 83 | fi |
| 84 | |
| 85 | if [ ! -d "$DIR/src" ]; then |
| 86 | die "Download directory must contain 'src' : $DIR" |
| 87 | fi |
| 88 | |
| 89 | if [ -z "$BRANCH" ]; then |
| 90 | BRANCH=${DIR##*/} |
| 91 | if [ -n "${BRANCH//[0-9.]}" ]; then |
| 92 | die "Couldn't determine branch number from dir name: $BRANCH" |
| 93 | fi |
| 94 | fi |
| 95 | |
| 96 | # echo "debug: DIR=$DIR BRANCH=$BRANCH" |
| 97 | if [ -n "$OUTPUT" ]; then |
| 98 | emit_json > "$OUTPUT.tmp" |
| 99 | mv -f "$OUTPUT.tmp" "$OUTPUT" |
| 100 | rm -f "$OUTPUT.tmp" |
| 101 | else |
| 102 | emit_json |
| 103 | fi |