1a2d930bca7966c44f1551ea2f0b8d7aecc80e56

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

Message

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

Diff

 1diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh
 2index af27e669a63ce1fdb56fc45ea2014dacc019eef4..9cb69dd09284aac5243b267de5e20e7c9663b6a8 100644
 3--- a/plugins/aws/aws.plugin.zsh
 4+++ b/plugins/aws/aws.plugin.zsh
 5@@ -1,32 +1,9 @@
 6-_homebrew-installed() {
 7-  type brew &> /dev/null
 8-  _xit=$?
 9-  if [ $_xit -eq 0 ];then
10-        # ok , we have brew installed
11-        # speculatively we check default brew prefix
12-        if [ -h  /usr/local/opt/awscli ];then
13-                _brew_prefix="/usr/local/opt/awscli"
14-        else
15-                # ok , it is not default prefix
16-                # this call to brew is expensive ( about 400 ms ), so at least let's make it only once
17-                _brew_prefix=$(brew --prefix awscli)
18-        fi
19-        return 0
20-   else
21-        return $_xit
22-   fi
23-}
24-
25-_awscli-homebrew-installed() {
26-  [ -r $_brew_prefix/libexec/bin/aws_zsh_completer.sh ] &> /dev/null
27-}
28-
29 function agp {
30   echo $AWS_PROFILE
31 }
32 
33 function asp {
34-  local rprompt=${RPROMPT/<aws:$(agp)>/}
35+  local rprompt=${RPROMPT/<aws:$AWS_PROFILE>/}
36 
37   export AWS_DEFAULT_PROFILE=$1
38   export AWS_PROFILE=$1
39@@ -39,11 +16,32 @@ function aws_profiles {
40 }
41 compctl -K aws_profiles asp
42 
43-if which aws_zsh_completer.sh &>/dev/null; then
44-  _aws_zsh_completer_path=$(which aws_zsh_completer.sh 2>/dev/null)
45-elif _homebrew-installed && _awscli-homebrew-installed; then
46+
47+# Load awscli completions
48+
49+_awscli-homebrew-installed() {
50+  # check if Homebrew is installed
51+  (( $+commands[brew] )) || return 1
52+
53+  # speculatively check default brew prefix
54+  if [ -h /usr/local/opt/awscli ]; then
55+    _brew_prefix=/usr/local/opt/awscli
56+  else
57+    # ok, it is not in the default prefix
58+    # this call to brew is expensive (about 400 ms), so at least let's make it only once
59+    _brew_prefix=$(brew --prefix awscli)
60+  fi
61+}
62+
63+# get aws_zsh_completer.sh location from $PATH
64+_aws_zsh_completer_path="$commands[aws_zsh_completer.sh]"
65+
66+# otherwise check if installed via Homebrew
67+if [[ -z $_aws_zsh_completer_path ]] && _awscli-homebrew-installed; then
68   _aws_zsh_completer_path=$_brew_prefix/libexec/bin/aws_zsh_completer.sh
69+else
70+  _aws_zsh_completer_path=/usr/share/zsh/site-functions/aws_zsh_completer.sh
71 fi
72 
73-[ -n "$_aws_zsh_completer_path" ] && [ -x $_aws_zsh_completer_path ] && source $_aws_zsh_completer_path
74-unset _aws_zsh_completer_path
75+[[ -r $_aws_zsh_completer_path ]] && source $_aws_zsh_completer_path
76+unset _aws_zsh_completer_path _brew_prefix