05bf69c604e34300b02406462d4376c86d595470

Author
0rxa <reiarifi@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(aws): implement permanent state (#12018)

Co-authored-by: Rei Arifi <reiarifi@Reis-MacBook-Pro.local>
Co-authored-by: Hysen Ndregjoni <hndregjoni@hotmail.com>

Diff

 1diff --git a/plugins/aws/README.md b/plugins/aws/README.md
 2index dbf8dcfeb8b829f760232a1fdbf882bd5934202a..0d0773f6364e3bbcd0f847a72076ff4cbd73db3a 100644
 3--- a/plugins/aws/README.md
 4+++ b/plugins/aws/README.md
 5@@ -47,6 +47,11 @@ plugins=(... aws)
 6   Some themes might overwrite the value of RPROMPT instead of appending to it, so they'll need to be fixed to
 7   see the AWS profile/region prompt.
 8 
 9+* Set `AWS_PROFILE_STATE_ENABLED=true` in your zshrc file if you want the aws profile to persist between shell sessions.
10+  This option might slow down your shell startup time.
11+  By default the state file path is `/tmp/.aws_current_profile`. This means that the state won't survive a reboot or otherwise GC.
12+  You can control the state file path using the `AWS_STATE_FILE` environment variable.
13+
14 ## Theme
15 
16 The plugin creates an `aws_prompt_info` function that you can use in your theme, which displays
17diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh
18index 7a2e08a7ca7c21e5521a634d3562ddb991ddbfcc..071dd1f0b9a9f84ec2d3426f0c392b713c65be0d 100644
19--- a/plugins/aws/aws.plugin.zsh
20+++ b/plugins/aws/aws.plugin.zsh
21@@ -6,10 +6,26 @@ function agr() {
22   echo $AWS_REGION
23 }
24 
25+# Update state file if enabled
26+function _aws_update_state() {
27+  if [[ "$AWS_PROFILE_STATE_ENABLED" == true ]]; then
28+    test -d $(dirname ${AWS_STATE_FILE}) || exit 1
29+    echo "${AWS_PROFILE} ${AWS_REGION}" > "${AWS_STATE_FILE}"
30+  fi
31+}
32+
33+function _aws_clear_state() {
34+  if [[ "$AWS_PROFILE_STATE_ENABLED" == true ]]; then
35+    test -d $(dirname ${AWS_STATE_FILE}) || exit 1
36+    echo -n > "${AWS_STATE_FILE}"
37+  fi
38+}
39+
40 # AWS profile selection
41 function asp() {
42   if [[ -z "$1" ]]; then
43     unset AWS_DEFAULT_PROFILE AWS_PROFILE AWS_EB_PROFILE AWS_PROFILE_REGION
44+    _aws_clear_state
45     echo AWS profile cleared.
46     return
47   fi
48@@ -28,6 +44,8 @@ function asp() {
49 
50   export AWS_PROFILE_REGION=$(aws configure get region)
51 
52+  _aws_update_state
53+
54   if [[ "$2" == "login" ]]; then
55     if [[ -n "$3" ]]; then
56       aws sso login --sso-session $3
57@@ -43,6 +61,7 @@ function asp() {
58 function asr() {
59   if [[ -z "$1" ]]; then
60     unset AWS_DEFAULT_REGION AWS_REGION
61+    _aws_update_state
62     echo AWS region cleared.
63     return
64   fi
65@@ -56,6 +75,7 @@ function asr() {
66 
67   export AWS_REGION=$1
68   export AWS_DEFAULT_REGION=$1
69+  _aws_update_state
70 }
71 
72 # AWS profile switch
73@@ -255,6 +275,22 @@ if [[ "$SHOW_AWS_PROMPT" != false && "$RPROMPT" != *'$(aws_prompt_info)'* ]]; th
74   RPROMPT='$(aws_prompt_info)'"$RPROMPT"
75 fi
76 
77+if [[ "$AWS_PROFILE_STATE_ENABLED" == true ]]; then
78+  AWS_STATE_FILE="${AWS_STATE_FILE:-/tmp/.aws_current_profile}"
79+  test -s "${AWS_STATE_FILE}" || return
80+
81+  aws_state=($(cat $AWS_STATE_FILE))
82+  
83+  export AWS_DEFAULT_PROFILE="${aws_state[1]}"
84+  export AWS_PROFILE="$AWS_DEFAULT_PROFILE"
85+  export AWS_EB_PROFILE="$AWS_DEFAULT_PROFILE"
86+
87+  test -z "${aws_state[2]}" && AWS_REGION=$(aws configure get region)
88+
89+  export AWS_REGION=${AWS_REGION:-$aws_state[2]}
90+  export AWS_DEFAULT_REGION="$AWS_REGION"
91+fi
92+
93 # Load awscli completions
94 
95 # AWS CLI v2 comes with its own autocompletion. Check if that is there, otherwise fall back