Skip to content

Instantly share code, notes, and snippets.

@hmdmph
Last active November 21, 2022 09:27
Show Gist options
  • Save hmdmph/72f0f86e4de1415eb3ef9a3a5645c612 to your computer and use it in GitHub Desktop.
Save hmdmph/72f0f86e4de1415eb3ef9a3a5645c612 to your computer and use it in GitHub Desktop.
# completion script for above awss function
# If you change your function name, replace your function name with here awss
# the grep I used here is quite important and may be not work with your profile without simple tweak
# Simple logic is you have to have a command that returns all of the profile in aws config
_awss_completion()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=`(grep '\[profile' ~/.aws/config | cut -d ' ' -f 2 | cut -d ']' -f 1)`
if [[ ${cur} == * ]] ; then
#COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
} && complete -F _awss_completion awss
# Simple function to switch aws profiles with the help of awsudo
# function name awss (with extra 's') just used for easiness and laziness (just one more key push)
# you can change fucntion name as you wish but remember to use the same in auto-complete code too.
function awss(){
FILEPATH="/tmp/temp-env.txt"
if [ "$#" -le 1 ] ; then
echo "Run awss command to switch profile.\n\tUsage: awss <profile-name> <any-other-optional-command(s)>.\n\t\tExample: awss admin-test-aws-acc-1 \n\t\tExample: awss read-test-aws-acc-2 aws s3 ls"
awsudo "${1}" > $FILEPATH
while read p; do
export "${p}"
done <$FILEPATH
export AWS_PROFILE=${1}
else
export AWS_PROFILE=${1}
awsudo "${1}" -- "${2}" "${@:3}"
fi
if [ -f "$FILEPATH" ] ; then
rm "$FILEPATH"
fi
}
@hmdmph
Copy link
Author

hmdmph commented Jan 23, 2022

Have fun switching AWS profiles with bash auto-completion.

aws-profile-auto-complete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment