Skip to content

Instantly share code, notes, and snippets.

@jan-swiecki
Created September 1, 2020 11:08
Show Gist options
  • Save jan-swiecki/286768524a633dd4127c73c300ba7a46 to your computer and use it in GitHub Desktop.
Save jan-swiecki/286768524a633dd4127c73c300ba7a46 to your computer and use it in GitHub Desktop.
#!/bin/bash -i
function find_config_path () (
test / == "$PWD" && return || test -f "$1" && echo "$PWD/$1" && return || cd .. && find_config_path "$1"
)
local_awsenv () {
IFS=$'\n'
for a in "$@"; do
[ "$a" == "--save" -o "$a" == "-s" ] && { save_env=1; } || {
name="$a"
}
done
name="${name:-default}"
source <(gpg2 -d ~/.aws/${name}.asc)
if [ -z "$AWS_ACCESS_KEY_ID" ]; then
echo "awsenv: failed to load ${name} (no AWS_ACCESS_KEY_ID)" >&2
return 1
fi
export AWS_ENV="$name"
[ "$save_env" == "1" ] && {
echo "Saving '$name' to .awsenv"
echo "$name" > .awsenv
}
unset name
}
PROMPT_DIRTRIM=2
yellow="\[\033[01;33m\]"
end="\[\033[00m\]"
# PS1='$([ -f .terraform/environment ] && { echo -n "["; cat .terraform/environment; echo "] "; })\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
# PS1='\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
# PS1="${PS1/]:/]${yellow}[AWS=$AWS_ENV,TF=]:${end}}"
PS1='\[\033[01;32m\]\u\[\033[00m\]\[\033[01;33m\][AWS=$AWS_ENV$([ -f .terraform/environment ] && { echo -n ",TF="; cat .terraform/environment; })]:\[\033[00m\]\[\033[01;34m\]\w\[\033[00m\]\$ '
set +eo pipefail
[ -z "$AWS_ENV" ] && {
path="$(find_config_path ".awsenv")"
if [ -f "$path" ]; then
name=`cat "$path"`
echo "tfawsenv: loading awsenv: $name"
local_awsenv "$name"
if [ -z "$AWS_ENV" ]; then
echo "tfawsenv: error loading awsenv: $name" >&2
exit 1
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment