-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbashful.inc.sh
181 lines (146 loc) · 4.03 KB
/
bashful.inc.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash
# Bashful is copyright 2009-2016 Dejay Clayton, all rights reserved:
# https://github.com/dejayc/bashful
# Bashful is licensed under the 2-Clause BSD License:
# http://opensource.org/licenses/BSD-2-Clause
# Verify caller context.
[[ "${BASH_ARGV}" != '' ]] || {
echo "ERROR: ${BASH_SOURCE[0]##*/} must be sourced, not executed"
exit 1
} >&2
# Exit if Bashful was previously included.
[[ -z "${BASHFUL_VERSION:-}" ]] || return 0
# Initialize global variables.
{
# Bashful information.
declare BASHFUL_VERSION='1.0'
declare BASHFUL_LOADED_bashful='bashful'
# Script information.
declare BASHFUL_PATH="${BASH_SOURCE[0]}"
if [[ "${BASHFUL_PATH}" =~ / ]]
then
BASHFUL_PATH="$( cd "${BASHFUL_PATH%/*}" && pwd )"
else
BASHFUL_PATH="$( pwd )"
fi
declare SCRIPT_INVOKED_NAME="${BASH_SOURCE[${#BASH_SOURCE[@]}-1]}"
declare SCRIPT_NAME="${SCRIPT_INVOKED_NAME##*/}"
declare SCRIPT_INVOKED_PATH="$( dirname "${SCRIPT_INVOKED_NAME}" )"
declare SCRIPT_PATH="$( cd "${SCRIPT_INVOKED_PATH}"; pwd )"
declare SCRIPT_RUN_DATE="$( date )"
# Script debugging level.
declare -i SCRIPT_DEBUG_LEVEL=0
}
# Returns true if the current debugging level is greater than or equal to the
# specified debugging level.
function ifDebug()
{
local REQUIRED_DEBUG_LEVEL="${1-1}"
[[ "${SCRIPT_DEBUG_LEVEL-0}" -ge ${REQUIRED_DEBUG_LEVEL} ]]
}
function indexOf()
{
local FIND="${1-}"
declare -i I=2
declare -i N=${#@}
while [ ${I} -le ${N} ]
do
[[ "${!I}" == "${FIND}" ]] && {
echo -n $(( I - 2 ))
return
}
let I+=1
done
false
}
function isFunction()
{
local FUNCTION_NAME="${1-}"
[[ -n "${FUNCTION_NAME}" ]] && declare -f "${FUNCTION_NAME}" > /dev/null
}
function isModuleLoaded()
{
local MODULE="${1-}"
[[ -z "${MODULE}" ]] || isVariableSet "BASHFUL_LOADED_${MODULE}"
}
function isScriptInteractive()
{
[[ "${-}" =~ 'i' ]]
}
function isScriptSourced()
{
[[ "${BASH_ARGV}" != '' ]]
}
function isScriptSshCommand()
{
[[ "$(ps -o comm= -p $PPID)" =~ 'sshd' ]]
}
function isVariableSet()
{
local VAR_NAME="${1-}"
[[ -n "${VAR_NAME}" && ! -z "${!VAR_NAME+x}" ]];
}
function stderr()
{
declare -i ERR_CODE="${1-$(( ${?} > 0 ? ${?} : 2 ))}"
stdout >&2
return ${ERR_CODE}
}
# If debugging is enabled, output the specified text to STDERR.
function stderr_ifDebug()
{
declare -i STATUS_CODE="${2-${?}}"
local REQUIRED_DEBUG_LEVEL="${1-1}"
[[ "${SCRIPT_DEBUG_LEVEL-0}" -ge ${REQUIRED_DEBUG_LEVEL} ]] && \
stderr ${STATUS_CODE}
}
function stdout()
{
local LINE
IFS='' read -r -d '' LINE
echo -n "${LINE}"
}
# If debugging is enabled, output the specified text to STDOUT.
function stdout_ifDebug()
{
local REQUIRED_DEBUG_LEVEL="${1-1}"
[[ "${SCRIPT_DEBUG_LEVEL-0}" -ge ${REQUIRED_DEBUG_LEVEL} ]] && stdout
}
# Verifies that all required module dependencies are loaded, or generates an
# error otherwise.
function verifyBashfulDependencies()
{
local VAR_LIST="$( compgen -v )"
declare -a MODULES=()
while [[ "${VAR_LIST}" =~ \
(^|[[:space:]])BASHFUL_LOADED_([^[:space:]]+)(.*)$ ]]
do
MODULES[${#MODULES[@]}]="${BASH_REMATCH[2]}"
VAR_LIST="${BASH_REMATCH[3]}"
done
declare -i MODULE_LEN=${#MODULES[@]}
declare -i I=0
declare -i MODULE_MISSING=0
while [ ${I} -lt ${MODULE_LEN} ]
do
local MODULE="${MODULES[I]}"
let I++ ||:
local DEPS_VAR="BASHFUL_DEPS_${MODULE}"
isVariableSet "${DEPS_VAR}" || continue
unset DEPS
declare -a DEPS="( ${!DEPS_VAR} )"
declare -i DEPS_LEN=${#DEPS[@]}
declare -i J=0
while [ ${J} -lt ${DEPS_LEN} ]
do
local DEP="${DEPS[J]}"
let J++ ||:
isModuleLoaded "${DEP}" || {
let MODULE_MISSING=1
echo \
"Bashful module '${MODULE}' requires missing module '${DEP}'" | stderr
}
done
done
return ${MODULE_MISSING}
}