1ef3d57739b09f29ef07bf7c83b56ed5e996eab2

Author
Marc Cornellà <marc.cornella@live.com>
Committer
GitHub <noreply@github.com>
Date

Message

aws: add support for AWS CLI v2 autocompletion (#8670)

Co-authored-by: Marc Cornellà <marc.cornella@live.com>

Diff

 1diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh
 2index 567311372ea71fe00ef81933e32a64e338698cff..eab3dc76b23124096ef7164b7c39dc72fbd8f9e7 100644
 3--- a/plugins/aws/aws.plugin.zsh
 4+++ b/plugins/aws/aws.plugin.zsh
 5@@ -62,36 +62,42 @@ fi
 6 
 7 # Load awscli completions
 8 
 9-function _awscli-homebrew-installed() {
10-  # check if Homebrew is installed
11-  (( $+commands[brew] )) || return 1
12-
13-  # speculatively check default brew prefix
14-  if [ -h /usr/local/opt/awscli ]; then
15-    _brew_prefix=/usr/local/opt/awscli
16-  else
17-    # ok, it is not in the default prefix
18-    # this call to brew is expensive (about 400 ms), so at least let's make it only once
19-    _brew_prefix=$(brew --prefix awscli)
20+# AWS CLI v2 comes with its own autocompletion. Check if that is there, otherwise fall back
21+if [[ -x /usr/local/bin/aws_completer ]]; then
22+  autoload -Uz bashcompinit && bashcompinit
23+  complete -C aws_completer aws
24+else
25+  function _awscli-homebrew-installed() {
26+    # check if Homebrew is installed
27+    (( $+commands[brew] )) || return 1
28+
29+    # speculatively check default brew prefix
30+    if [ -h /usr/local/opt/awscli ]; then
31+      _brew_prefix=/usr/local/opt/awscli
32+    else
33+      # ok, it is not in the default prefix
34+      # this call to brew is expensive (about 400 ms), so at least let's make it only once
35+      _brew_prefix=$(brew --prefix awscli)
36+    fi
37+  }
38+
39+  # get aws_zsh_completer.sh location from $PATH
40+  _aws_zsh_completer_path="$commands[aws_zsh_completer.sh]"
41+
42+  # otherwise check common locations
43+  if [[ -z $_aws_zsh_completer_path ]]; then
44+    # Homebrew
45+    if _awscli-homebrew-installed; then
46+      _aws_zsh_completer_path=$_brew_prefix/libexec/bin/aws_zsh_completer.sh
47+    # Ubuntu
48+    elif [[ -e /usr/share/zsh/vendor-completions/_awscli ]]; then
49+      _aws_zsh_completer_path=/usr/share/zsh/vendor-completions/_awscli
50+    # RPM
51+    else
52+      _aws_zsh_completer_path=/usr/share/zsh/site-functions/aws_zsh_completer.sh
53+    fi
54   fi
55-}
56 
57-# get aws_zsh_completer.sh location from $PATH
58-_aws_zsh_completer_path="$commands[aws_zsh_completer.sh]"
59-
60-# otherwise check common locations
61-if [[ -z $_aws_zsh_completer_path ]]; then
62-  # Homebrew
63-  if _awscli-homebrew-installed; then
64-    _aws_zsh_completer_path=$_brew_prefix/libexec/bin/aws_zsh_completer.sh
65-  # Ubuntu
66-  elif [[ -e /usr/share/zsh/vendor-completions/_awscli ]]; then
67-    _aws_zsh_completer_path=/usr/share/zsh/vendor-completions/_awscli
68-  # RPM
69-  else
70-    _aws_zsh_completer_path=/usr/share/zsh/site-functions/aws_zsh_completer.sh
71-  fi
72+  [[ -r $_aws_zsh_completer_path ]] && source $_aws_zsh_completer_path
73+  unset _aws_zsh_completer_path _brew_prefix
74 fi
75-
76-[[ -r $_aws_zsh_completer_path ]] && source $_aws_zsh_completer_path
77-unset _aws_zsh_completer_path _brew_prefix