rwx/sh/main.sh

155 lines
2.7 KiB
Bash
Raw Normal View History

2024-11-29 14:45:32 +00:00
# ╭───────────╮
# │ constants │
# ╰───────────╯
2024-11-25 16:32:44 +00:00
2024-11-29 21:26:26 +00:00
RWX_MAIN_NAME="main.sh"
2024-11-29 21:04:00 +00:00
RWX_NAME="rwx"
2024-11-18 10:41:12 +00:00
2024-11-29 20:47:36 +00:00
RWX_ROOT_SYSTEM="/usr/local/lib/${RWX_NAME}"
2024-11-18 10:41:12 +00:00
2024-11-29 14:45:32 +00:00
# ╭───────────╮
# │ variables │
# ╰───────────╯
2024-11-25 16:32:44 +00:00
2024-11-29 18:41:07 +00:00
RWX_ROOT_USER="${HOME}/${RWX_NAME}"
2024-11-29 20:47:36 +00:00
RWX_SHELL="$(cat "/proc/${$}/comm")"
2024-11-25 16:32:44 +00:00
2024-11-29 21:26:26 +00:00
RWX_MAIN_PATH="${RWX_ROOT_SYSTEM}/${RWX_MAIN_NAME}"
2024-11-29 18:25:49 +00:00
# ╭──────────╮
# │ internal │
# ╰──────────╯
# _RWX_IFS
2024-11-29 14:45:32 +00:00
# ╭─────────╮
# │ private │
# ╰─────────╯
2024-11-15 15:43:48 +00:00
2024-11-29 18:25:49 +00:00
rwx_ifs_set() {
_RWX_IFS="${IFS}"
2024-11-25 15:38:48 +00:00
IFS="
"
}
2024-11-29 18:25:49 +00:00
rwx_ifs_unset() {
IFS="${_RWX_IFS}"
unset RWX_IFS
2024-11-25 15:38:48 +00:00
}
2024-11-29 17:46:52 +00:00
_rwx_main_log() {
2024-11-29 17:48:21 +00:00
if rwx_shell_interactive; then
2024-11-25 17:33:23 +00:00
[ ${#} -gt 0 ] || set -- ""
local line
for line in "${@}"; do
echo "${line}"
done
2024-11-29 11:01:01 +00:00
fi
2024-11-25 17:33:23 +00:00
}
2024-11-29 14:45:32 +00:00
# ╭────────╮
# │ public │
# ╰────────╯
2024-11-25 17:39:25 +00:00
2024-11-29 14:42:58 +00:00
# find directory’s files by extension
2024-11-29 18:25:49 +00:00
rwx_find_extension() {
2024-11-25 19:46:09 +00:00
local extension="${1}"
local root="${2}"
local file="${3}"
2024-11-25 19:40:57 +00:00
set -- \
"${root}" \
2024-11-25 19:46:09 +00:00
-name "*.${extension}" \
2024-11-25 19:40:57 +00:00
-type "f"
[ -n "${file}" ] &&
set -- "${@}" \
-not \
-name "${file}"
find "${@}" \
-printf "%P\n" |
sort
}
2024-11-29 14:42:58 +00:00
# find directory’s sh files
2024-11-29 18:25:49 +00:00
rwx_find_sh() {
rwx_find_extension "sh" "${@}"
2024-11-25 19:46:09 +00:00
}
2024-11-29 14:42:58 +00:00
# get functions from file
2024-11-29 18:25:49 +00:00
rwx_grep_functions() {
2024-11-25 20:00:14 +00:00
local file="${1}"
grep "()" "${file}" |
cut --delimiter "(" --fields 1
}
2024-11-29 14:42:58 +00:00
# output help message
2024-11-29 16:15:09 +00:00
rwx_help() {
2024-11-29 18:25:49 +00:00
rwx_log \
"rwx_… = functions" \
" a__… = aliases" \
" u__… = user"
2024-11-21 10:25:40 +00:00
}
2024-11-19 11:32:29 +00:00
2024-11-29 14:42:58 +00:00
# test if active shell is in interactive mode
2024-11-29 17:48:21 +00:00
rwx_shell_interactive() {
2024-11-29 10:55:03 +00:00
case "${-}" in
*i*) ;;
*) return 1 ;;
esac
}
2024-11-29 17:31:49 +00:00
rwx_main_source() {
2024-11-25 21:21:01 +00:00
local path="${1}"
[ -d "${path}" ] ||
return 1
local cmd count module modules
2024-11-29 21:26:26 +00:00
modules="$(rwx_find_sh "${path}" "${RWX_MAIN_NAME}")"
2024-11-29 18:25:49 +00:00
rwx_ifs_set
2024-11-25 21:21:01 +00:00
count=0
2024-11-29 17:46:52 +00:00
_rwx_main_log "" \
2024-11-25 21:21:01 +00:00
". ${path}"
for module in ${modules}; do
count=$((count + 1))
2024-11-29 17:46:52 +00:00
_rwx_main_log "$(printf "%02d" "${count}") ${module%.sh}"
2024-11-25 21:21:01 +00:00
module="${path}/${module}"
# shellcheck disable=SC1090
. "${module}"
2024-11-29 18:25:49 +00:00
cmd="$(rwx_grep_functions "${module}")"
2024-11-25 21:21:01 +00:00
if [ -n "${cmd}" ]; then
[ -n "${CMD}" ] && CMD="${CMD}
"
CMD="${CMD}${cmd}"
fi
done
2024-11-29 18:25:49 +00:00
rwx_ifs_unset
2024-11-25 21:21:01 +00:00
}
2024-11-29 14:45:32 +00:00
# ╭──────╮
# │ main │
# ╰──────╯
2024-11-25 21:21:01 +00:00
2024-11-29 14:42:58 +00:00
# run initial steps
2024-11-29 16:13:51 +00:00
rwx_main() {
2024-11-29 14:42:58 +00:00
# system root
2024-11-29 18:41:07 +00:00
if ! rwx_main_source "${RWX_ROOT_SYSTEM}"; then
_rwx_main_log "Not a directory: ${RWX_ROOT_SYSTEM}"
2024-11-25 17:51:24 +00:00
return 1
fi
2024-11-29 14:42:58 +00:00
# user root
2024-11-29 18:41:07 +00:00
rwx_main_source "${RWX_ROOT_USER}"
2024-11-29 14:42:58 +00:00
# run interactive extras
2024-11-29 17:48:21 +00:00
if rwx_shell_interactive; then
2024-11-29 16:20:38 +00:00
# check format
2024-11-29 18:25:49 +00:00
rwx_log
2024-11-29 18:41:07 +00:00
rwx_shfmt_check "${RWX_ROOT_SYSTEM}"
2024-11-29 16:20:38 +00:00
# check syntax
2024-11-29 18:25:49 +00:00
rwx_log
2024-11-29 18:41:07 +00:00
rwx_shellcheck_check "${RWX_ROOT_SYSTEM}"
2024-11-29 14:42:58 +00:00
# help
2024-11-29 18:25:49 +00:00
rwx_log
2024-11-29 16:15:09 +00:00
rwx_help
2024-11-29 11:01:01 +00:00
fi
2024-11-25 16:57:41 +00:00
}
2024-11-29 14:42:58 +00:00
# run main function
2024-11-29 16:13:51 +00:00
rwx_main