Created
July 11, 2019 20:38
-
-
Save tyrostone/f0af38f56d51c4e9881e29b985319ecb to your computer and use it in GitHub Desktop.
Bash wrapper to complement the usage of aws-vault for AWS credentials
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
ENV=$1 | |
RED='\[\033[1;31m\]' | |
YELLOW='\[\033[1;33m\]' | |
NC='\[\033[0m\]' # No Color | |
if [[ "$OLD_PS1" == "" ]]; then | |
OLD_PS1="$PS1" | |
fi | |
# Unset AWS_* variables previously set from aws-vault. Do this via some janky bash to ensure it works. | |
while read -r line; do unset $line; done < <(env | grep AWS_ | while read old_env_value; do | |
# take each AWS var and unset them, ex: 'AWS_REGION=us-east-1' turns into 'unset AWS_REGION' | |
env_var="$(echo $old_env_value | awk -F'=' '{print $1}')"; | |
echo $env_var; | |
done); | |
if [[ "${ENV}" != "clear-env" ]]; then | |
# Re-assign AWS_* variables from aws-vault. Do this via some janky bash to ensure it works. | |
while read -r line; do declare -x "$line"; done < <(aws-vault exec ${ENV} -- env | grep AWS_) | |
# Export $PS1 so we know what we are doing. | |
if [[ "${ENV}" == "production" ]]; then | |
export PS1="[${RED}$ENV}${NC}] $OLD_PS1"; | |
else | |
export PS1="[${YELLOW}${ENV}${NC}] $OLD_PS1"; | |
fi | |
else | |
if [[ "$OLD_PS1" != "" ]]; then | |
export PS1="$OLD_PS1"; | |
fi | |
Fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment