Created
July 27, 2017 13:51
-
-
Save bennycornelissen/904f2327d1a44bd69b334723727a2c23 to your computer and use it in GitHub Desktop.
Situation-specific shell config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Source the functions for situation-specific shell config | |
. situation-specific.sh | |
## Load the global env | |
load_env global | |
## Load the machine-specific env for this machine | |
load_env machines/$(hostname -s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Source these functions in your Bash configuration | |
## See https://blog.bennycornelissen.nl/situation-aware-shell-config for more info | |
load_env() { | |
local env=${1:?no env set} | |
# Fuzzy filtering that removes the trailing .env if you do specify it | |
if [[ ${env} =~ .*.env$ ]]; then | |
local env="${env%'.env'}" | |
fi | |
if [ -r ${HOME}/.env/${env}.env ]; then | |
. ${HOME}/.env/${env}.env | |
echo "Loading ENV: ${env}" | |
export LOAD_ENV="${env} ${LOAD_ENV}" | |
fi | |
} | |
_load_env() | |
{ | |
COMPREPLY=($(cd ${HOME}/.env; compgen -f ${COMP_WORDS[COMP_CWORD]})) | |
} | |
complete -o nospace -F _load_env load_env | |
get_env() { | |
echo ${LOAD_ENV:?no env loaded} | |
} | |
unload_credentials() { | |
local env=${1:?no env set} | |
# Fuzzy filtering to make sure we're only dealing with credentials | |
# even if you're not specific about it | |
if [[ ! ${env} =~ ^credentials/.* ]]; then | |
local env="credentials/${env}" | |
fi | |
# Fuzzy filtering that removes the trailing .env if you do specify it | |
if [[ ${env} =~ .*.env$ ]]; then | |
local env="${env%'.env'}" | |
fi | |
local envfile="${HOME}/.env/${env}.env" | |
if [ -r ${envfile} ]; then | |
echo "Unloading ENV: ${env}" | |
local envvars=$(awk -F" |=" '/^export/ {printf $2" "}' ${envfile}) | |
for envvar in ${envvars}; do | |
unset ${envvar} | |
done | |
export LOAD_ENV="${LOAD_ENV//${env}/}" | |
fi | |
} | |
_unload_credentials() { | |
COMPREPLY=($(compgen -W "$(echo ${LOAD_ENV} | tr ' ' '\n' | egrep ^credentials)" -- ${COMP_WORDS[COMP_CWORD]})) | |
} | |
complete -o nospace -F _unload_credentials unload_credentials |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment