532a784b806375370022605d8be17c1116553572

Author
David Kane <dkanejs@gmail.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

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" <vsmoustakas@gmail.com>

Diff

 1diff --git a/plugins/aws/README.md b/plugins/aws/README.md
 2index 8a45199b8e95d0882fbac95219b3375a5278f53b..57ab1e5ac750179571357b47130eded3a138f6fe 100644
 3--- a/plugins/aws/README.md
 4+++ b/plugins/aws/README.md
 5@@ -1,8 +1,7 @@
 6 # aws
 7 
 8 This plugin provides completion support for [awscli](https://docs.aws.amazon.com/cli/latest/reference/index.html)
 9-and a few utilities to manage AWS profiles: a function to change profiles with autocompletion support
10-and a function to get the current AWS profile. The current AWS profile is also displayed in `RPROMPT`.
11+and a few utilities to manage AWS profiles and display them in the prompt.
12 
13 To use it, add `aws` to the plugins array in your zshrc file.
14 
15@@ -12,9 +11,23 @@ plugins=(... aws)
16 
17 ## Plugin commands
18 
19-* `asp <profile>`: Sets `AWS_PROFILE` and `AWS_DEFAULT_PROFILE` (legacy) to `<profile>`.
20-It also adds it to your RPROMPT.
21+* `asp [<profile>]`: Sets `$AWS_PROFILE` and `$AWS_DEFAULT_PROFILE` (legacy) to `<profile>`.
22+  Run `asp` without arguments to clear the profile.
23 
24-* `agp`: Gets the current value of `AWS_PROFILE`.
25+* `agp`: Gets the current value of `$AWS_PROFILE`.
26 
27-* `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.
28+* `aws_profiles`: Lists the available profiles in the  `$AWS_CONFIG_FILE` (default: `~/.aws/config`).
29+  Used to provide completion for the `asp` function.
30+
31+## Plugin options
32+
33+* Set `SHOW_AWS_PROMPT=false` in your zshrc file if you want to prevent the plugin from modifying your RPROMPT.
34+
35+## Theme
36+
37+The plugin creates an `aws_prompt_info` function that you can use in your theme, which displays
38+the current `$AWS_PROFILE`. It uses two variables to control how that is shown:
39+
40+- ZSH_THEME_AWS_PREFIX: sets the prefix of the AWS_PROFILE. Defaults to `<aws:`.
41+
42+- ZSH_THEME_AWS_SUFFIX: sets the suffix of the AWS_PROFILE. Defaults to `>`.
43diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh
44index 9cb69dd09284aac5243b267de5e20e7c9663b6a8..2fb3518125f4dd540c4654c5c28b24e549dbc79a 100644
45--- a/plugins/aws/aws.plugin.zsh
46+++ b/plugins/aws/aws.plugin.zsh
47@@ -1,14 +1,16 @@
48+# AWS profile selection
49+
50 function agp {
51   echo $AWS_PROFILE
52 }
53 
54 function asp {
55-  local rprompt=${RPROMPT/<aws:$AWS_PROFILE>/}
56-
57   export AWS_DEFAULT_PROFILE=$1
58   export AWS_PROFILE=$1
59 
60-  export RPROMPT="<aws:$AWS_PROFILE>$rprompt"
61+  if [[ -z "$1" ]]; then
62+    echo AWS profile cleared.
63+  fi
64 }
65 
66 function aws_profiles {
67@@ -17,6 +19,18 @@ function aws_profiles {
68 compctl -K aws_profiles asp
69 
70 
71+# AWS prompt
72+
73+function aws_prompt_info() {
74+  [[ -z $AWS_PROFILE ]] && return
75+  echo "${ZSH_THEME_AWS_PREFIX:=<aws:}${AWS_PROFILE}${ZSH_THEME_AWS_SUFFIX:=>}"
76+}
77+
78+if [ "$SHOW_AWS_PROMPT" != false ]; then
79+  export RPROMPT='$(aws_prompt_info)'"$RPROMPT"
80+fi
81+
82+
83 # Load awscli completions
84 
85 _awscli-homebrew-installed() {