bash-libs/import.sh

51 lines
1.5 KiB
Bash
Raw Normal View History

2024-06-01 21:57:13 +00:00
declare LIB_PATH=/opt/bash-lib/
########################
# Function: dep_exists
# Usage: You provide it a utility name and it checks to see if it can find an
# executable of the same name in path
function dep_exists () {
local DEP_UTIL=${1:?"$FUNCNAME: No dep utility was provided for search"}
for path in ${PATH//:/ }; do
if [[ -x $path/$DEP_UTIL ]]; then
return 0
fi
done
# The above is just more bash friendly since it can be error handled better
## OR
# find ${PATH//:/ } -name $DEP_UTIL -executable | grep $DEP_UTIL
# return $?
echo "$FUNCNAME: $DEP_UTIL is not installed, or is not in path"
return 1
}
####################
# Function: import
# Usage: You provide the script/lib name that you want to import from the LIB_PATH
function import () {
local LIB_NAME=${1:?"$FUNCNAME: No lib provided for import"}
## Fixing lib_name in case user/dev provides a non *.sh ending script (as all should have extension)
if [[ ${LIB_NAME##*.} != 'sh' ]]; then
LIB_NAME=${LIB_NAME}.sh
fi
# Creating REGEX pattern to match if lib has already been sourced
local SOURCED_LIBS_REGEX="($( IFS='|'; echo "${BASH_SOURCE[*]}" ))"
if [[ $LIB_NAME =~ ^$SOURCED_LIBS_REGEX$ ]]; then
echo "$FUNCNAME: This lib ($LIB_NAME) has already been sourced"
return 0
fi
## Getting FULL PATH for convinence
FULLPATH=$LIB_PATH/$LIB_NAME
if [[ -f $FULLPATH ]]; then
. $FULLPATH
else
echo "$FUNCNAME: $LIB_NAME ($FULLPATH) does not exist or is not accessible by you."
exit
fi
}