1343ab67edd8a81b75aceca77ddb526be87a20c1

Author
programmer04 <jakub.warczarek@gmail.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

aws: check availability of aws profiles (#7839)

Diff

 1diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh
 2index b78cd2ac1efa6701d492660359c0479ebbe1bbaa..5f5bf9bbbb3770ff25e4ac915ca47d0ae983fc9e 100644
 3--- a/plugins/aws/aws.plugin.zsh
 4+++ b/plugins/aws/aws.plugin.zsh
 5@@ -1,29 +1,35 @@
 6-# AWS profile selection
 7-
 8-function agp {
 9+agp() {
10   echo $AWS_PROFILE
11 }
12 
13-function asp {
14+# AWS profile selection
15+asp() {
16   if [[ -z "$1" ]]; then
17     unset AWS_DEFAULT_PROFILE AWS_PROFILE AWS_EB_PROFILE
18     echo AWS profile cleared.
19     return
20   fi
21 
22+  local available_profiles=($(aws_profiles))
23+  if [[ -z "${available_profiles[(r)$1]}" ]]; then
24+    echo "${fg[red]}Profile '$1' not found in '${AWS_CONFIG_FILE:-$HOME/.aws/config}'" >&2
25+    echo "Available profiles: ${(j:, :)available_profiles:-no profiles found}${reset_color}" >&2
26+    return 1
27+  fi
28+
29   export AWS_DEFAULT_PROFILE=$1
30   export AWS_PROFILE=$1
31   export AWS_EB_PROFILE=$1
32 }
33 
34-function aws_change_access_key {
35-  if [[ -z "$1" ]] then
36+aws_change_access_key() {
37+  if [[ -z "$1" ]]; then
38     echo "usage: $0 <profile>"
39     return 1
40   fi
41 
42   echo Insert the credentials when asked.
43-  asp "$1"
44+  asp "$1" || return 1
45   aws iam create-access-key
46   aws configure --profile "$1"
47 
48@@ -32,16 +38,18 @@ function aws_change_access_key {
49   aws iam list-access-keys
50 }
51 
52-function aws_profiles {
53+aws_profiles() {
54   [[ -r "${AWS_CONFIG_FILE:-$HOME/.aws/config}" ]] || return 1
55-  reply=($(grep '\[profile' "${AWS_CONFIG_FILE:-$HOME/.aws/config}"|sed -e 's/.*profile \([a-zA-Z0-9_\.-]*\).*/\1/'))
56+  grep '\[profile' "${AWS_CONFIG_FILE:-$HOME/.aws/config}"|sed -e 's/.*profile \([a-zA-Z0-9_\.-]*\).*/\1/'
57 }
58-compctl -K aws_profiles asp aws_change_access_key
59 
60+_aws_profiles() {
61+  reply=($(aws_profiles))
62+}
63+compctl -K _aws_profiles asp aws_change_access_key
64 
65 # AWS prompt
66-
67-function aws_prompt_info() {
68+aws_prompt_info() {
69   [[ -z $AWS_PROFILE ]] && return
70   echo "${ZSH_THEME_AWS_PREFIX:=<aws:}${AWS_PROFILE}${ZSH_THEME_AWS_SUFFIX:=>}"
71 }