From e80fa60f10426bccc11f04636abc95eb0bfc3680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Bardon?= Date: Wed, 8 Jan 2025 23:05:07 +0100 Subject: [PATCH] chore: Add `task update` to update versions used in `compose.yaml` --- Taskfile.dist.yaml | 12 +++++++ tools/update | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 Taskfile.dist.yaml create mode 100755 tools/update diff --git a/Taskfile.dist.yaml b/Taskfile.dist.yaml new file mode 100644 index 0000000..4b0a2f3 --- /dev/null +++ b/Taskfile.dist.yaml @@ -0,0 +1,12 @@ +# See and +# 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 }} diff --git a/tools/update b/tools/update new file mode 100755 index 0000000..36c3ec2 --- /dev/null +++ b/tools/update @@ -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