-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbash-helpers.sh
117 lines (104 loc) · 2.89 KB
/
bash-helpers.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
#!/usr/bin/env bash
# TODO: get more ideas from: https://github.com/martinburger/bash-common-helpers
blue='\033[0;34m'
nocolor='\033[0m'
red="\033[0;31m"
green="\033[0;32m"
# https://github.com/martinburger/bash-common-helpers/blob/master/bash-common-helpers.sh
function exit_on_error_and_undefined() {
# Will exit script if we would use an uninitialised variable:
set -o nounset
# Will exit script when a simple command (not a control structure) fails:
set -o errexit
}
function test_dep() {
command -v "$1" >/dev/null 2>&1
}
function install_if_not_installed() {
for i in "$@"; do
test_dep "$i" || sudo apt install -y "$i"
done
}
function info() {
echo -e "${blue}------------------------------------------------------------"
echo -e "$*"
echo -e "${nocolor}"
}
function success() {
echo -e "${green}✅ $*${nocolor}"
}
function die() {
if [ "$1" -ge 1 ]; then
echo >&2 -e "${red}❌ ${*:2}${nocolor}"
exit "$1"
fi
}
function json_message() {
if command -v jq >/dev/null 2>&1; then
filter=${3:-tostring}
jq -n \
--arg message "$1" \
--argjson success "[$2]" \
'{"success":$success[0],"message":$message|'"$filter"'}'
else
echo "'jq' is not installed" >&2
fi
}
function json_error() {
json_message "$1" false "$2"
exit 1
}
function json_success() {
json_message "$1" true "$2"
}
#
# USER INTERACTION -------------------------------------------------------------
#
# cmn_ask_to_continue message
#
# Asks the user - using the given message - to either hit 'y/Y' to continue or
# 'n/N' to cancel the script.
#
# Example:
# cmn_ask_to_continue "Do you want to delete the given file?"
#
# On yes (y/Y), the function just returns; on no (n/N), it prints a confirmative
# message to the screen and exits with return code 1 by calling `cmn_die`.
# https://github.com/martinburger/bash-common-helpers/blob/master/bash-common-helpers.sh
#
function ask_to_continue() {
local msg=${1}
local waitingforanswer=true
while ${waitingforanswer}; do
read -p "${msg} (hit 'y/Y' to continue, 'n/N' to cancel) " -r -n 1 ynanswer
case ${ynanswer} in
[Yy])
waitingforanswer=false
break
;;
[Nn])
echo ""
die 1 "Operation cancelled as requested!"
;;
*)
echo ""
echo "Please answer either yes (y/Y) or no (n/N)."
;;
esac
done
echo ""
}
function run_in_script_dir() {
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
cd "${script_dir}" || exit 1
}
function countdownWithNotice() {
local seconds=${1}
local message=${2}
while [ "${seconds}" -gt 0 ]; do
echo -ne "${message} in ${seconds} seconds\033[0K\r"
sleep 1
: $((seconds--))
done
echo -ne "\033[0K\r"
}