forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shellUtils.sh
105 lines (87 loc) · 2.35 KB
/
shellUtils.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
#!/bin/bash
# Check if GREEN has already been defined
if [ -z "${GREEN+x}" ]; then
declare -r GREEN=$'\e[1;32m'
fi
# Check if RED has already been defined
if [ -z "${RED+x}" ]; then
declare -r RED=$'\e[1;31m'
fi
# Check if BLUE has already been defined
if [ -z "${BLUE+x}" ]; then
declare -r BLUE=$'\e[1;34m'
fi
# Check if TITLE has already been defined
if [ -z "${TITLE+x}" ]; then
declare -r TITLE=$'\e[1;4;34m'
fi
# Check if RESET has already been defined
if [ -z "${RESET+x}" ]; then
declare -r RESET=$'\e[0m'
fi
function success {
echo "🎉 $GREEN$1$RESET"
}
function error {
echo "💥 $RED$1$RESET"
}
function info {
echo "$BLUE$1$RESET"
}
function title {
printf "\n%s%s%s\n" "$TITLE" "$1" "$RESET"
}
function assert_equal {
if [[ "$1" != "$2" ]]; then
error "Assertion failed: $1 is not equal to $2"
exit 1
else
success "Assertion passed: $1 is equal to $1"
fi
}
# Usage: join_by_string <delimiter> ...strings
# example: join_by_string ' + ' 'string 1' 'string 2'
# example: join_by_string ',' "${ARRAY_OF_STRINGS[@]}"
function join_by_string {
local separator="$1"
shift
local first="$1"
shift
printf "%s" "$first" "${@/#/$separator}"
}
# Usage: get_abs_path <path>
# Will make a path absolute, resolving any relative paths
# example: get_abs_path "./foo/bar"
get_abs_path() {
local the_path=$1
local -a path_elements
IFS='/' read -ra path_elements <<< "$the_path"
# If the path is already absolute, start with an empty string.
# We'll prepend the / later when reconstructing the path.
if [[ "$the_path" = /* ]]; then
abs_path=""
else
abs_path="$(pwd)"
fi
# Handle each path element
for element in "${path_elements[@]}"; do
if [ "$element" = "." ] || [ -z "$element" ]; then
continue
elif [ "$element" = ".." ]; then
# Remove the last element from abs_path
abs_path=$(dirname "$abs_path")
else
# Append element to the absolute path
abs_path="${abs_path}/${element}"
fi
done
# Remove any trailing '/'
while [[ $abs_path == */ ]]; do
abs_path=${abs_path%/}
done
# Special case for root
[ -z "$abs_path" ] && abs_path="/"
# Special case to remove any starting '//' when the input path was absolute
abs_path=${abs_path/#\/\//\/}
echo "$abs_path"
}