From 1ef3d57739b09f29ef07bf7c83b56ed5e996eab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Thu, 5 Mar 2020 17:13:46 +0100 Subject: [PATCH] aws: add support for AWS CLI v2 autocompletion (#8670) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marc Cornellà --- plugins/aws/aws.plugin.zsh | 64 +++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh index 567311372..eab3dc76b 100644 --- a/plugins/aws/aws.plugin.zsh +++ b/plugins/aws/aws.plugin.zsh @@ -62,36 +62,42 @@ fi # Load awscli completions -function _awscli-homebrew-installed() { - # check if Homebrew is installed - (( $+commands[brew] )) || return 1 +# AWS CLI v2 comes with its own autocompletion. Check if that is there, otherwise fall back +if [[ -x /usr/local/bin/aws_completer ]]; then + autoload -Uz bashcompinit && bashcompinit + complete -C aws_completer aws +else + function _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) + # 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 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 -} -# get aws_zsh_completer.sh location from $PATH -_aws_zsh_completer_path="$commands[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 + [[ -r $_aws_zsh_completer_path ]] && source $_aws_zsh_completer_path + unset _aws_zsh_completer_path _brew_prefix fi - -[[ -r $_aws_zsh_completer_path ]] && source $_aws_zsh_completer_path -unset _aws_zsh_completer_path _brew_prefix