shell-base/modules/module-extra.lib

100 lines
2.9 KiB
Plaintext
Raw Permalink Normal View History

#!/bin/bash
# additional module functions. Only need these explicitly
module_print() {
[ ! $1 ] && echo "no module specified" && return 1
# (return 0 2>/dev/null) && echo "module_load was sourced" || echo "module_log was executed"
local FILE
FILE=$(module_find $1)
[ $? -ne 0 ] && echo no module $1 found && return 1
cat $FILE
return 0
}
module_cp() {
[ ! $1 ] && echo "no module specified" && return 1
local FILE
FILE=$(module_find $1)
[ $? -ne 0 ] && echo no module $1 found && return 1
echo cp $FILE ${2:-$(pwd)}
cp $FILE ${2:-$(pwd)}
return 0
}
module_loaded() {
[ ! $1 ] && echo "no module specified" && return 1
local func
local ret
local file
ret=0
# function_list | grep module
file=$(module_find $1)
[ ! $file ] && echo no module $1 found && return 1
# echo list "$(function_list $file)"
for func in $(function_list $file)
do
[[ ! $(function_list | grep $func) ]] && ret=1
[[ $2 = -vv ]] && ([[ $ret = 0 ]] && echo "$func loaded" || echo "$func not loaded")
done
[[ $2 = -v ]] && echo "module: $1" && ([[ $ret = 0 ]] && echo "all functions " || echo "some or all functions not loaded")
[[ $ret = 0 ]] && echo "loaded"
return $ret
}
module_functions () {
if module_find $1 >/dev/null ; then
module_print $1 | grep -E '^[[:space:]]*([[:alnum:]_]+[[:space:]]*\(\)|function[[:space:]]+[[:alnum:]_]+)' \
| sed 's/[[:space:]]*//g' | cut -d '(' -f 1
fi
}
module_search() {
[ ! $1 ] && echo "no module search string" && return 1
local MDIRS
local MDIR
local DIRS
local NAME=$1
# 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
# the shell system. These take precedence over any modules found in shell directories below.
# Precedence is first in list to last.
if [[ $MODULE_DIRS ]]; then
for DIR in "${MODULE_DIRS[@]}"
do
if [[ -d $DIR ]]; then
echo custom modules
# echo $DIR/$NAME
# ls $DIR/$SEARCH
# [ $? -eq 0 ] && echo $RES && return 0
fi
done
fi
# 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
DIRS=( ${BASH_SHELL_DIRS:-$BASH_SHELL_BASE} ${BASH_SHELL_NETWORKS_LOADED} )
[ -d BASH_SHELL_USER_DIR ] && DIRS=("${DIRS[@]}" "BASH_SHELL_USER_DIR")
[ -d $BASH_SHELL_DEV ] && DIRS=("${DIRS[@]}" "$BASH_SHELL_DEV")
cnt=${#DIRS[@]}
for ((i=1;i<=cnt;i++)); do
# find modules in reverse order so more specific ones override
DIR="${DIRS[cnt-i]}/modules"
if [[ -d $DIR ]]; then
echo search: "$DIR/$NAME"
FILES="$(command find -L ${DIR} -type f -iname "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh -o -name "${NAME}".env)"
echo "$FILES"
# [ $? -eq 0 ] && echo $RES && return 0
fi
done
# no module found anywhere
return 1
}