forked from druidfi/git-backupper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.sh
69 lines (54 loc) · 1.35 KB
/
utils.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
#!/bin/bash
DEBUG=${DEBUG:-0}
# The function `run` will exit the script if the given command fails.
run () {
"$@"
status=$?
if [ $status -ne 0 ]; then
echo "ERROR: Encountered error (${status}) while running the following:" >&2
echo " $@" >&2
echo " (at line ${BASH_LINENO[0]} of file $0.)" >&2
echo " Aborting." >&2
exit $status
fi
}
debug () {
NOW=$(date +"%Y-%m-%d %H:%M:%S")
echo -e "🔹 $NOW [DEBUG] $1"
}
info () {
NOW=$(date +"%Y-%m-%d %H:%M:%S")
echo -e "🔸 $NOW [INFO] $1"
}
warning () {
NOW=$(date +"%Y-%m-%d %H:%M:%S")
echo -e "🔸 $NOW [WARNING] $1"
}
success () {
NOW=$(date +"%Y-%m-%d %H:%M:%S")
echo -e "👍 $NOW [SUCCESS] $1"
slack "$1" ":white_check_mark:"
}
error () {
NOW=$(date +"%Y-%m-%d %H:%M:%S")
echo -e "🔺 $NOW [ERROR] $1"
slack "$1" ":warning:"
}
slack () {
# Slack hook is added as ENV variable
SLACK_HOOK="${SLACK_HOOK:-""}"
if [ "$DEBUG" == "1" ]
then
debug "Not flooding Slack in debug mode: $1"
exit 0
elif [ -z "${SLACK_HOOK}" ]
then
info "No Slack webhook supplied. You need to give Slack webhook as an ENV variable SLACK_HOOK."
exit 0
fi
TEXT=$1
EMOJI=$2
MSG="$EMOJI $TEXT"
DATA="$(printf '{"text": "%s"}' "${MSG}" )"
curl -s -o /dev/null -X POST -H 'Content-type: application/json' --data "$DATA" "$SLACK_HOOK"
}