-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy path100-base-libs.sh
More file actions
executable file
·109 lines (89 loc) · 2.25 KB
/
100-base-libs.sh
File metadata and controls
executable file
·109 lines (89 loc) · 2.25 KB
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
#!/usr/bin/env bash
set -e
set -u
set -o pipefail
############################################################
# Functions
############################################################
###
### Log to stdout/stderr
###
log() {
set -o noglob # prevent message from being expanded
local type="${1}" # ok, warn or err
local message="${2}" # msg to print
local debug="${3}" # 0: only warn and error, >0: ok and info
local clr_ok="\033[0;32m"
local clr_info="\033[0;34m"
local clr_warn="\033[0;33m"
local clr_err="\033[0;31m"
local clr_rst="\033[0m"
if [ "${type}" = "ok" ]; then
if [ "${debug}" -gt "0" ]; then
printf "${clr_ok}[OK] %s${clr_rst}\n" "${message}"
fi
elif [ "${type}" = "info" ]; then
if [ "${debug}" -gt "0" ]; then
printf "${clr_info}[INFO] %s${clr_rst}\n" "${message}"
fi
elif [ "${type}" = "warn" ]; then
printf "${clr_warn}[WARN] %s${clr_rst}\n" "${message}" 1>&2 # stdout -> stderr
elif [ "${type}" = "err" ]; then
printf "${clr_err}[ERR] %s${clr_rst}\n" "${message}" 1>&2 # stdout -> stderr
else
printf "${clr_err}[???] %s${clr_rst}\n" "${message}" 1>&2 # stdout -> stderr
fi
set +o noglob
}
export -f log
###
### Wrapper for run_run command
###
run() {
local cmd="${1}" # command to execute
local debug="${2}" # show commands if debug level > 1
local clr_red="\033[0;31m"
local clr_green="\033[0;32m"
local clr_reset="\033[0m"
if [ "${debug}" -gt "1" ]; then
printf "${clr_red}%s \$ ${clr_green}${cmd}${clr_reset}\n" "$( whoami )"
fi
/bin/sh -c "LANG=C LC_ALL=C ${cmd}"
}
export -f run
###
### Is argument a positive integer?
###
isint() {
test -n "${1##*[!0-9]*}"
}
###
### Is env variable set?
###
env_set() {
printenv "${1}" >/dev/null 2>&1
}
export -f env_set
###
### Get env variable by name
###
env_get() {
local env_name="${1}"
# Did we have a default value specified?
if [ "${#}" -gt "1" ]; then
if ! env_set "${env_name}"; then
echo "${2}"
return 0
fi
fi
# Just output the env value
printenv "${1}"
}
export -f env_get
############################################################
# Sanity Checks
############################################################
if ! command -v printenv >/dev/null 2>&1; then
log "err" "printenv not found, but required." "1"
exit 1
fi