Update gotify-send script
This commit is contained in:
parent
562e0087de
commit
cc1b58f90f
1 changed files with 23 additions and 12 deletions
|
@ -192,48 +192,59 @@
|
||||||
end
|
end
|
||||||
'';
|
'';
|
||||||
gotify-send = ''
|
gotify-send = ''
|
||||||
|
# --- Argument Check ---
|
||||||
if test (count $argv) -lt 1
|
if test (count $argv) -lt 1
|
||||||
echo "Usage: <command> | gotify-send <subject>" >&2
|
echo "Usage: <command> | gotify-send <subject>" >&2
|
||||||
cat > /dev/null
|
# Removed cat > /dev/null
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
|
|
||||||
set -l subject $argv[1]
|
set -l subject "$(hostname): $argv[1]"
|
||||||
set -l priority 3 # Default priority
|
set -l priority 3 # Default priority
|
||||||
set -l gotify_url "https://gotify.sysctl.io"
|
set -l gotify_url "https://gotify.sysctl.io"
|
||||||
set -l token_file "/var/run/secrets/gotify_global_token"
|
set -l token_file "/var/run/secrets/gotify_global_token"
|
||||||
|
|
||||||
# Read token from file
|
# --- Read Token ---
|
||||||
set -l gotify_token
|
set -l gotify_token
|
||||||
if test -r "$token_file"
|
if test -r "$token_file"
|
||||||
set gotify_token (cat "$token_file" | string trim)
|
set gotify_token (cat "$token_file" | string trim)
|
||||||
else
|
else
|
||||||
echo "Error: Cannot read token file '$token_file'" >&2
|
echo "Error: Cannot read token file '$token_file'" >&2
|
||||||
cat > /dev/null
|
# Removed cat > /dev/null
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
|
|
||||||
if test -z "$gotify_token"
|
if test -z "$gotify_token"
|
||||||
echo "Error: Token file '$token_file' is empty or read failed." >&2
|
echo "Error: Token file '$token_file' is empty or read failed." >&2
|
||||||
cat > /dev/null
|
# Removed cat > /dev/null
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
|
|
||||||
set -l message_body (string collect)
|
# --- Read Piped Input ---
|
||||||
|
set -l message_body "Command completed"
|
||||||
|
|
||||||
set -l escaped_subject (string escape --style=json "$subject")
|
# --- Manual JSON Escaping ---
|
||||||
set -l escaped_message (string escape --style=json "$message_body")
|
set -l escaped_subject (string replace -a '\\' '\\\\' "$subject" | string replace -a '"' '\\"')
|
||||||
|
set -l escaped_message (string replace -a '\\' '\\\\' "$message_body" | string replace -a '"' '\\"' | string replace -a \n '\\n')
|
||||||
|
|
||||||
|
# --- Construct Payload ---
|
||||||
set -l json_payload (printf '{"title": "%s", "message": "%s", "priority": %d}' \
|
set -l json_payload (printf '{"title": "%s", "message": "%s", "priority": %d}' \
|
||||||
"$escaped_subject" "$escaped_message" $priority)
|
"$escaped_subject" "$escaped_message" $priority)
|
||||||
|
|
||||||
# Assumes curl exists
|
# --- Execute curl (Verbose, Foreground) ---
|
||||||
echo $json_payload | curl -sS -X POST \
|
echo $json_payload | curl -v -X POST \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-H "X-Gotify-Key: $gotify_token" \
|
-H "X-Gotify-Key: $gotify_token" \
|
||||||
--data @- \
|
--data @- \
|
||||||
"$gotify_url/message" > /dev/null 2>&1 &
|
"$gotify_url/message"
|
||||||
|
|
||||||
|
# --- Check curl Status ---
|
||||||
|
set curl_status $status
|
||||||
|
echo "--- curl Exit Status: $curl_status ---" >&2
|
||||||
|
|
||||||
|
if test $curl_status -ne 0
|
||||||
|
echo "Error: curl command failed." >&2
|
||||||
|
return $curl_status
|
||||||
|
end
|
||||||
return 0
|
return 0
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue