2020-11-06 14:24:10 -08:00
|
|
|
#!/bin/bash
|
2023-12-17 12:54:45 -08:00
|
|
|
|
2020-11-20 08:48:48 -08:00
|
|
|
# echo loading module library
|
2021-02-05 13:00:57 -08:00
|
|
|
|
2023-01-05 12:36:23 -08:00
|
|
|
# source with set -a and set +a to have these bootstrap functions available in the environment
|
2021-02-05 13:00:57 -08:00
|
|
|
module_confirm() {
|
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
local FILE
|
|
|
|
local DIR
|
|
|
|
local NAME
|
|
|
|
DIR=$1
|
|
|
|
NAME=$2
|
2023-12-14 21:09:19 -08:00
|
|
|
|
2022-03-28 10:02:17 -07:00
|
|
|
# TODO use file instead for valid extensions
|
2023-12-17 12:54:45 -08:00
|
|
|
# will look for valid module extensions and also look for duplicate modules with a directory which is not allowed
|
2022-03-28 10:02:17 -07:00
|
|
|
FILE="$(command find -L ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh -o -name "${NAME}".env)"
|
2020-11-06 14:24:10 -08:00
|
|
|
# echo files found $FILE
|
|
|
|
COUNT=0
|
|
|
|
if [ "$FILE" ]; then
|
|
|
|
COUNT=$(echo $FILE | xargs -n 1 | wc -l)
|
|
|
|
fi
|
|
|
|
if [ $COUNT -gt 1 ]; then
|
2024-02-21 11:11:07 -08:00
|
|
|
>&2 echo two or more modules of same name found, aborting
|
|
|
|
>&2 echo $FILE | xargs -n 1
|
2020-11-06 14:24:10 -08:00
|
|
|
return 1
|
|
|
|
fi
|
2024-02-21 11:11:07 -08:00
|
|
|
[[ $COUNT == 1 ]] && echo $FILE
|
2020-11-06 14:24:10 -08:00
|
|
|
}
|
|
|
|
|
2023-12-17 12:54:45 -08:00
|
|
|
# Returns path to module if succesful
|
2021-02-05 13:00:57 -08:00
|
|
|
module_find() {
|
2020-11-06 14:24:10 -08:00
|
|
|
|
2024-02-21 11:11:07 -08:00
|
|
|
[ ! $1 ] && >&2 echo "no module specified" && return 1
|
2020-11-06 14:24:10 -08:00
|
|
|
|
|
|
|
local MDIRS
|
|
|
|
local MDIR
|
|
|
|
local DIRS
|
|
|
|
local MODULE=$1
|
|
|
|
|
2022-02-20 12:44:29 -08:00
|
|
|
# MODULE_DIRS is array set in the environment and is an array of additional directories to
|
|
|
|
# search for modules. This makes is easy to include a custom module libraries outside
|
2022-03-09 17:02:39 -08:00
|
|
|
# the shell system. These take precedence over any modules found in shell directories below.
|
|
|
|
# Precedence is first in list to last.
|
2020-11-06 14:24:10 -08:00
|
|
|
|
2022-02-20 12:44:29 -08:00
|
|
|
# BASH_SHELL_DIRS holds shell directories to search for modules. If will be searched
|
|
|
|
# in revsere order, user or network repos over host over base
|
2023-12-17 12:54:45 -08:00
|
|
|
|
2023-01-05 12:36:23 -08:00
|
|
|
DIRS=( ${BASH_SHELL_DIRS:-$BASH_SHELL_BASE} ${BASH_SHELL_NETWORKS_LOADED} )
|
2022-12-30 09:18:39 -08:00
|
|
|
[ -d BASH_SHELL_USER_DIR ] && DIRS=("${DIRS[@]}" "BASH_SHELL_USER_DIR")
|
2022-03-09 17:02:39 -08:00
|
|
|
[ -d $BASH_SHELL_DEV ] && DIRS=("${DIRS[@]}" "$BASH_SHELL_DEV")
|
2023-12-17 12:54:45 -08:00
|
|
|
# echo "before: ${DIRS[@]}";
|
|
|
|
DIRS=("${DIRS[@]/%/\/modules}")
|
|
|
|
DIRS=("${MODULE_DIRS[@]}" "${DIRS[@]}")
|
|
|
|
# echo "after: ${DIRS[@]}"
|
2020-11-06 14:24:10 -08:00
|
|
|
|
2022-02-20 12:44:29 -08:00
|
|
|
cnt=${#DIRS[@]}
|
|
|
|
for ((i=1;i<=cnt;i++)); do
|
|
|
|
# find modules in reverse order so more specific ones override
|
2023-12-17 12:54:45 -08:00
|
|
|
DIR="${DIRS[cnt-i]}"
|
2022-02-20 12:44:29 -08:00
|
|
|
if [[ -d $DIR ]]; then
|
2023-12-14 21:09:19 -08:00
|
|
|
# echo $DIR $MODULE
|
2022-02-20 12:44:29 -08:00
|
|
|
RES=$(module_confirm "$DIR" "$MODULE")
|
2020-11-06 14:24:10 -08:00
|
|
|
[ $? -eq 0 ] && echo $RES && return 0
|
2022-02-20 12:44:29 -08:00
|
|
|
fi
|
2020-11-06 14:24:10 -08:00
|
|
|
done
|
2024-02-21 11:11:07 -08:00
|
|
|
>&2 echo no module found anywhere
|
2020-11-06 14:24:10 -08:00
|
|
|
return 1
|
2022-02-20 12:44:29 -08:00
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
}
|
|
|
|
|
2021-02-05 13:00:57 -08:00
|
|
|
module_load() {
|
2024-02-21 11:11:07 -08:00
|
|
|
local module;local mpath
|
|
|
|
[ ! $1 ] && >&2 echo "no module specified" && return 1
|
2020-11-06 14:24:10 -08:00
|
|
|
local FILE
|
2024-02-21 11:11:07 -08:00
|
|
|
for module in "$@"
|
|
|
|
do
|
|
|
|
# is there way to not reload modules??
|
|
|
|
# if [[ $(eval "echo \"\$MODULE_$module_LOADED\"") ]]; then
|
|
|
|
# echo $module already loaded
|
|
|
|
# else
|
|
|
|
eval "_${module}_ () { echo "$fname test"; }"
|
|
|
|
mpath=$(module_find $module)
|
|
|
|
[ $? -ne 0 ] && >&2 echo extreme warning: no module $1 found, so was not loaded
|
|
|
|
source $mpath "$@"
|
|
|
|
# eval "MODULE_$module_LOADED=true"
|
|
|
|
# fi
|
|
|
|
done
|
2020-11-06 14:24:10 -08:00
|
|
|
}
|
|
|
|
|
2023-01-05 12:36:23 -08:00
|
|
|
# put here so it's globally available
|
|
|
|
# runs a script but loads the current interactive shell environment, not recommended unless there is a need
|
|
|
|
# mostly for testing. Better to source some part of the shell as needed using shell-process-directory module
|
|
|
|
shell_run () {
|
|
|
|
bash -ci ". ${1}"
|
|
|
|
}
|