-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils
executable file
·74 lines (64 loc) · 1.58 KB
/
utils
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
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# Copyright (C) 2023 Utsav Balar
# Copyright (C) 2023 Vicharak Computers LLP
# Error handler for this script to exit on error
function err_handler() {
ret=$?
[ "${ret}" -eq 0 ] && return
printf "\e[1;31mError: Running %s failed!\e[0m\n" "${FUNCNAME[1]}"
printf "\e[1;31mError: exit code %s from line %s: %s\e[0m\n" "${ret}" "${BASH_LINENO[0]}" "${BASH_COMMAND}"
exit "${ret}"
}
trap 'err_handler' ERR
# Finish build and exit
function finish_build() {
print "Running ${FUNCNAME[1]} succeeded."
cd "${SCRIPT_DIR}" || exit
}
# Prints the string in bold yellow color if the line is a header
# if the line is not a header then it prints the string in bold green color
function print() {
# check if the line is a header or not
if [[ $1 == *"---"* ]]; then
echo -e "\e[1;33m${1}\e[0m"
return
fi
echo -e "\e[1;32m ${1}\e[0m"
}
# Exit with error message
function exit_with_error() {
print "----------------------------------------------------------------"
print "Build failed because ${1}"
print "----------------------------------------------------------------"
cd "${SCRIPT_DIR}" || exit
exit "${2:-1}"
}
# Delete file if exists
function delete_file() {
if [ -f "${1}" ]; then
rm -f "${1}"
fi
}
# Delete directory if exists
function delete_dir() {
if [ -d "${1}" ]; then
rm -rf "${1}"
fi
}
# Check if $1 is set
function is_set() {
if [ -n "${1}" ]; then
return 0
else
return 1
fi
}
# check if $1 is true or $1 is empty
function is_enabled() {
if is_set "${1}" && [ "${1}" = "true" ]; then
return 0
else
return 1
fi
}