Skip to content

Commit

Permalink
chore: Add task update to update versions used in compose.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
RemiBardon committed Jan 8, 2025
1 parent 0af9b7d commit e80fa60
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Taskfile.dist.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# See <https://taskfile.dev/usage> and <https://taskfile.dev/reference/schema>
# to find all keys and their usage.
version: "3"

silent: true
env:
SELF: "task {{ .ALIAS | default .TASK }} --"

tasks:
update:
desc: Update which versions of Prose Pod Server and Prose Pod API to use.
cmd: ./tools/update {{ .CLI_ARGS }}
82 changes: 82 additions & 0 deletions tools/update
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash

COMPOSE_FILE=compose.yaml

set -e

: ${SELF:="$(basename $0)"}

Color_Off='\033[0m'
Green='\033[0;32m'
Yellow='\033[0;33m'
BRed='\033[1;31m'

die() {
echo -e "${BRed:?}${@:?"Must pass a message"}${Color_Off:?}" >&2
exit 1
}
usage() {
echo "Usage: ${SELF:?} [api=X.Y.Z] [server=X.Y.Z]"
}
help() {
usage
exit 0
}

[[ $# == 0 ]] && die $(usage)

# A simplified `sed` command that works on both macOS and Linux.
replace() {
local in_place
[[ "$1" == "-i" ]] && { in_place=1; shift 1; }

if [[ "$(uname)" == "Darwin" ]]; then
echo sed ${in_place:+-i ''} -E
else
echo sed ${in_place:+-i} -E
fi
}
replace_version() {
local regex="${1:?}" replace="${2:?}" file="${3:?}"
local pattern="s/${regex}/${replace}/"

$(replace -i) "$pattern" "$file"
}
find_version() {
local regex="${1:?}"
$(replace) -n 's/.*'"${regex}"'.*/\2/p' "${COMPOSE_FILE:?}"
}

for arg in "$@"; do
case $arg in
api=*) NEW_API_TAG="${arg#'api='}" ;;
server=*) NEW_SERVER_TAG="${arg#'server='}" ;;
--help) help ;;
*) die "Unknown argument: $arg.\n$(usage)" ;;
esac
done

# `:` blocks the greedy `+` from eating the `:-` prefix and `}` the suffix.
# Otherwise, allow pretty much any name, there's no reason to enforce a pattern here.
DOCKER_TAG_REGEX='[^:}]+'

if [[ -n "${NEW_API_TAG}" ]]; then
API_REGEX='(PROSE_POD_API_IMAGE_TAG.+:-)('"${DOCKER_TAG_REGEX:?}"')\}+"'
OLD_API_TAG="$(find_version "${API_REGEX:?}")"
echo -e "API: ${Yellow:?}${OLD_API_TAG:?}${Color_Off:?} -> ${Green:?}${NEW_API_TAG:?}${Color_Off:?}"
fi
if [[ -n "${NEW_SERVER_TAG}" ]]; then
SERVER_REGEX='(PROSE_POD_SERVER_IMAGE_TAG.+:-)('"${DOCKER_TAG_REGEX:?}"')\}+"'
OLD_SERVER_TAG="$(find_version "${SERVER_REGEX:?}")"
echo -e "Server: ${Yellow:?}${OLD_SERVER_TAG:?}${Color_Off:?} -> ${Green:?}${NEW_SERVER_TAG:?}${Color_Off:?}"
fi

read -p "Proceed? [y/N]: " ok
[[ "$ok" =~ ^[Yy]$ ]] || exit 1

if [[ -n "${NEW_API_TAG}" ]]; then
replace "${API_REGEX:?}" "\1${NEW_API_TAG:?}" "${COMPOSE_FILE:?}"
fi
if [[ -n "${NEW_SERVER_TAG}" ]]; then
replace "${SERVER_REGEX:?}" "\1${NEW_SERVER_TAG:?}" "${COMPOSE_FILE:?}"
fi

0 comments on commit e80fa60

Please sign in to comment.