-
Notifications
You must be signed in to change notification settings - Fork 0
/
bashkit.bash
executable file
·206 lines (166 loc) · 5.37 KB
/
bashkit.bash
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env bash
# bashkit.bash --
# wuage bash script devkit
# ----
# (ɔ) 2022 wuage.org
# shellcheck source-path=SCRIPTDIR
# shellcheck enable=require-variable-braces,check-set-e-suppressed
if [[ $0 = "${BASH_SOURCE[0]}" ]]; then
printf '%s is to be sourced not executed!\n' "${BASH_SOURCE[0]}"
exit 1
fi
# bashkit base entry point
bashkit::main() {
# strict-mode http://redsymbol.net/articles/unofficial-bash-strict-mode/
# bashkit does not use 'set -e' but its own errcode handler
set -o nounset
set -o pipefail
IFS=$'\n\t'
# enable DEBUG trap
shopt -u extdebug # errtrace is implied
set -o functrace # functrace must appear after extdebug
# activate aliases expansion globally for errcode
shopt -s expand_aliases
# activate lastpipe globally
# does not work in interactive mode or if job control is
# on (ie. set -m)
shopt -s lastpipe
# activate default_connector_and_and shopt if available
shopt -s default_connector_and_and 2>/dev/null || true
# there's no point in sourcing bashkit without initializing it
bashkit::init "$@"
unset __ # clear $__
}
bashkit::init() {
# already initialized
if declare -p BASHKIT &> /dev/null; then return 0; fi
# check current bash major version because we need printf
# strftime() and EPOCHREALTIME for instance
local major=5
if (( BASH_VERSINFO[0] < major )); then
printf '%s\n' "minimum supported bash version is ${major}.0"
exit 2
fi
init__pathinfo() {
local -n __INFOS=$1;
local src=$2
local dir name full repo
name=${src##*/} # every char after last '/' is the script name
dir=${src%"${name}"} # dir is what's left without name
# resolve symlinks
dir=$( cd "${dir:-.}" && pwd -P ) \
|| fatal "unable to find ${src} basedir"
full=${dir}/${name}
repo=${3:-}
if [[ -z "${repo:-}" ]]; then
repo=$(
git rev-parse --show-toplevel 2>/dev/null \
|| printf ''
)
fi
__INFOS=(
[basedir]=${dir} # /absolute_path
[fullname]=${full} # /absolute_path/bashkit.bash
[filename]=${name%.*} # bashkit
[suffix]=${name##*.} # bash
[repository]=${repo} # /repository
)
}
# global variables
declare -g -A BASHKIT
init__pathinfo BASHKIT "${BASH_SOURCE[0]}" "${BASHKIT_REPOSITORY:-}"
declare -g -A SCRIPT
init__pathinfo SCRIPT "$0"
# bashkit modules path, can be modified
modpath=${BASHKIT[basedir]}/core':'${BASHKIT[basedir]}/modules
BASHKIT_MODPATH=${BASHKIT_MODPATH:-}${BASHKIT_MODPATH:+:}${modpath}
# load core !!! order matters !!!
local core=( cf trap errcode error color logging version)
case ${OSTYPE} in
darwin*) core+=( darwin );;
*) ;;
esac
bashkit::load "${core[@]}" \
|| fatal 'failed to load core modules'
# this out of order cleanup is ok
cleanup 'unset SCRIPT BASHKIT'
# load every other module requested from invocation
bashkit::load "$@"
debug done! # logging is loaded now!
}
bashkit::help() {
version::bashkit
printf '%s\n' \
"${SCRIPT[filename]} -- bashkit ${__}" \
"Usage: ${__usage:-undefined usage}" \
""
if [[ "${__help:=undefined help}" ]]; then
printf '%s\n' \
"${__help}" \
""
fi
if (( $# > 0 )); then
printf '%s\n' \
" $*" \
""
fi
exit 2
}
bashkit__modcache() {
case $1 in
get)
[[ -v BASHKIT[mods] && ${BASHKIT[mods]} = *$2* ]]
;;
set)
local newmod=$2 concat
printf -v concat '%s' \
"${BASHKIT[mods]:-}${BASHKIT[mods]:+ }${newmod}"
BASHKIT[mods]=${concat}
;;
*) return 1 ;;
esac
}
bashkit::load() {
local mod path file mods=( "$@" )
shift ${#@} # prevent arg sourcing
for mod in "${mods[@]}"; do
mod=${mod%.bash}
# assert new module request or discard
! bashkit__modcache get "${mod}" \
|| continue # mod loop
while read -d ':' -r path; do
file=${path}/${mod}.bash
[[ -f "${file}" ]] \
|| continue # resume path loop
# shellcheck source=/dev/null
source "${file}" \
|| fatal "can not source ${mod}"
# remove relative path if any
local modinit=${mod##*/}::init
if declare -f "${modinit}" &> /dev/null; then
"${modinit}"
fi
bashkit__modcache set "${mod}"
continue 2 # done! resume mod loop
done < <( printf '%s' "${BASHKIT_MODPATH}:" )
fatal "module not found: ${mod}"
done
}
# fatal can be used before module logging is loaded
declare -f fatal &> /dev/null \
|| fatal() { printf '%s\n' "${@:-${__:-fatal}}" >&2; exit 1; }
: sanitize source args
{
__BASHKIT_ARGV=( "$@" )
__shopts=$( shopt -p ) # save shopt
{
shopt -s extdebug # create BASH_ARGV
# `source bashkit.bash` has no arg
if [[ ${BASH_ARGV[0]:-} == "${BASH_SOURCE[0]:-}" ]]; then
__BASHKIT_ARGV=() # clear `${__BASHKIT_ARGV[@]}`
fi
}
eval "${__shopts}" && unset __shopts # restore shopt
} 2>/dev/null
bashkit::main "${__BASHKIT_ARGV[@]}" || fatal
unset __BASHKIT_ARGV