113 lines
2.7 KiB
Bash
Executable File
113 lines
2.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
|
||
_PLUG_TITLE="ntfy Helper"
|
||
_PLUG_DESCRIPTION=""
|
||
_PLUG_ARGS=(
|
||
"message;m;;string"
|
||
"title;t;;string;(No Title)"
|
||
"file;f;;path"
|
||
"level;l;;string;info"
|
||
"invoked-by;i;;string"
|
||
)
|
||
|
||
function get_level_emoji() {
|
||
case "$1" in
|
||
"critical"|"error") echo "⛔" ;;
|
||
"warning") echo "⚠️" ;;
|
||
"success") echo "✅" ;;
|
||
"info") echo "ℹ️" ;;
|
||
"debug"|*) echo "🔧" ;;
|
||
esac
|
||
}
|
||
|
||
function get_level_priority() {
|
||
case "$1" in
|
||
"critical") echo "5" ;;
|
||
"error") echo "4" ;;
|
||
"warning") echo "4" ;;
|
||
"success") echo "3" ;;
|
||
"info") echo "2" ;;
|
||
"debug"|*) echo "1" ;;
|
||
esac
|
||
}
|
||
|
||
function get_topic() {
|
||
invoked_by="$1"
|
||
topic_prefix="zio"
|
||
topic="test"
|
||
|
||
if [[ -n "$invoked_by" ]]; then
|
||
topic="$(echo "$invoked_by" | sed -r 's/sh.zio.//g' | sed -r 's/[.]+/-/g')"
|
||
fi
|
||
|
||
echo "${topic_prefix}_$topic"
|
||
}
|
||
|
||
function main() {
|
||
token_path="$(get_config_dir "sh.zio.notify")/token"
|
||
[[ ! -f "$token_path" ]] && die "'$token_path' does not exist"
|
||
|
||
token="$(cat "$token_path")"
|
||
|
||
hostname="$(hostname -s)"
|
||
invoked_by="$_invoked_by"
|
||
level_emoji="$(get_level_emoji "$_level")"
|
||
topic="$(get_topic "$_invoked_by")"
|
||
|
||
curl_command="curl"
|
||
|
||
curl_command+=" -H \"Authorization: Bearer $token\""
|
||
curl_command+=" -H \"Markdown: yes\""
|
||
curl_command+=" -H \"Priority: $(get_level_priority "$_level")\""
|
||
curl_command+=" -H \"Tags: $hostname,$invoked_by\""
|
||
curl_command+=" -H \"Title: $level_emoji [$hostname] $_title\""
|
||
|
||
if [[ "$_message" != "true" ]]; then
|
||
curl_command+=" -H \"Message: $_message\""
|
||
fi
|
||
|
||
if [[ "$_file" != "" ]]; then
|
||
if [[ ! -f "$_file" ]]; then
|
||
die "Cannot attach '$_file': file does not exist"
|
||
fi
|
||
|
||
if [[ "$n_message" != "" ]]; then
|
||
say warning "Cannot send --message/-m with --file/-f. Only sending file"
|
||
fi
|
||
|
||
full_file="$(realpath -s "$_file")"
|
||
|
||
curl_command+=" -T \"$full_file\""
|
||
curl_command+=" -H \"Filename: $(basename "$full_file")\""
|
||
else
|
||
curl_command+=" -X POST"
|
||
|
||
# HACK: Attachments don't work if you use "Cache: no"
|
||
if [[ "$topic" == "zio_test" ]]; then
|
||
curl_command+=" -H \"Cache: no\""
|
||
fi
|
||
fi
|
||
|
||
curl_command+=" https://ntfy.zio.sh/$topic"
|
||
eval $curl_command
|
||
}
|
||
|
||
if [[ $_PLUG_INVOKED != "true" ]]; then
|
||
base_dir="$(dirname "$(realpath -s "$0")")"
|
||
git_dir="$base_dir/../../.."
|
||
|
||
if [[ -d "$git_dir/.git" ]]; then
|
||
. "$git_dir/usr/local/libexec/zio/helpers/bash.sh"
|
||
else
|
||
. "/usr/local/libexec/zio/helpers/bash.sh"
|
||
fi
|
||
|
||
export -f get_config_dir
|
||
|
||
if [[ -d "$git_dir/.git" ]]; then
|
||
"$git_dir/usr/local/libexec/sodalite/invoker/src/invoke.sh" "$0" $@
|
||
else
|
||
"/usr/local/libexec/sodalite/invoker/src/invoke.sh" "$0" $@
|
||
fi
|
||
fi
|