From fc8f82329bdbd3535bc15c5d3b434652c2aec41e Mon Sep 17 00:00:00 2001 From: dcarrillo Date: Sat, 22 Feb 2020 16:11:38 +0100 Subject: [PATCH] [aws] Add function to activate and switch profiles taken from aws credentials file --- modules/aws/functions/_aws_switch_profile | 13 +++++++++++++ modules/aws/init.zsh | 9 +++++++++ 2 files changed, 22 insertions(+) create mode 100644 modules/aws/functions/_aws_switch_profile diff --git a/modules/aws/functions/_aws_switch_profile b/modules/aws/functions/_aws_switch_profile new file mode 100644 index 0000000..434f5b3 --- /dev/null +++ b/modules/aws/functions/_aws_switch_profile @@ -0,0 +1,13 @@ +#compdef aws_switch_profile +#autoload + +if [[ ! -f ~/.aws/credentials ]]; then + return 1 +fi + +local profiles +profiles=$(grep -E "^\[.+\]" ~/.aws/credentials | tr -d '[' | tr -d ']' ) + +_arguments "1:toggle:(( +$profiles +))" && return 0 diff --git a/modules/aws/init.zsh b/modules/aws/init.zsh index 5271b75..6bde1a9 100644 --- a/modules/aws/init.zsh +++ b/modules/aws/init.zsh @@ -127,3 +127,12 @@ function aws_cf { function ssh_aws_any { aws_ssm_session_any $1 ssh } + +function aws_switch_profile { + echo "Activating profile $1..." + export AWS_PROFILE=$1 + + if [[ -f ~/.aws/credentials ]]; then #&& (( ! ${+AWS_DEFAULT_REGION} )) + export AWS_DEFAULT_REGION=$(aws configure get region) + fi +}