dotfiles/.config/bash/functions
Andrei Jiroh Halili 3d03a19d09
chore(global): update shellrcs and config
Signed-off-by: Andrei Jiroh Halili <ajhalili2006@andreijiroh.xyz>
2024-08-13 09:06:17 +08:00

53 lines
1.9 KiB
Bash

#!/usr/bin/env bash
# shellcheck disable=SC2046
# handle .env.keys detection
_load_env_keys() {
if [[ "$LAST_DOTENV_DIR" == "$PWD" ]]; then
return
elif [[ "$LAST_DOTENV_DIR" != "$PWD" ]] && [ -f "$LAST_DOTENV_DIR/.env.keys" ]; then
return
fi
if [ -f "$PWD/.env.keys" ] && [[ "$LOADED_DOTENV_KEYS" != "1" ]]; then
echo "dotenv-keys: loading up dotenv keys from this directory"
export "$(grep -v '^#' .env.keys | xargs)"
export DOTENV_KEYS_LOADED=1 LAST_DOTENV_DIR=$PWD DOTENV_KEYS_LOADER=auto
elif [ ! -f "$PWD/.env.keys" ] && [[ "$LOADED_DOTENV_KEYS" == "1" ]]; then
echo "dotenv-keys: unloading dotenv keys"
unset "${!DOTENV_PRIVATE_KEYS*}" DOTENV_KEYS_LOADED DOTENV_KEYS_LOADER
export LAST_DOTENV_DIR=$PWD
fi
}
if [[ ";${PROMPT_COMMAND[*]:-};" != *";_load_env_keys;"* ]]; then
if [[ "$(declare -p PROMPT_COMMAND 2>&1)" == "declare -a"* ]]; then
PROMPT_COMMAND=(_load_env_keys "${PROMPT_COMMAND[@]}")
else
PROMPT_COMMAND="_load_env_keys${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
fi
fi
# Allow manual access to .env.keys, even without the keys autoloader.
dotenv-keys() {
if [[ $1 == "load" ]]; then
if [ ! -f "$PWD/.env.keys" ]; then
echo "error: missing dotenv encryption keys"
return 1
fi
echo "dotenv-keys: loading up dotenv keys from this directory"
export "$(grep -v '^#' .env.keys | xargs)"
export LOADED_DOTENV_KEYS=$PWD DOTENV_KEYS_LOADER=manual DOTENV_KEYS_LOADED=1
elif [[ $1 == "unload" ]]; then
echo "dotenv-keys: manually unloading dotenv keys"
unset "${!DOTENV_PRIVATE_KEY*}" DOTENV_KEYS_LOADED DOTENV_KEYS_LOADER DOTENV_KEYS_LOADED
else
echo "dotenv-keys - .env.keys manager for dotenvx"
echo ""
echo "Commands:"
echo " load - load keys from .env.keys in current directory into shell session"
echo " unload - unload keys from shell session"
fi
}