forked from EESSI/software-layer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.sh
98 lines (88 loc) · 2.16 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
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
function echo_green() {
echo -e "\e[32m$1\e[0m"
}
function echo_red() {
echo -e "\e[31m$1\e[0m"
}
function echo_yellow() {
echo -e "\e[33m$1\e[0m"
}
ANY_ERROR_EXITCODE=1
function fatal_error() {
echo_red "ERROR: $1" >&2
if [[ $# -gt 1 ]]; then
exit $2
else
exit "${ANY_ERROR_EXITCODE}"
fi
}
function check_exit_code {
ec=$1
ok_msg=$2
fail_msg=$3
if [[ $ec -eq 0 ]]; then
echo_green "${ok_msg}"
else
fatal_error "${fail_msg}"
fi
}
function get_path_for_tool {
tool_name=$1
tool_envvar_name=$2
which_out=$(which ${tool_name} 2>&1)
exit_code=$?
if [[ ${exit_code} -eq 0 ]]; then
echo "INFO: found tool ${tool_name} in PATH (${which_out})" >&2
echo "${which_out}"
return 0
fi
if [[ -z "${tool_envvar_name}" ]]; then
msg="no env var holding the full path to tool '${tool_name}' provided"
echo "${msg}" >&2
return 1
else
tool_envvar_value=${!tool_envvar_name}
if [[ -x "${tool_envvar_value}" ]]; then
msg="INFO: found tool ${tool_envvar_value} via env var ${tool_envvar_name}"
echo "${msg}" >&2
echo "${tool_envvar_value}"
return 0
else
msg="ERROR: tool '${tool_name}' not in PATH\n"
msg+="ERROR: tool '${tool_envvar_value}' via '${tool_envvar_name}' not in PATH"
echo "${msg}" >&2
echo ""
return 2
fi
fi
}
function get_host_from_url {
url=$1
re="(http|https)://([^/:]+)"
if [[ $url =~ $re ]]; then
echo ${BASH_REMATCH[2]}
return 0
else
echo ""
return 1
fi
}
function get_port_from_url {
url=$1
re="(http|https)://[^:]+:([0-9]+)"
if [[ $url =~ $re ]]; then
echo ${BASH_REMATCH[2]}
return 0
else
echo ""
return 1
fi
}
function get_ipv4_address {
hname=$1
hipv4=$(grep ${hname} /etc/hosts | grep -v '^[[:space:]]*#' | cut -d ' ' -f 1)
# TODO try other methods if the one above does not work --> tool that verifies
# what method can be used?
echo "${hipv4}"
return 0
}