Files
scwrypts/zsh/utils/utils.module.zsh
T
wrynegade 7f14edd039 v5.0.0
=====================================================================

Excited to bring V5 to life. This includes some BREAKING CHANGES to
several aspects of ZSH-type scwrypts. Please refer to the readme
for upgrade details (specifically docs/upgrade/v4-to-v5.md)

--- New Features -------------------------

- ZSH testing library with basic mock capabilities

- new scwrypts environment file format includes metadata and more
  advanced features like optional parent env overrides, selection
  inheritence, and improved structurual flexibility

- speedup cache for non-CI runs of ZSH-type scwrypts

- ${scwryptsmodule} syntax now allows a consistent unique-naming
  scheme for functions in ZSH-type scwrypts while providing better
  insight into origin of API calls in other modules

- reusable, case-statement-driven argument parsers in ZSH-type scwrypts

--- Changes ------------------------------

- several utility function renames in ZSH-type scwrypts to improve
  consistency

- documentation comments included in ZSH libraries

- ZSH-type scwrypts now allow library modules to live alongside
  executables
  (zsh/lib still supported; autodetection determines default)

--- Bug Fixes ----------------------------

- hardened environment checking for REQUIRED_ENV variables; this removes
  the ability to overwrite variables in local function contexts
2025-05-24 08:10:33 -06:00

95 lines
2.3 KiB
Bash

#####################################################################
DEPENDENCIES+=(fzf) # (extensible) list of PATH dependencies
REQUIRED_ENV+=() # (extensible) list of required environment variables
#####################################################################
source ${0:a:h}/os.zsh
source ${0:a:h}/parse.zsh
for __UTILS_FILE in \
$(find "${0:a:h}/io" -type f | sort) \
$(find "${0:a:h}/apps" -type f | sort) \
;
do
source "${__UTILS_FILE}"
done
unset __UTILS_FILE
#####################################################################
source ${0:a:h}/dependencies.zsh
source ${0:a:h}/environment.zsh
#####################################################################
utils.check-environment() {
local OPTIONAL=0
while [[ $# -gt 0 ]]
do
case $1 in
( --optional ) OPTIONAL=1 ;;
esac
shift 1
done
local ENVIRONMENT_STATUS=0
utils.dependencies.check-all
local MISSING_DEPENDENCIES=$?
utils.environment.check-all
local MISSING_ENVIRONMENT_VARIABLES=$?
##########################################
local ERROR_MESSAGE=""
[[ ${MISSING_DEPENDENCIES} -ne 0 ]] && {
((ENVIRONMENT_STATUS+=1))
ERROR_MESSAGE+="\n${MISSING_DEPENDENCIES} missing "
[[ ${MISSING_DEPENDENCIES} -eq 1 ]] \
&& ERROR_MESSAGE+='dependency' \
|| ERROR_MESSAGE+='dependencies' \
;
}
[[ ${MISSING_ENVIRONMENT_VARIABLES} -ne 0 ]] && {
((ENVIRONMENT_STATUS+=2))
ERROR_MESSAGE+="\n${MISSING_ENVIRONMENT_VARIABLES} missing environment variable"
[[ ${MISSING_ENVIRONMENT_VARIABLES} -gt 1 ]] && ERROR_MESSAGE+=s
}
[ ${IMPORT_ERRORS} ] && [[ ${IMPORT_ERRORS} -ne 0 ]] && {
((ENVIRONMENT_STATUS+=4))
ERROR_MESSAGE+="\n${IMPORT_ERRORS} import error"
[[ ${IMPORT_ERRORS} -gt 1 ]] && ERROR_MESSAGE+=s
}
##########################################
[[ ENVIRONMENT_STATUS -ne 0 ]] && [[ ${OPTIONAL} -eq 0 ]] && {
ERROR_MESSAGE=$(echo ${ERROR_MESSAGE} | sed '1d; s/^/ /')
${E} "environment errors found (see above)\n${ERROR_MESSAGE}"
}
[[ ${MISSING_ENVIRONMENT_VARIABLES} -ne 0 ]] && [[ ${__SCWRYPT} ]] && {
echo.reminder "
to quickly update missing environment variables, run:
'scwrypts zsh/scwrypts/environment/edit'
"
}
[[ ${ENVIRONMENT_STATUS} -ne 0 ]] && [[ ${NO_EXIT} -ne 1 ]] && [[ ${OPTIONAL} -eq 0 ]] && {
exit ${ENVIRONMENT_STATUS}
}
return ${ENVIRONMENT_STATUS}
}
utils.check-environment