From 92efdb08484cb163d4f7167c7cceacbfd87049e2 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Tue, 2 Apr 2024 16:29:27 +0200 Subject: [PATCH] new: [0km] add ``vps-subscribe {add,rm} CHANNEL TOPICS`` actions --- bin/0km | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/bin/0km b/bin/0km index 4f13f5b..8bb3393 100755 --- a/bin/0km +++ b/bin/0km @@ -656,6 +656,92 @@ EOF } +NTFY_TOPIC_FILE="/etc/ntfy/topics.yml" +subscribe:ntfy:exists() { + local vps="$1" + if ! out=$(echo "[ -f \"$NTFY_TOPIC_FILE\" ] && echo ok || true" | \ + ssh:run "root@$vps" -- bash); then + err "Unable to check for existence of '$NTFY_TOPIC_FILE'." + fi + if [ -z "$out" ]; then + err "File '$NTFY_TOPIC_FILE' not found on $vps." + return 1 + fi +} + + +ntfy:rm() { + local channel="$1" topic="$2" vps="$3" + subscribe:ntfy:exists "$vps" || return 1 + if ! out=$(echo "yq -i 'del(.[\"$channel\"][] | select(. == \"$TOPIC\"))' \"$NTFY_TOPIC_FILE\"" | \ + ssh:run "root@$vps" -- bash); then + err "Failed to remove channel '$channel' from '$NTFY_TOPIC_FILE'." + return 1 + fi + info "Channel '$channel' removed from '$NTFY_TOPIC_FILE' on $vps." + ssh:run "root@$vps" -- cat "$NTFY_TOPIC_FILE" +} + +ntfy:add() { + local vps="$1" + read-0 channel topic || { + err "Couldn't read CHANNEL and TOPIC arguments." + return 1 + } + vps_connection_check "$vps"