-
Notifications
You must be signed in to change notification settings - Fork 2
/
pushbullet-exit.sh
executable file
·134 lines (116 loc) · 3.63 KB
/
pushbullet-exit.sh
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
# pushbullet-exit.sh by Roger Filmyer.
# MIT license.
# Things to add:
# proper getopts handling
# less hacky way to handle account tokens
# stdin > message body
# help message
# Still can't figure out how to capture the last command and exit status
# PREV_CMD=$(fc -ln "$1" "$1" | sed '1s/^[[:space:]]*//')
# PREV_EXIT=$(echo $?)
# new ACCT_TOKEN handling - look for key in this priority:
# 1. -k flag in command
# 2. PUSHBULLET_ACCT_TOKEN environment variable
# 3. Config file of the other pushbullet shell script
# 4. Working directory: ./api-key (Will be deprecated)
KEY_FILE=~/.config/pushbullet #should mirror pushbullet-bash's location
if [ "$1" == "--help" ]; then
echo "Usage: pushbullet-exit.sh [OPTION] [EXIT CODE]"
echo "Shell script to send a pushbullet notification upon running."
echo "To use the previous command's exit code, use \$?"
echo "Example: make; ./pushbullet-exit -m \"make install complete\" \$?"
echo ""
echo "Options:"
echo "-p Pass through the exit code of the previous program."
echo " Useful for stringing together commands with &&."
echo "-m Puts a custom message in the body of the push notification."
echo "-t Uses specified account token."
exit 0
fi
CAUGHT_EXIT=FALSE
MSG_GIVEN=TRUE #Indicates whether the user has supplied a custom message
#Is the API token in a file?
if [ -r $KEY_FILE ]; then
source $KEY_FILE #Is this safe?
ACCT_TOKEN=$API_KEY #Pushbullet official phrasing vs pushbullet-bash's
elif [ -r ./acct-token ]; then
ACCT_TOKEN=$(cat ./acct-token)
echo "./acct-token lookup is deprecated, will be removed April 1, 2015" >&2
fi
#Check environment variable
if [ -n "$PUSHBULLET_ACCT_TOKEN" ]; then
ACCT_TOKEN=$PUSHBULLET_ACCT_TOKEN
fi
while getopts ":pm:t:" opt; do
case $opt in
p)
PASSTHRU=TRUE
;;
m)
CUSTOM_MESSAGE=("$OPTARG")
MSG_GIVEN=TRUE
shift
;;
t)
ACCT_TOKEN=("$OPTARG")
shift
;;
# [0-255])
# PREV_EXIT=$1
# CAUGHT_EXIT=TRUE
# echo "Caught PREV_EXIT in getopts:" $PREV_EXIT
# ;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
shift
# echo "current args: $@"
OPTIND=1
done
# Fallback: execute with ./pushbullet-exit.sh $? to pass error along
if [ -n "$1" ]; then
if [ "$1" -lt 255 -a "$1" -ge 0 ]; then
PREV_EXIT=$1
CAUGHT_EXIT=TRUE
# echo "Caught PREV_EXIT in fallback:" $PREV_EXIT
fi
fi
if [ "$CAUGHT_EXIT" = FALSE ]; then
TITLE="Command Complete."
BODY="The command has exited."
elif [ "$PREV_EXIT" -eq "0" ]; then
TITLE="Command Successful."
BODY="The command run on your computer has successfully completed."
elif [ "$PREV_EXIT" -lt "255" -a "$PREV_EXIT" -ge "1" ]; then
TITLE="Error "$PREV_EXIT" on command."
BODY="The command run on your computer did not complete successfully."
else
echo "Invalid exit status: $?" >&2
exit 3
fi
if [ "$MSG_GIVEN" = "TRUE" ]; then
BODY=$CUSTOM_MESSAGE
fi
if [ -z "$ACCT_TOKEN" ]; then
echo "Could not find account token" >&2
exit 2
fi
#Assemble the JSON!
JSON='{"type": "note", "title": "'"$TITLE"'", "body": "'"$BODY"'"}'
#Works with V2 of the Pushbullet API.
curl -s -S \
-u $ACCT_TOKEN: \
-X POST https://api.pushbullet.com/v2/pushes\
--header 'Content-Type: application/json' \
--data-binary "$JSON" > /dev/null
if [ "$PASSTHRU" = TRUE ]; then
exit $PREV_EXIT
fi
exit 0