From ab03873c52156e9572d25ed6bd5a3a60d84674ed Mon Sep 17 00:00:00 2001 From: Tony Blyler Date: Fri, 26 Aug 2022 23:32:56 -0400 Subject: [PATCH] Add an AWS profile setter for the CLI --- chezmoi/dot_zshrc.d/executable_aws.zsh | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/chezmoi/dot_zshrc.d/executable_aws.zsh b/chezmoi/dot_zshrc.d/executable_aws.zsh index 460b995..3d117d7 100644 --- a/chezmoi/dot_zshrc.d/executable_aws.zsh +++ b/chezmoi/dot_zshrc.d/executable_aws.zsh @@ -1,3 +1,31 @@ if command -v aws_completer &> /dev/null; then complete -C "$(command -v aws_completer)" aws fi + +aws_profile_set() { + local AWS_CONFIG_PATH="${HOME}/.aws/config" + if ! [ -r "${AWS_CONFIG_PATH}" ]; then + >&2 echo "${AWS_CONFIG_PATH} does not exist" + return 1 + fi + + local PROFILE="" + + PROFILE="$(awk '/^\[profile /{print substr($2, 1, length($2)-1)}' \ + "${AWS_CONFIG_PATH}" | + fzf --query "${1:-}" -1 -m 1 --height="$((LINES/4))" + )" + + if [ -z "${PROFILE}" ]; then + >&2 echo "failed to select a profile" + return 2 + fi + + echo "setting AWS profile to ${PROFILE}" + + export AWS_PROFILE="$PROFILE" + + export _ORIG_PS1="${_ORIG_PS1:-$PS1}" + + export PS1="🌩 ${AWS_PROFILE}🌩 ${_ORIG_PS1}" +}