zsh/zsh.d/zshfunctions/awsprofile

34 lines
1.0 KiB
Bash

profile=$1
if [ $profile ]; then # We got a profile pattern, look for a match
match=$(grep -E "\[profile .*$profile.*" ~/.aws/config | sed -E 's/\[profile (.+)\]/\1/') # Array of matching profiles
match_no=$(echo $match|wc -l) # Number of profile matching
if [ $match_no -gt 1 ]; then # more than one match
echo "Multiple profile match:"
echo $match
return
fi
if [ $match ]; then # Single match, setting profile
echo "Activating profile $match"
export AWS_DEFAULT_PROFILE=$match
export AWS_PROFILE=$match
export AWS_DEFAULT_REGION=$(grep -A1 $match ~/.aws/config|grep region|cut -d" " -f3)
export AWS_REGION=${AWS_DEFAULT_REGION}
return
else
echo "No match for $profile"
return
fi
elif [ $AWS_PROFILE ]; then # no profile passed, clean up current one, logout
unset AWS_DEFAULT_PROFILE AWS_PROFILE AWS_DEFAULT_REGION AWS_REGION
echo profile cleared.
return
fi
echo "Available profiles"
grep profile ~/.aws/config | sed -E 's/\[profile (.+)\]/\1/'
# vim: set ts=2 sw=2 tw=0 ft=sh :