Fix multimatch on awsprofile function

This commit is contained in:
Andrea Mistrali 2022-06-10 09:30:40 +02:00
parent 0173294d2a
commit b8eec1e213
No known key found for this signature in database
GPG Key ID: 6FB0A77F6D5DA9B2
1 changed files with 25 additions and 12 deletions

View File

@ -1,5 +1,5 @@
profile=$1 profile=$1
shift [ $# -gt 0 ] && shift
region=${1:-""} region=${1:-""}
@ -9,24 +9,37 @@ if [ ! -f ~/.aws/config ]; then
fi fi
if [ $profile ]; then # We got a profile pattern, look for a match 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 # look for an exact match
match_no=$(echo $match|wc -l) # Number of profile matching grep -qE "\[profile $profile\]" ~/.aws/config
if [ $match_no -gt 1 ]; then # more than one match if [ $? -eq 0 ]; then # We have an exact match
print -P "$FX[bold]multiple profile match:" match=$profile
print -P "$FG[003]" else # Look for regex match
echo $match match=$(grep -E "\[profile .*$profile.*" ~/.aws/config | sed -E 's/\[profile (.+)\]/\1/') # Array of matching profiles
print -P "$FX[reset]" match_no=$(echo $match|wc -l) # Number of profiles matching
return
if [ $match_no -gt 1 ]; then # more than one match
print -P "$FX[bold]multiple profile match:"
print -P "$FG[003]"
echo $match
print -P "$FX[reset]"
return
fi
fi fi
if [ $match ]; then # Single match, setting profile if [ $match ]; then # Single match, setting profile
export AWS_CLI_AUTO_PROMPT=on export AWS_CLI_AUTO_PROMPT=off
# Set default profile and profile
export AWS_DEFAULT_PROFILE=$match export AWS_DEFAULT_PROFILE=$match
export AWS_PROFILE=${AWS_DEFAULT_PROFILE}
# Get default region and set region to argument or default region
export AWS_DEFAULT_REGION=$(aws configure get region) export AWS_DEFAULT_REGION=$(aws configure get region)
export AWS_PROFILE=$match
export AWS_REGION=${region:-$AWS_DEFAULT_REGION} export AWS_REGION=${region:-$AWS_DEFAULT_REGION}
print -P "$FX[bold]activating profile $FG[075]$match$FX[reset] $FX[bold] on region $FG[075]$AWS_REGION$FX[reset]"
print -P "$FX[bold]activating profile $FG[075]$match$FX[reset]$FX[bold] on region $FG[075]$AWS_REGION$FX[reset]"
export AWS_CLI_AUTO_PROMPT=on
return return
else else
print -P "$FX[bold]$FG[009]no match for $profile$FX[reset]" print -P "$FX[bold]$FG[009]no match for $profile$FX[reset]"