From 1a2d930bca7966c44f1551ea2f0b8d7aecc80e56 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sat, 23 Mar 2019 19:52:31 +0100 Subject: aws: refactor completion sourcing logic (#7364) * Clean up Homebrew detection and add comments. Also changed some if flags. * Detect aws cli completion file from RPM --- plugins/aws/aws.plugin.zsh | 56 ++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 29 deletions(-) (limited to 'plugins/aws/aws.plugin.zsh') diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh index af27e669a..9cb69dd09 100644 --- a/plugins/aws/aws.plugin.zsh +++ b/plugins/aws/aws.plugin.zsh @@ -1,32 +1,9 @@ -_homebrew-installed() { - type brew &> /dev/null - _xit=$? - if [ $_xit -eq 0 ];then - # ok , we have brew installed - # speculatively we check default brew prefix - if [ -h /usr/local/opt/awscli ];then - _brew_prefix="/usr/local/opt/awscli" - else - # ok , it is not default prefix - # this call to brew is expensive ( about 400 ms ), so at least let's make it only once - _brew_prefix=$(brew --prefix awscli) - fi - return 0 - else - return $_xit - fi -} - -_awscli-homebrew-installed() { - [ -r $_brew_prefix/libexec/bin/aws_zsh_completer.sh ] &> /dev/null -} - function agp { echo $AWS_PROFILE } function asp { - local rprompt=${RPROMPT//} + local rprompt=${RPROMPT//} export AWS_DEFAULT_PROFILE=$1 export AWS_PROFILE=$1 @@ -39,11 +16,32 @@ function aws_profiles { } compctl -K aws_profiles asp -if which aws_zsh_completer.sh &>/dev/null; then - _aws_zsh_completer_path=$(which aws_zsh_completer.sh 2>/dev/null) -elif _homebrew-installed && _awscli-homebrew-installed; then + +# Load awscli completions + +_awscli-homebrew-installed() { + # check if Homebrew is installed + (( $+commands[brew] )) || return 1 + + # speculatively check default brew prefix + if [ -h /usr/local/opt/awscli ]; then + _brew_prefix=/usr/local/opt/awscli + else + # ok, it is not in the default prefix + # this call to brew is expensive (about 400 ms), so at least let's make it only once + _brew_prefix=$(brew --prefix awscli) + fi +} + +# get aws_zsh_completer.sh location from $PATH +_aws_zsh_completer_path="$commands[aws_zsh_completer.sh]" + +# otherwise check if installed via Homebrew +if [[ -z $_aws_zsh_completer_path ]] && _awscli-homebrew-installed; then _aws_zsh_completer_path=$_brew_prefix/libexec/bin/aws_zsh_completer.sh +else + _aws_zsh_completer_path=/usr/share/zsh/site-functions/aws_zsh_completer.sh fi -[ -n "$_aws_zsh_completer_path" ] && [ -x $_aws_zsh_completer_path ] && source $_aws_zsh_completer_path -unset _aws_zsh_completer_path +[[ -r $_aws_zsh_completer_path ]] && source $_aws_zsh_completer_path +unset _aws_zsh_completer_path _brew_prefix -- cgit v1.2.3-70-g09d2 From 532a784b806375370022605d8be17c1116553572 Mon Sep 17 00:00:00 2001 From: David Kane Date: Sun, 24 Mar 2019 18:37:07 +0000 Subject: aws: refactor AWS plugin (#7615) * Update the AWS plugin to support disabling RPROMT display: Use a $SHOW_AWS_PROMPT option. * Refactoring aws plugin: Exposing customizable aws_prompt_info function to be used in themes. * Set aws prompt prefix and suffix to original values and fix README Co-authored-by: "Vassilis S. Moustakas" --- plugins/aws/README.md | 25 +++++++++++++++++++------ plugins/aws/aws.plugin.zsh | 20 +++++++++++++++++--- 2 files changed, 36 insertions(+), 9 deletions(-) (limited to 'plugins/aws/aws.plugin.zsh') diff --git a/plugins/aws/README.md b/plugins/aws/README.md index 8a45199b8..57ab1e5ac 100644 --- a/plugins/aws/README.md +++ b/plugins/aws/README.md @@ -1,8 +1,7 @@ # aws This plugin provides completion support for [awscli](https://docs.aws.amazon.com/cli/latest/reference/index.html) -and a few utilities to manage AWS profiles: a function to change profiles with autocompletion support -and a function to get the current AWS profile. The current AWS profile is also displayed in `RPROMPT`. +and a few utilities to manage AWS profiles and display them in the prompt. To use it, add `aws` to the plugins array in your zshrc file. @@ -12,9 +11,23 @@ plugins=(... aws) ## Plugin commands -* `asp `: Sets `AWS_PROFILE` and `AWS_DEFAULT_PROFILE` (legacy) to ``. -It also adds it to your RPROMPT. +* `asp []`: Sets `$AWS_PROFILE` and `$AWS_DEFAULT_PROFILE` (legacy) to ``. + Run `asp` without arguments to clear the profile. -* `agp`: Gets the current value of `AWS_PROFILE`. +* `agp`: Gets the current value of `$AWS_PROFILE`. -* `aws_profiles`: Lists the available profiles in the file referenced in `AWS_CONFIG_FILE` (default: ~/.aws/config). Used to provide completion for the `asp` function. +* `aws_profiles`: Lists the available profiles in the `$AWS_CONFIG_FILE` (default: `~/.aws/config`). + Used to provide completion for the `asp` function. + +## Plugin options + +* Set `SHOW_AWS_PROMPT=false` in your zshrc file if you want to prevent the plugin from modifying your RPROMPT. + +## Theme + +The plugin creates an `aws_prompt_info` function that you can use in your theme, which displays +the current `$AWS_PROFILE`. It uses two variables to control how that is shown: + +- ZSH_THEME_AWS_PREFIX: sets the prefix of the AWS_PROFILE. Defaults to ``. diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh index 9cb69dd09..2fb351812 100644 --- a/plugins/aws/aws.plugin.zsh +++ b/plugins/aws/aws.plugin.zsh @@ -1,14 +1,16 @@ +# AWS profile selection + function agp { echo $AWS_PROFILE } function asp { - local rprompt=${RPROMPT//} - export AWS_DEFAULT_PROFILE=$1 export AWS_PROFILE=$1 - export RPROMPT="$rprompt" + if [[ -z "$1" ]]; then + echo AWS profile cleared. + fi } function aws_profiles { @@ -17,6 +19,18 @@ function aws_profiles { compctl -K aws_profiles asp +# AWS prompt + +function aws_prompt_info() { + [[ -z $AWS_PROFILE ]] && return + echo "${ZSH_THEME_AWS_PREFIX:=}" +} + +if [ "$SHOW_AWS_PROMPT" != false ]; then + export RPROMPT='$(aws_prompt_info)'"$RPROMPT" +fi + + # Load awscli completions _awscli-homebrew-installed() { -- cgit v1.2.3-70-g09d2 From 8cbdd79517d710e5bab6ce7aca0754b37462f2ee Mon Sep 17 00:00:00 2001 From: Logan Lindquist Date: Sun, 24 Mar 2019 13:46:27 -0500 Subject: aws: set AWS_EB_PROFILE for the EB CLI (#7388) Added AWS_EB_PROFILE environment variable to the AWS Plugin. The EB CLI uses this variable instead of the primary AWS_PROFILE variable to keep track of what profile it is using. https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-configuration.html --- plugins/aws/README.md | 1 + plugins/aws/aws.plugin.zsh | 1 + 2 files changed, 2 insertions(+) (limited to 'plugins/aws/aws.plugin.zsh') diff --git a/plugins/aws/README.md b/plugins/aws/README.md index 57ab1e5ac..45e6de937 100644 --- a/plugins/aws/README.md +++ b/plugins/aws/README.md @@ -12,6 +12,7 @@ plugins=(... aws) ## Plugin commands * `asp []`: Sets `$AWS_PROFILE` and `$AWS_DEFAULT_PROFILE` (legacy) to ``. + It also sets `$AWS_EB_PROFILE` to `` for the Elastic Beanstalk CLI. Run `asp` without arguments to clear the profile. * `agp`: Gets the current value of `$AWS_PROFILE`. diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh index 2fb351812..944557e14 100644 --- a/plugins/aws/aws.plugin.zsh +++ b/plugins/aws/aws.plugin.zsh @@ -7,6 +7,7 @@ function agp { function asp { export AWS_DEFAULT_PROFILE=$1 export AWS_PROFILE=$1 + export AWS_EB_PROFILE=$1 if [[ -z "$1" ]]; then echo AWS profile cleared. -- cgit v1.2.3-70-g09d2 From 4f4985fddc632a1d9745aec94ce95bea10006f97 Mon Sep 17 00:00:00 2001 From: Cristian Măgherușan-Stanciu Date: Wed, 30 Jul 2014 14:59:00 +0200 Subject: aws: added an alias for changing the AWS access key set on a profile --- plugins/aws/aws.plugin.zsh | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'plugins/aws/aws.plugin.zsh') diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh index 944557e14..7005af2d6 100644 --- a/plugins/aws/aws.plugin.zsh +++ b/plugins/aws/aws.plugin.zsh @@ -14,10 +14,25 @@ function asp { fi } +function aws_change_access_key { + if [[ "x$1" == "x" ]] then + echo "usage: $0 " + return 1 + else + echo "Insert the credentials when asked." + asp $1 + aws iam create-access-key + aws configure --profile $1 + echo "You can now safely delete the old access key running 'aws iam delete-access-key --access-key-id ID'" + echo "Your current keys are:" + aws iam list-access-keys + fi +} + function aws_profiles { reply=($(grep '\[profile' "${AWS_CONFIG_FILE:-$HOME/.aws/config}"|sed -e 's/.*profile \([a-zA-Z0-9_\.-]*\).*/\1/')) } -compctl -K aws_profiles asp +compctl -K aws_profiles asp aws_change_access_key # AWS prompt -- cgit v1.2.3-70-g09d2 From 5f893dcd209fdad920a1450355acbd4c10faabf2 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sat, 23 Mar 2019 20:56:55 +0100 Subject: aws: clean up aws_change_access_key function --- plugins/aws/aws.plugin.zsh | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'plugins/aws/aws.plugin.zsh') diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh index 7005af2d6..6de329bb8 100644 --- a/plugins/aws/aws.plugin.zsh +++ b/plugins/aws/aws.plugin.zsh @@ -15,18 +15,19 @@ function asp { } function aws_change_access_key { - if [[ "x$1" == "x" ]] then - echo "usage: $0 " + if [[ -z "$1" ]] then + echo "usage: $0 " return 1 - else - echo "Insert the credentials when asked." - asp $1 - aws iam create-access-key - aws configure --profile $1 - echo "You can now safely delete the old access key running 'aws iam delete-access-key --access-key-id ID'" - echo "Your current keys are:" - aws iam list-access-keys fi + + echo Insert the credentials when asked. + asp "$1" + aws iam create-access-key + aws configure --profile "$1" + + echo You can now safely delete the old access key running \`aws iam delete-access-key --access-key-id ID\` + echo Your current keys are: + aws iam list-access-keys } function aws_profiles { -- cgit v1.2.3-70-g09d2 From 5f9cc016d7fb19004529ea935d6a386846ad46e2 Mon Sep 17 00:00:00 2001 From: programmer04 Date: Tue, 9 Apr 2019 21:07:42 +0200 Subject: Fix redundant export of RPROMPT in aws plugin --- plugins/aws/aws.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/aws/aws.plugin.zsh') diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh index 6de329bb8..0895cfb3a 100644 --- a/plugins/aws/aws.plugin.zsh +++ b/plugins/aws/aws.plugin.zsh @@ -44,7 +44,7 @@ function aws_prompt_info() { } if [ "$SHOW_AWS_PROMPT" != false ]; then - export RPROMPT='$(aws_prompt_info)'"$RPROMPT" + RPROMPT='$(aws_prompt_info)'"$RPROMPT" fi -- cgit v1.2.3-70-g09d2 From d16adb6a9f9b0ea46121f48751a012f0511b465b Mon Sep 17 00:00:00 2001 From: dorons Date: Mon, 29 Apr 2019 12:42:36 +0300 Subject: aws: fix path resolution of aws completion for Ubuntu (#7812) --- plugins/aws/aws.plugin.zsh | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'plugins/aws/aws.plugin.zsh') diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh index 0895cfb3a..4bec30490 100644 --- a/plugins/aws/aws.plugin.zsh +++ b/plugins/aws/aws.plugin.zsh @@ -67,11 +67,18 @@ _awscli-homebrew-installed() { # get aws_zsh_completer.sh location from $PATH _aws_zsh_completer_path="$commands[aws_zsh_completer.sh]" -# otherwise check if installed via Homebrew -if [[ -z $_aws_zsh_completer_path ]] && _awscli-homebrew-installed; then - _aws_zsh_completer_path=$_brew_prefix/libexec/bin/aws_zsh_completer.sh -else - _aws_zsh_completer_path=/usr/share/zsh/site-functions/aws_zsh_completer.sh +# otherwise check common locations +if [[ -z $_aws_zsh_completer_path ]]; then + # Homebrew + if _awscli-homebrew-installed; then + _aws_zsh_completer_path=$_brew_prefix/libexec/bin/aws_zsh_completer.sh + # Ubuntu + elif [[ -e /usr/share/zsh/vendor-completions/_awscli ]]; then + _aws_zsh_completer_path=/usr/share/zsh/vendor-completions/_awscli + # RPM + else + _aws_zsh_completer_path=/usr/share/zsh/site-functions/aws_zsh_completer.sh + fi fi [[ -r $_aws_zsh_completer_path ]] && source $_aws_zsh_completer_path -- cgit v1.2.3-70-g09d2 From 4585c6662be4cefafd6e67df1d859abb7988168e Mon Sep 17 00:00:00 2001 From: Mohamed El Mouctar Haidara Date: Fri, 10 May 2019 17:28:31 +0200 Subject: aws: really unset AWS*PROFILE variables in asp function (#7838) --- plugins/aws/aws.plugin.zsh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'plugins/aws/aws.plugin.zsh') diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh index 4bec30490..d70a64fcc 100644 --- a/plugins/aws/aws.plugin.zsh +++ b/plugins/aws/aws.plugin.zsh @@ -5,13 +5,15 @@ function agp { } function asp { - export AWS_DEFAULT_PROFILE=$1 - export AWS_PROFILE=$1 - export AWS_EB_PROFILE=$1 - if [[ -z "$1" ]]; then + unset AWS_DEFAULT_PROFILE AWS_PROFILE AWS_EB_PROFILE echo AWS profile cleared. + return fi + + export AWS_DEFAULT_PROFILE=$1 + export AWS_PROFILE=$1 + export AWS_EB_PROFILE=$1 } function aws_change_access_key { -- cgit v1.2.3-70-g09d2 From 9598a855fa55f9f126f099a4ab792335d2dc1a69 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sat, 25 May 2019 10:24:43 +0200 Subject: aws: check for existence of config file in aws_profiles Fixes #7872 --- plugins/aws/aws.plugin.zsh | 1 + 1 file changed, 1 insertion(+) (limited to 'plugins/aws/aws.plugin.zsh') diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh index d70a64fcc..b78cd2ac1 100644 --- a/plugins/aws/aws.plugin.zsh +++ b/plugins/aws/aws.plugin.zsh @@ -33,6 +33,7 @@ function aws_change_access_key { } function aws_profiles { + [[ -r "${AWS_CONFIG_FILE:-$HOME/.aws/config}" ]] || return 1 reply=($(grep '\[profile' "${AWS_CONFIG_FILE:-$HOME/.aws/config}"|sed -e 's/.*profile \([a-zA-Z0-9_\.-]*\).*/\1/')) } compctl -K aws_profiles asp aws_change_access_key -- cgit v1.2.3-70-g09d2 From 1343ab67edd8a81b75aceca77ddb526be87a20c1 Mon Sep 17 00:00:00 2001 From: programmer04 Date: Sun, 26 May 2019 12:31:37 +0200 Subject: aws: check availability of aws profiles (#7839) --- plugins/aws/aws.plugin.zsh | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'plugins/aws/aws.plugin.zsh') diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh index b78cd2ac1..5f5bf9bbb 100644 --- a/plugins/aws/aws.plugin.zsh +++ b/plugins/aws/aws.plugin.zsh @@ -1,29 +1,35 @@ -# AWS profile selection - -function agp { +agp() { echo $AWS_PROFILE } -function asp { +# AWS profile selection +asp() { if [[ -z "$1" ]]; then unset AWS_DEFAULT_PROFILE AWS_PROFILE AWS_EB_PROFILE echo AWS profile cleared. return fi + local available_profiles=($(aws_profiles)) + if [[ -z "${available_profiles[(r)$1]}" ]]; then + echo "${fg[red]}Profile '$1' not found in '${AWS_CONFIG_FILE:-$HOME/.aws/config}'" >&2 + echo "Available profiles: ${(j:, :)available_profiles:-no profiles found}${reset_color}" >&2 + return 1 + fi + export AWS_DEFAULT_PROFILE=$1 export AWS_PROFILE=$1 export AWS_EB_PROFILE=$1 } -function aws_change_access_key { - if [[ -z "$1" ]] then +aws_change_access_key() { + if [[ -z "$1" ]]; then echo "usage: $0 " return 1 fi echo Insert the credentials when asked. - asp "$1" + asp "$1" || return 1 aws iam create-access-key aws configure --profile "$1" @@ -32,16 +38,18 @@ function aws_change_access_key { aws iam list-access-keys } -function aws_profiles { +aws_profiles() { [[ -r "${AWS_CONFIG_FILE:-$HOME/.aws/config}" ]] || return 1 - reply=($(grep '\[profile' "${AWS_CONFIG_FILE:-$HOME/.aws/config}"|sed -e 's/.*profile \([a-zA-Z0-9_\.-]*\).*/\1/')) + grep '\[profile' "${AWS_CONFIG_FILE:-$HOME/.aws/config}"|sed -e 's/.*profile \([a-zA-Z0-9_\.-]*\).*/\1/' } -compctl -K aws_profiles asp aws_change_access_key +_aws_profiles() { + reply=($(aws_profiles)) +} +compctl -K _aws_profiles asp aws_change_access_key # AWS prompt - -function aws_prompt_info() { +aws_prompt_info() { [[ -z $AWS_PROFILE ]] && return echo "${ZSH_THEME_AWS_PREFIX:=}" } -- cgit v1.2.3-70-g09d2 From 9930dd6e49df5630ac23c68b85465f0bb801d17f Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 28 May 2019 15:33:02 +0200 Subject: aws: use function keyword in function definitions Fixes #7886 --- plugins/aws/aws.plugin.zsh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'plugins/aws/aws.plugin.zsh') diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh index 5f5bf9bbb..231ac5ad2 100644 --- a/plugins/aws/aws.plugin.zsh +++ b/plugins/aws/aws.plugin.zsh @@ -1,9 +1,9 @@ -agp() { +function agp() { echo $AWS_PROFILE } # AWS profile selection -asp() { +function asp() { if [[ -z "$1" ]]; then unset AWS_DEFAULT_PROFILE AWS_PROFILE AWS_EB_PROFILE echo AWS profile cleared. @@ -22,7 +22,7 @@ asp() { export AWS_EB_PROFILE=$1 } -aws_change_access_key() { +function aws_change_access_key() { if [[ -z "$1" ]]; then echo "usage: $0 " return 1 @@ -38,18 +38,18 @@ aws_change_access_key() { aws iam list-access-keys } -aws_profiles() { +function aws_profiles() { [[ -r "${AWS_CONFIG_FILE:-$HOME/.aws/config}" ]] || return 1 grep '\[profile' "${AWS_CONFIG_FILE:-$HOME/.aws/config}"|sed -e 's/.*profile \([a-zA-Z0-9_\.-]*\).*/\1/' } -_aws_profiles() { +function _aws_profiles() { reply=($(aws_profiles)) } compctl -K _aws_profiles asp aws_change_access_key # AWS prompt -aws_prompt_info() { +function aws_prompt_info() { [[ -z $AWS_PROFILE ]] && return echo "${ZSH_THEME_AWS_PREFIX:=}" } @@ -61,7 +61,7 @@ fi # Load awscli completions -_awscli-homebrew-installed() { +function _awscli-homebrew-installed() { # check if Homebrew is installed (( $+commands[brew] )) || return 1 -- cgit v1.2.3-70-g09d2