Diff
1diff --git a/plugins/aws/README.md b/plugins/aws/README.md
2index d6f4f4600a5c9c3f00b2db78cbfbb2253a158adf..e1e355741403d095994278e52ce8bec9fe1a9df1 100644
3--- a/plugins/aws/README.md
4+++ b/plugins/aws/README.md
5@@ -1,7 +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 and display them in the prompt.
10+and a few utilities to manage AWS profiles/regions and display them in the prompt.
11
12 To use it, add `aws` to the plugins array in your zshrc file.
13
14@@ -16,6 +16,9 @@ plugins=(... aws)
15 Run `asp` without arguments to clear the profile.
16 * `asp [<profile>] login`: If AWS SSO has been configured in your aws profile, it will run the `aws sso login` command following profile selection.
17
18+* `asr [<region>]`: sets `$AWS_REGION` and `$AWS_DEFAULT_REGION` (legacy) to `<region>`.
19+ Run `asr` without arguments to clear the profile.
20+
21 * `acp [<profile>] [<mfa_token>]`: in addition to `asp` functionality, it actually changes
22 the profile by assuming the role specified in the `<profile>` configuration. It supports
23 MFA and sets `$AWS_ACCESS_KEY_ID`, `$AWS_SECRET_ACCESS_KEY` and `$AWS_SESSION_TOKEN`, if
24@@ -25,25 +28,34 @@ plugins=(... aws)
25
26 * `agp`: gets the current value of `$AWS_PROFILE`.
27
28+* `agr`: gets the current value of `$AWS_REGION`.
29+
30 * `aws_change_access_key`: changes the AWS access key of a profile.
31
32 * `aws_profiles`: lists the available profiles in the `$AWS_CONFIG_FILE` (default: `~/.aws/config`).
33 Used to provide completion for the `asp` function.
34
35+* `aws_regions`: lists the available regions.
36+ Used to provide completion for the `asr` function.
37+
38 ## Plugin options
39
40 * Set `SHOW_AWS_PROMPT=false` in your zshrc file if you want to prevent the plugin from modifying your RPROMPT.
41 Some themes might overwrite the value of RPROMPT instead of appending to it, so they'll need to be fixed to
42- see the AWS profile prompt.
43+ see the AWS profile/region prompt.
44
45 ## Theme
46
47 The plugin creates an `aws_prompt_info` function that you can use in your theme, which displays
48-the current `$AWS_PROFILE`. It uses two variables to control how that is shown:
49+the current `$AWS_PROFILE` and `$AWS_REGION`. It uses four variables to control how that is shown:
50+
51+* ZSH_THEME_AWS_PROFILE_PREFIX: sets the prefix of the AWS_PROFILE. Defaults to `<aws:`.
52+
53+* ZSH_THEME_AWS_PROFILE_SUFFIX: sets the suffix of the AWS_PROFILE. Defaults to `>`.
54
55-* ZSH_THEME_AWS_PREFIX: sets the prefix of the AWS_PROFILE. Defaults to `<aws:`.
56+* ZSH_THEME_AWS_REGION_PREFIX: sets the prefix of the AWS_REGION. Defaults to `<region:`.
57
58-* ZSH_THEME_AWS_SUFFIX: sets the suffix of the AWS_PROFILE. Defaults to `>`.
59+* ZSH_THEME_AWS_REGION_SUFFIX: sets the suffix of the AWS_REGION. Defaults to `>`.
60
61 ## Configuration
62
63diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh
64index 865e82f193c88fef40c190e4de6170fe6c189ad6..1c386a3e180b17238ac8315c3bf60d9c2a5a433e 100644
65--- a/plugins/aws/aws.plugin.zsh
66+++ b/plugins/aws/aws.plugin.zsh
67@@ -2,6 +2,10 @@ function agp() {
68 echo $AWS_PROFILE
69 }
70
71+function agr() {
72+ echo $AWS_REGION
73+}
74+
75 # AWS profile selection
76 function asp() {
77 if [[ -z "$1" ]]; then
78@@ -27,6 +31,25 @@ function asp() {
79 fi
80 }
81
82+# AWS region selection
83+function asr() {
84+ if [[ -z "$1" ]]; then
85+ unset AWS_DEFAULT_REGION AWS_REGION
86+ echo AWS region cleared.
87+ return
88+ fi
89+
90+ local -a available_regions
91+ available_regions=($(aws_regions))
92+ if [[ -z "${available_regions[(r)$1]}" ]]; then
93+ echo "${fg[red]}Available regions: \n$(aws_regions)"
94+ return 1
95+ fi
96+
97+ export AWS_REGION=$1
98+ export AWS_DEFAULT_REGION=$1
99+}
100+
101 # AWS profile switch
102 function acp() {
103 if [[ -z "$1" ]]; then
104@@ -145,12 +168,25 @@ function aws_change_access_key() {
105 AWS_PAGER="" aws iam list-access-keys
106 }
107
108+function aws_regions() {
109+ if [[ $AWS_DEFAULT_PROFILE || $AWS_PROFILE ]];then
110+ aws ec2 describe-regions |grep RegionName | awk -F ':' '{gsub(/"/, "", $2);gsub(/,/, "", $2);gsub(/ /, "", $2); print $2}'
111+ else
112+ echo "You must specify a AWS profile."
113+ fi
114+}
115+
116 function aws_profiles() {
117 aws --no-cli-pager configure list-profiles 2> /dev/null && return
118 [[ -r "${AWS_CONFIG_FILE:-$HOME/.aws/config}" ]] || return 1
119 grep --color=never -Eo '\[.*\]' "${AWS_CONFIG_FILE:-$HOME/.aws/config}" | sed -E 's/^[[:space:]]*\[(profile)?[[:space:]]*([^[:space:]]+)\][[:space:]]*$/\2/g'
120 }
121
122+function _aws_regions() {
123+ reply=($(aws_regions))
124+}
125+compctl -K _aws_regions asr
126+
127 function _aws_profiles() {
128 reply=($(aws_profiles))
129 }
130@@ -158,8 +194,8 @@ compctl -K _aws_profiles asp acp aws_change_access_key
131
132 # AWS prompt
133 function aws_prompt_info() {
134- [[ -n "$AWS_PROFILE" ]] || return
135- echo "${ZSH_THEME_AWS_PREFIX=<aws:}${AWS_PROFILE:gs/%/%%}${ZSH_THEME_AWS_SUFFIX=>}"
136+ if [[ -z $AWS_REGION && -z $AWS_PROFILE ]];then return; fi
137+ echo "${ZSH_THEME_AWS_PROFILE_PREFIX:=<aws:}${AWS_PROFILE}${ZSH_THEME_AWS_PROFILE_SUFFIX:=>} ${ZSH_THEME_AWS_REGION_PREFIX:=<region:}${AWS_REGION}${ZSH_THEME_AWS_REGION_SUFFIX:=>}"
138 }
139
140 if [[ "$SHOW_AWS_PROMPT" != false && "$RPROMPT" != *'$(aws_prompt_info)'* ]]; then
141@@ -211,3 +247,4 @@ else
142 [[ -r $_aws_zsh_completer_path ]] && source $_aws_zsh_completer_path
143 unset _aws_zsh_completer_path _brew_prefix
144 fi
145+