152 lines
2.7 KiB
Bash
152 lines
2.7 KiB
Bash
# ╭───────────╮
|
|
# │ constants │
|
|
# ╰───────────╯
|
|
|
|
RWX_MAIN_FILE_NAME="main.sh"
|
|
RWX_NAME="rwx"
|
|
|
|
RWX_ROOT_SYSTEM="/usr/local/lib/${RWX_NAME}"
|
|
|
|
# ╭───────────╮
|
|
# │ variables │
|
|
# ╰───────────╯
|
|
|
|
RWX_ROOT_USER="${HOME}/${RWX_NAME}"
|
|
RWX_SHELL="$(cat "/proc/${$}/comm")"
|
|
|
|
# ╭──────────╮
|
|
# │ internal │
|
|
# ╰──────────╯
|
|
|
|
# _RWX_IFS
|
|
|
|
# ╭─────────╮
|
|
# │ private │
|
|
# ╰─────────╯
|
|
|
|
rwx_ifs_set() {
|
|
_RWX_IFS="${IFS}"
|
|
IFS="
|
|
"
|
|
}
|
|
|
|
rwx_ifs_unset() {
|
|
IFS="${_RWX_IFS}"
|
|
unset RWX_IFS
|
|
}
|
|
|
|
_rwx_main_log() {
|
|
if rwx_shell_interactive; then
|
|
[ ${#} -gt 0 ] || set -- ""
|
|
local line
|
|
for line in "${@}"; do
|
|
echo "${line}"
|
|
done
|
|
fi
|
|
}
|
|
|
|
# ╭────────╮
|
|
# │ public │
|
|
# ╰────────╯
|
|
|
|
# find directory’s files by extension
|
|
rwx_find_extension() {
|
|
local extension="${1}"
|
|
local root="${2}"
|
|
local file="${3}"
|
|
set -- \
|
|
"${root}" \
|
|
-name "*.${extension}" \
|
|
-type "f"
|
|
[ -n "${file}" ] &&
|
|
set -- "${@}" \
|
|
-not \
|
|
-name "${file}"
|
|
find "${@}" \
|
|
-printf "%P\n" |
|
|
sort
|
|
}
|
|
|
|
# find directory’s sh files
|
|
rwx_find_sh() {
|
|
rwx_find_extension "sh" "${@}"
|
|
}
|
|
|
|
# get functions from file
|
|
rwx_grep_functions() {
|
|
local file="${1}"
|
|
grep "()" "${file}" |
|
|
cut --delimiter "(" --fields 1
|
|
}
|
|
|
|
# output help message
|
|
rwx_help() {
|
|
rwx_log \
|
|
"rwx_… = functions" \
|
|
" a__… = aliases" \
|
|
" u__… = user"
|
|
}
|
|
|
|
# test if active shell is in interactive mode
|
|
rwx_shell_interactive() {
|
|
case "${-}" in
|
|
*i*) ;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
|
|
rwx_main_source() {
|
|
local path="${1}"
|
|
[ -d "${path}" ] ||
|
|
return 1
|
|
local cmd count module modules
|
|
modules="$(rwx_find_sh "${path}" "${RWX_MAIN_FILE_NAME}")"
|
|
rwx_ifs_set
|
|
count=0
|
|
_rwx_main_log "" \
|
|
". ${path}"
|
|
for module in ${modules}; do
|
|
count=$((count + 1))
|
|
_rwx_main_log "$(printf "%02d" "${count}") ${module%.sh}"
|
|
module="${path}/${module}"
|
|
# shellcheck disable=SC1090
|
|
. "${module}"
|
|
cmd="$(rwx_grep_functions "${module}")"
|
|
if [ -n "${cmd}" ]; then
|
|
[ -n "${CMD}" ] && CMD="${CMD}
|
|
"
|
|
CMD="${CMD}${cmd}"
|
|
fi
|
|
done
|
|
rwx_ifs_unset
|
|
}
|
|
|
|
# ╭──────╮
|
|
# │ main │
|
|
# ╰──────╯
|
|
|
|
# run initial steps
|
|
rwx_main() {
|
|
# system root
|
|
if ! rwx_main_source "${RWX_ROOT_SYSTEM}"; then
|
|
_rwx_main_log "Not a directory: ${RWX_ROOT_SYSTEM}"
|
|
return 1
|
|
fi
|
|
# user root
|
|
rwx_main_source "${RWX_ROOT_USER}"
|
|
# run interactive extras
|
|
if rwx_shell_interactive; then
|
|
# check format
|
|
rwx_log
|
|
rwx_shfmt_check "${RWX_ROOT_SYSTEM}"
|
|
# check syntax
|
|
rwx_log
|
|
rwx_shellcheck_check "${RWX_ROOT_SYSTEM}"
|
|
# help
|
|
rwx_log
|
|
rwx_help
|
|
fi
|
|
}
|
|
|
|
# run main function
|
|
rwx_main
|