Skip to content

Commit d87bfbe

Browse files
committed
Modularize into functions
1 parent 1657b5a commit d87bfbe

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

etc/jhb.conf.sh

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,44 @@
2323

2424
### functions ##################################################################
2525

26-
# Nothing here.
26+
function _get_self_dir
27+
{
28+
echo "$(
29+
cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
30+
pwd
31+
)"
32+
}
2733

28-
### main #######################################################################
34+
function _get_jhb_conf_dirs
35+
{
36+
local conf_dirs
37+
conf_dirs=$(_get_self_dir)/jhb.conf.d
2938

30-
_SELF_DIR=$(
31-
cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
32-
pwd
33-
)
34-
35-
_DIRS=$_SELF_DIR/jhb.conf.d
36-
# shellcheck disable=SC1073 # this is more readable
37-
# iterate through all conf.d directories
38-
for _DIR in "$_SELF_DIR"{/,/../../}*{pre,post}.conf.d; do
39-
# Check if _DIR is actually a valid/existing directory. Otherwise we
40-
# end up adding an unresolved wildcard expression to _DIRS.
41-
if [ -d "$_DIR" ]; then
42-
# Decide if the directory needs to prepended or appended to the list.
43-
if [[ $_DIR = *pre* ]]; then
44-
_DIRS="$_DIR $_DIRS" # prepend
45-
else
46-
_DIRS="$_DIRS $_DIR" # append
39+
# iterate through all conf.d directories
40+
for dir in "$(_get_self_dir)"{/,/../../}*{pre,post}.conf.d; do
41+
# Check if dir is actually a valid/existing directory. Otherwise we
42+
# end up adding an unresolved wildcard expression to conf_dirs.
43+
if [ -d "$dir" ]; then
44+
# Decide if the directory needs to prepended or appended to the list.
45+
if [[ $dir = *pre* ]]; then
46+
conf_dirs="$dir $conf_dirs" # prepend
47+
else
48+
conf_dirs="$conf_dirs $dir" # append
49+
fi
4750
fi
48-
fi
49-
done
51+
done
52+
53+
echo "$conf_dirs"
54+
}
55+
56+
### main #######################################################################
5057

5158
# source items from configuration directories
52-
for _DIR in $_DIRS; do
53-
for _FILE in $("$_SELF_DIR"/../usr/bin/run-parts list "$_DIR"/'*.sh'); do
59+
for DIR in $(_get_jhb_conf_dirs); do
60+
for FILE in $("$(_get_self_dir)"/../usr/bin/run-parts list "$DIR"/'*.sh'); do
5461
# shellcheck disable=SC1090 # can't point to a single source here
55-
source "$_FILE"
62+
source "$FILE"
5663
done
5764
done
5865

59-
unset _FILE _DIR _DIRS
60-
unset _SELF_DIR
66+
unset FILE DIR

0 commit comments

Comments
 (0)