scwrypts/zsh/lib/import.driver.zsh
yage 3fe01a7263 v4.4.0
=====================================================================

Increased non-scwrypts-runtime compatibility and improved clarity
in user environments after sourcing the scwrypts.plugin.zsh.

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

- added experimental support for --output json
- added 'scwrypts --list-groups' to output the SCWRYPTS_GROUPS value
- added 'scwrypts --config' to be `eval`-ed in non-scwrypts-runtime zsh

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

- removed config variables which pertained to old scwrypts

- removed deprecated --no-log

- cleaned up environment requirements and improved import saftey for
  scwrypts.plugin.zsh; scwrypts is now *required* on $PATH in order to work

- refactored group configuration to match external group configuration
  (configuration now in scwrypts.scwrypts.zsh rather than zsh/lib/config.group.zsh)

- plugins/kubectl now forces an unalias of `f` (for fluxcd) on load

- the 'use' command now supports the '-c' short flag for ease of quick
  use

- upgraded max supported python version to 3.12; dropped support for
  python 3.9 (>3.10 required)

- remove old references to SCWRYPTS_ROOT in favour of SCWRYPTS_ROOT__scwrypts

- SCWRYPTS_LOG_LEVEL setting is now forwarded when using the SCWRYPTS__RUN
  meta execution function
2024-05-08 23:11:09 -06:00

157 lines
5.4 KiB
Bash

[[ $SCWRYPTS_IMPORT_DRIVER_READY -eq 1 ]] && return 0
###################################################################
# #
# usage: use [OPTIONS ...] zsh/module/path #
# #
###################################################################
# #
# OPTIONS: #
# #
# -g, --group lookup library root from friendly group #
# name (requires configuration) #
# (default: scwrypts) #
# #
# -r, --library-root fully qualified path to a library root #
# #
# --check-environment check environment immediately rather than #
# wait for downstream CHECK_ENVIRONMENT call #
# #
# #
# Allows for import-style library loading in zsh. No matter what #
# scwrypt is run, this function (and required helpers) are *also* #
# loaded, ensuring that 'use' is always available in scwrypts #
# context. #
# #
# #
# Friendly group-names can be configured by setting the variable #
# 'SCWRYPTS_LIBRARY_ROOT__<group-name>' to the fully qualified path #
# to the root directory of the modules library. #
# #
# #
###################################################################
source "${0:a:h}/config.zsh"
use() {
local SCWRYPTS_LIBRARY SCWRYPTS_LIBRARY_ROOT SCWRYPTS_LIBRARY_GROUP
local DEFER_ENVIRONMENT_CHECK=true
while [[ $# -gt 0 ]]
do
case $1 in
-g | --group )
[ $SCWRYPTS_LIBRARY_ROOT ] && ERROR 'specify only one of {(-g), (-r)}'
SCWRYPTS_LIBRARY_GROUP=$2
shift 1
;;
-r | --library-root )
[ $SCWRYPTS_LIBRARY_GROUP ] && ERROR 'specify only one of {(-g), (-r)}'
SCWRYPTS_LIBRARY_ROOT=$2
shift 1
;;
-c | --check-environment )
DEFER_ENVIRONMENT_CHECK=false
;;
* )
[ ! $SCWRYPTS_LIBRARY ] \
&& SCWRYPTS_LIBRARY=$1 \
|| ERROR 'too many arguments; expected exactly 1 argument' \
;;
esac
shift 1
done
[ ! $SCWRYPTS_LIBRARY ] && ERROR 'no library specified for import'
: \
&& [ ! $SCWRYPTS_LIBRARY_GROUP ] \
&& [ ! $SCWRYPTS_LIBRARY_ROOT ] \
&& SCWRYPTS_LIBRARY_GROUP=scwrypts
[ ! $SCWRYPTS_LIBRARY_ROOT ] && SCWRYPTS_LIBRARY_ROOT=$(GET_SCWRYPTS_LIBRARY_ROOT)
[ ! $SCWRYPTS_LIBRARY_ROOT ] && ERROR "unable to determine library root from group name '$SCWRYPTS_LIBRARY_GROUP'"
#####################################################################
local LIBRARY_FILE LIBRARY_FILE_TEMP
[ ! $LIBRARY_FILE ] \
&& LIBRARY_FILE_TEMP="$SCWRYPTS_LIBRARY_ROOT/$SCWRYPTS_LIBRARY.module.zsh" \
&& [ -f "$LIBRARY_FILE_TEMP" ] \
&& LIBRARY_FILE="$LIBRARY_FILE_TEMP"
[ ! $LIBRARY_FILE ] \
&& LIBRARY_FILE_TEMP="$SCWRYPTS_LIBRARY_ROOT/$SCWRYPTS_LIBRARY/$(echo $SCWRYPTS_LIBRARY | sed 's/.*\///').module.zsh" \
&& [ -f "$LIBRARY_FILE_TEMP" ] \
&& LIBRARY_FILE="$LIBRARY_FILE_TEMP" \
[ ! $LIBRARY_FILE ] \
&& ERROR "no such library '$SCWRYPTS_LIBRARY_GROUP/$SCWRYPTS_LIBRARY'"
#####################################################################
CHECK_ERRORS --no-fail || {
((IMPORT_ERRORS+=1))
return 1
}
#####################################################################
IS_LOADED && return 0
source "$LIBRARY_FILE" || {
((IMPORT_ERRORS+=1))
ERROR "import error for '$SCWRYPTS_LIBRARY_GROUP/$SCWRYPTS_LIBRARY'"
return 1
}
[[ $DEFER_ENVIRONMENT_CHECK =~ false ]] && {
CHECK_ENVIRONMENT || {
((IMPORT_ERRORS+=1))
ERROR "import error for '$SCWRYPTS_LIBRARY_GROUP/$SCWRYPTS_LIBRARY'"
return 1
}
}
IS_LOADED --set
}
GET_SCWRYPTS_LIBRARY_ROOT() {
local ROOT
ROOT=$(eval echo '$SCWRYPTS_LIBRARY_ROOT__'$SCWRYPTS_LIBRARY_GROUP)
[ $ROOT ] && echo $ROOT && return 0
[[ $(eval echo '$SCWRYPTS_TYPE__'$SCWRYPTS_LIBRARY_GROUP) =~ zsh ]] \
&& ROOT=$(eval echo '$SCWRYPTS_ROOT__'$SCWRYPTS_LIBRARY_GROUP/lib) \
|| ROOT=$(eval echo '$SCWRYPTS_ROOT__'$SCWRYPTS_LIBRARY_GROUP/zsh/lib) \
;
[ $ROOT ] && echo $ROOT && return 0
}
IS_LOADED() {
local VARIABLE_NAME="SCWRYPTS_LIBRARY_LOADED__${SCWRYPTS_LIBRARY_GROUP}__$(echo $SCWRYPTS_LIBRARY | sed 's|[/-]|_|g')"
[[ $1 =~ ^--set$ ]] \
&& eval $VARIABLE_NAME=1 \
[[ $(eval echo '$'$VARIABLE_NAME || echo 0) -eq 1 ]]
}
# temporary definitions for first load
CHECK_ERRORS() { return 0; unset -f CHECK_ERRORS; }
CHECK_ENVIRONMENT() { return 0; unset -f CHECK_ENVIRONMENT; }
ERROR() { echo $@ >&2; exit 1; }
#####################################################################
# ensures that zsh/utils and zsh/scwrypts/meta are always present!
use utils
use scwrypts/meta
#####################################################################
SCWRYPTS_IMPORT_DRIVER_READY=1