-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpush
executable file
·78 lines (66 loc) · 2.57 KB
/
push
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env bash
################################################################################
# Global variables.
################################################################################
_NAME="$(command basename "$0")"
_HELP_DOC="Send a push notification using Pushover, a push notification service.
Usage:
$_NAME [options] <text_message>
Options:
-p Send message with high priority.
-h Print help.
Environment variables:
PUSHOVER_USER https://pushover.net
PUSHOVER_TOKEN https://pushover.net/apps/build"
CURL_ARGS="--connect-timeout 2 --max-time 5"
################################################################################
# Helper methods.
################################################################################
# Usage:
# error <message> [<exit_code>]
error() {
[[ $2 -eq 0 ]] && std_err_or_out=1 || std_err_or_out=2
builtin echo "$_NAME: $1" >&"$std_err_or_out"
exit "${2:-1}"
}
################################################################################
# Validate input.
################################################################################
priority=0
while getopts ":ph" arg; do
case $arg in
p) # priority
priority=1
;;
h) # help
builtin echo "$_HELP_DOC" && exit
;;
*)
builtin echo "$_HELP_DOC" >&2 && exit 64 # EX_USAGE
;;
esac
done
shift $((OPTIND - 1))
[[ "${!#}" == "-p" ]] && priority=1 && set -- "${@:1:$#-1}"
[[ -z "$PUSHOVER_USER" ]] && error "missing environment variable, please set PUSHOVER_USER" 78 # EX_CONFIG
[[ -z "$PUSHOVER_TOKEN" ]] && error "missing environment variable, please set PUSHOVER_TOKEN" 78 # EX_CONFIG
[[ $# -eq 0 ]] && error "missing input, please pass a message" 64 # EX_USAGE
################################################################################
# Execute.
################################################################################
# shellcheck disable=SC2086
response="$(
command curl -qsS $CURL_ARGS \
--form-string "user=$PUSHOVER_USER" \
--form-string "token=$PUSHOVER_TOKEN" \
--form-string "priority=$priority" \
--form-string "message=$*" \
"https://api.pushover.net/1/messages.json"
)" || error "unable to reach pushover.net"
if command grep -q '"user":"invalid"' <<<"$response"; then
error "invalid user, please check the environment variable PUSHOVER_USER" 78 # EX_CONFIG
elif command grep -q '"token":"invalid"' <<<"$response"; then
error "invalid token, please check the environment variable PUSHOVER_TOKEN" 78 # EX_CONFIG
elif ! command grep -q '"status":1' <<<"$response"; then
error "unknown error: $response"
fi