From 8ef2b737d1f6099966a1eb16bdfc90d67b367f22 Mon Sep 17 00:00:00 2001
From: romkatv <roman.perepelitsa@gmail.com>
Date: Thu, 19 Dec 2019 18:46:31 +0100
Subject: rename gcloud_app to google_app_cred and change its api

---
 config/p10k-classic.zsh | 39 ++++++++++++++++++++++++++-------------
 config/p10k-lean.zsh    | 39 ++++++++++++++++++++++++++-------------
 config/p10k-rainbow.zsh | 43 +++++++++++++++++++++++++++++--------------
 internal/p10k.zsh       | 35 ++++++++++++++++++-----------------
 4 files changed, 99 insertions(+), 57 deletions(-)

diff --git a/config/p10k-classic.zsh b/config/p10k-classic.zsh
index adf90c76..5ce31958 100644
--- a/config/p10k-classic.zsh
+++ b/config/p10k-classic.zsh
@@ -68,7 +68,7 @@
       # aws_eb_env            # aws elastic beanstalk environment (https://aws.amazon.com/elasticbeanstalk/)
       # azure                 # azure account name (https://docs.microsoft.com/en-us/cli/azure)
       # gcloud                # google cloud cli acccount and project (https://cloud.google.com/)
-      # gcloud_app            # google cloud application credentials (https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable)
+      # google_app_cred       # google application credentials (https://cloud.google.com/docs/authentication/production)
       context                 # user@hostname
       nordvpn                 # nordvpn connection status, linux only (https://nordvpn.com/)
       ranger                  # ranger shell (https://github.com/ranger/ranger)
@@ -786,22 +786,35 @@
   # Custom icon.
   # typeset -g POWERLEVEL9K_GCLOUD_VISUAL_IDENTIFIER_EXPANSION='⭐'
 
-  ##########[ gcloud_app: google cloud application credentials (https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable) ]###########
-  # Google cloud color.
-  typeset -g POWERLEVEL9K_GCLOUD_APP_FOREGROUND=32
+  #[ google_app_cred: google application credentials (https://cloud.google.com/docs/authentication/production) ]#
+  # Default google application credentials color.
+  typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_FOREGROUND=32
+  # Google application credentials color for service accounts.
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_SERVICE_ACCOUNT_FOREGROUND=32
 
-  # Google cloud format. Uncomment POWERLEVEL9K_GCLOUD_APP_CONTENT_EXPANSION and edit its value if the
-  # default is too verbose.
+  # Google application credentials format. Uncomment POWERLEVEL9K_GOOGLE_APP_CRED_CONTENT_EXPANSION
+  # and edit its value if the default is too verbose. You can use the following parameters in the
+  # expansion. Each of them corresponds to one of the fields in the JSON file pointed to by
+  # GOOGLE_APPLICATION_CREDENTIALS.
   #
-  #   P9K_GCLOUD_APP_EMAIL: `.client_email` field of keyfile
-  #   P9K_GCLOUD_APP_ACCOUNT_TYPE: `.type` field of keyfile
-  #   P9K_GCLOUD_APP_ACCOUNT_TYPE_SHORT: `sa` if `type=service_account`, empty otherwise
-  #   ${VARIABLE//\%/%%}: ${VARIABLE} with all occurences of '%' replaced with '%%'.
+  #   Parameter                        | JSON key file field
+  #   ---------------------------------+---------------
+  #   P9K_GOOGLE_APP_CRED_TYPE         | type
+  #   P9K_GOOGLE_APP_CRED_PROJECT_ID   | project_id
+  #   P9K_GOOGLE_APP_CRED_CLIENT_EMAIL | client_email
   #
-  # typeset -g POWERLEVEL9K_GCLOUD_APP_CONTENT_EXPANSION='${P9K_GCLOUD_APP_ACCOUNT_TYPE_SHORT//\%/%%}${P9K_GCLOUD_APP_EMAIL//\%/%%}'
+  # Note: ${VARIABLE%%.*} expands to ${VARIABLE} up to but not including the first period ('.').
+  # Note: ${VARIABLE//\%/%%} expands to ${VARIABLE} with all occurences of '%' replaced with '%%'.
+  #
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_CONTENT_EXPANSION='${${P9K_GOOGLE_APP_CRED_CLIENT_EMAIL%%.*}//\%/%%}'
+  #
+  # You can also define content expansion specifically for service accounts by defining
+  # POWERLEVEL9K_GOOGLE_APP_CRED_SERVICE_ACCOUNT_CONTENT_EXPANSION.
 
-  # Custom icon.
-  # typeset -g POWERLEVEL9K_GCLOUD_APP_VISUAL_IDENTIFIER_EXPANSION='⭐'
+  # Default google application credentials icon.
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_VISUAL_IDENTIFIER_EXPANSION='⭐'
+  # Google application credentials icon for service accounts.
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_SERVICE_ACCOUNT_VISUAL_IDENTIFIER_EXPANSION='⭐'
 
   #############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]#############
   # Kubernetes context classes for the purpose of using different colors, icons and expansions with
diff --git a/config/p10k-lean.zsh b/config/p10k-lean.zsh
index 574f654c..937bdbac 100644
--- a/config/p10k-lean.zsh
+++ b/config/p10k-lean.zsh
@@ -68,7 +68,7 @@
       # aws_eb_env            # aws elastic beanstalk environment (https://aws.amazon.com/elasticbeanstalk/)
       # azure                 # azure account name (https://docs.microsoft.com/en-us/cli/azure)
       # gcloud                # google cloud cli acccount and project (https://cloud.google.com/)
-      # gcloud_app            # google cloud application credentials (https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable)
+      # google_app_cred       # google application credentials (https://cloud.google.com/docs/authentication/production)
       context                 # user@hostname
       nordvpn                 # nordvpn connection status, linux only (https://nordvpn.com/)
       ranger                  # ranger shell (https://github.com/ranger/ranger)
@@ -843,22 +843,35 @@
   # Custom icon.
   # typeset -g POWERLEVEL9K_GCLOUD_VISUAL_IDENTIFIER_EXPANSION='⭐'
 
-  ##########[ gcloud_app: google cloud application credentials (https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable) ]###########
-  # Google cloud color.
-  typeset -g POWERLEVEL9K_GCLOUD_APP_FOREGROUND=32
+  #[ google_app_cred: google application credentials (https://cloud.google.com/docs/authentication/production) ]#
+  # Default google application credentials color.
+  typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_FOREGROUND=32
+  # Google application credentials color for service accounts.
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_SERVICE_ACCOUNT_FOREGROUND=32
 
-  # Google cloud format. Uncomment POWERLEVEL9K_GCLOUD_APP_CONTENT_EXPANSION and edit its value if the
-  # default is too verbose.
+  # Google application credentials format. Uncomment POWERLEVEL9K_GOOGLE_APP_CRED_CONTENT_EXPANSION
+  # and edit its value if the default is too verbose. You can use the following parameters in the
+  # expansion. Each of them corresponds to one of the fields in the JSON file pointed to by
+  # GOOGLE_APPLICATION_CREDENTIALS.
   #
-  #   P9K_GCLOUD_APP_EMAIL: `.client_email` field of keyfile
-  #   P9K_GCLOUD_APP_ACCOUNT_TYPE: `.type` field of keyfile
-  #   P9K_GCLOUD_APP_ACCOUNT_TYPE_SHORT: `sa` if `type=service_account`, empty otherwise
-  #   ${VARIABLE//\%/%%}: ${VARIABLE} with all occurences of '%' replaced with '%%'.
+  #   Parameter                        | JSON key file field
+  #   ---------------------------------+---------------
+  #   P9K_GOOGLE_APP_CRED_TYPE         | type
+  #   P9K_GOOGLE_APP_CRED_PROJECT_ID   | project_id
+  #   P9K_GOOGLE_APP_CRED_CLIENT_EMAIL | client_email
   #
-  # typeset -g POWERLEVEL9K_GCLOUD_APP_CONTENT_EXPANSION='${P9K_GCLOUD_APP_ACCOUNT_TYPE_SHORT//\%/%%}${P9K_GCLOUD_APP_EMAIL//\%/%%}'
+  # Note: ${VARIABLE%%.*} expands to ${VARIABLE} up to but not including the first period ('.').
+  # Note: ${VARIABLE//\%/%%} expands to ${VARIABLE} with all occurences of '%' replaced with '%%'.
+  #
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_CONTENT_EXPANSION='${${P9K_GOOGLE_APP_CRED_CLIENT_EMAIL%%.*}//\%/%%}'
+  #
+  # You can also define content expansion specifically for service accounts by defining
+  # POWERLEVEL9K_GOOGLE_APP_CRED_SERVICE_ACCOUNT_CONTENT_EXPANSION.
 
-  # Custom icon.
-  # typeset -g POWERLEVEL9K_GCLOUD_APP_VISUAL_IDENTIFIER_EXPANSION='⭐'
+  # Default google application credentials icon.
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_VISUAL_IDENTIFIER_EXPANSION='⭐'
+  # Google application credentials icon for service accounts.
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_SERVICE_ACCOUNT_VISUAL_IDENTIFIER_EXPANSION='⭐'
 
   ###############################[ public_ip: public IP address ]###############################
   # Public IP color.
diff --git a/config/p10k-rainbow.zsh b/config/p10k-rainbow.zsh
index 677448b6..a81714ce 100644
--- a/config/p10k-rainbow.zsh
+++ b/config/p10k-rainbow.zsh
@@ -68,7 +68,7 @@
       # aws_eb_env            # aws elastic beanstalk environment (https://aws.amazon.com/elasticbeanstalk/)
       # azure                 # azure account name (https://docs.microsoft.com/en-us/cli/azure)
       # gcloud                # google cloud cli acccount and project (https://cloud.google.com/)
-      # gcloud_app            # google cloud application credentials (https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable)
+      # google_app_cred       # google application credentials (https://cloud.google.com/docs/authentication/production)
       context                 # user@hostname
       nordvpn                 # nordvpn connection status, linux only (https://nordvpn.com/)
       ranger                  # ranger shell (https://github.com/ranger/ranger)
@@ -811,22 +811,37 @@
   # Custom icon.
   # typeset -g POWERLEVEL9K_GCLOUD_VISUAL_IDENTIFIER_EXPANSION='⭐'
 
-  ##########[ gcloud_app: google cloud application credentials (https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable) ]###########
-  # Google cloud color.
-  typeset -g POWERLEVEL9K_GCLOUD_APP_FOREGROUND=32
-
-  # Google cloud format. Uncomment POWERLEVEL9K_GCLOUD_APP_CONTENT_EXPANSION and edit its value if the
-  # default is too verbose.
+  #[ google_app_cred: google application credentials (https://cloud.google.com/docs/authentication/production) ]#
+  # Default google application credentials color.
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_FOREGROUND=7
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_BACKGROUND=4
+  # Google application credentials color for service accounts.
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_SERVICE_ACCOUNT_FOREGROUND=7
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_SERVICE_ACCOUNT_BACKGROUND=4
+
+  # Google application credentials format. Uncomment POWERLEVEL9K_GOOGLE_APP_CRED_CONTENT_EXPANSION
+  # and edit its value if the default is too verbose. You can use the following parameters in the
+  # expansion. Each of them corresponds to one of the fields in the JSON file pointed to by
+  # GOOGLE_APPLICATION_CREDENTIALS.
   #
-  #   P9K_GCLOUD_APP_EMAIL: `.client_email` field of keyfile
-  #   P9K_GCLOUD_APP_ACCOUNT_TYPE: `.type` field of keyfile
-  #   P9K_GCLOUD_APP_ACCOUNT_TYPE_SHORT: `sa` if `type=service_account`, empty otherwise
-  #   ${VARIABLE//\%/%%}: ${VARIABLE} with all occurences of '%' replaced with '%%'.
+  #   Parameter                        | JSON key file field
+  #   ---------------------------------+---------------
+  #   P9K_GOOGLE_APP_CRED_TYPE         | type
+  #   P9K_GOOGLE_APP_CRED_PROJECT_ID   | project_id
+  #   P9K_GOOGLE_APP_CRED_CLIENT_EMAIL | client_email
   #
-  # typeset -g POWERLEVEL9K_GCLOUD_APP_CONTENT_EXPANSION='${P9K_GCLOUD_APP_ACCOUNT_TYPE_SHORT//\%/%%}${P9K_GCLOUD_APP_EMAIL//\%/%%}'
+  # Note: ${VARIABLE%%.*} expands to ${VARIABLE} up to but not including the first period ('.').
+  # Note: ${VARIABLE//\%/%%} expands to ${VARIABLE} with all occurences of '%' replaced with '%%'.
+  #
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_CONTENT_EXPANSION='${${P9K_GOOGLE_APP_CRED_CLIENT_EMAIL%%.*}//\%/%%}'
+  #
+  # You can also define content expansion specifically for service accounts by defining
+  # POWERLEVEL9K_GOOGLE_APP_CRED_SERVICE_ACCOUNT_CONTENT_EXPANSION.
 
-  # Custom icon.
-  # typeset -g POWERLEVEL9K_GCLOUD_APP_VISUAL_IDENTIFIER_EXPANSION='⭐'
+  # Default google application credentials icon.
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_VISUAL_IDENTIFIER_EXPANSION='⭐'
+  # Google application credentials icon for service accounts.
+  # typeset -g POWERLEVEL9K_GOOGLE_APP_CRED_SERVICE_ACCOUNT_VISUAL_IDENTIFIER_EXPANSION='⭐'
 
   #############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]#############
   # Kubernetes context classes for the purpose of using different colors, icons and expansions with
diff --git a/internal/p10k.zsh b/internal/p10k.zsh
index 5235aa01..abdacd03 100644
--- a/internal/p10k.zsh
+++ b/internal/p10k.zsh
@@ -3388,27 +3388,28 @@ prompt_gcloud() {
   _p9k_prompt_segment "$0" "blue" "white" "GCLOUD_ICON" 0 '' "${P9K_GCLOUD_ACCOUNT//\%/%%}:${P9K_GCLOUD_PROJECT//\%/%%}"
 }
 
-prompt_gcloud_app() {
-  unset P9K_GCLOUD_APP_EMAIL P9K_GCLOUD_APP_ACCOUNT_TYPE P9K_GCLOUD_APP_ACCOUNT_TYPE_SHORT
-  (( $+commands[gcloud] )) || return
-  [[ ! -z $GOOGLE_APPLICATION_CREDENTIALS ]] || return
+prompt_google_app_cred() {
+  unset P9K_GOOGLE_APP_CRED_{TYPE,PROJECT_ID,CLIENT_EMAIL}
+  [[ -n $GOOGLE_APPLICATION_CREDENTIALS ]] || return
+  (( $+commands[jq] )) || return
 
   if ! _p9k_cache_stat_get $0 $GOOGLE_APPLICATION_CREDENTIALS; then
-    local email="$(cat $GOOGLE_APPLICATION_CREDENTIALS | jq -r '.client_email')"
-    local account_type="$(cat $GOOGLE_APPLICATION_CREDENTIALS | jq -r '.type')"
-    local account_type_short
-    if [[ "$account_type" == "service_account" ]]; then
-      account_type_short="sa:"
+    local -a lines
+    local q='[.type//"", .project_id//"", .client_email//"", 0][]'
+    if lines=("${(@f)$(jq -r $q <$GOOGLE_APPLICATION_CREDENTIALS 2>/dev/null)}") && (( $#lines == 4 )); then
+      _p9k_cache_stat_set 1 "${(@)lines[1,-2]}"
+    else
+      _p9k_cache_stat_set 0
     fi
-
-    # Service account name may contain only alpha-numeric chars and hyphens, so splitting by `.` gives us `service-account-name@project-id`
-    _p9k_cache_stat_set "${email%%.*}" "$account_type" "$account_type_short"
   fi
-  [[ -n $_p9k_cache_val[1] || -n $_p9k_cache_val[2] || -n $_p9k_cache_val[3] ]] || return
-  P9K_GCLOUD_APP_EMAIL=$_p9k_cache_val[1]
-  P9K_GCLOUD_APP_ACCOUNT_TYPE=$_p9k_cache_val[2]
-  P9K_GCLOUD_APP_ACCOUNT_TYPE_SHORT=$_p9k_cache_val[3]
-  _p9k_prompt_segment "$0" "blue" "white" "GCLOUD_ICON" 0 '' "${P9K_GCLOUD_APP_ACCOUNT_TYPE_SHORT//\%/%%}${P9K_GCLOUD_APP_EMAIL//\%/%%}"
+
+  (( _p9k_cache_val[1] )) || return
+  P9K_GOOGLE_APP_CRED_TYPE=$_p9k_cache_val[2]
+  P9K_GOOGLE_APP_CRED_PROJECT_ID=$_p9k_cache_val[3]
+  P9K_GOOGLE_APP_CRED_CLIENT_EMAIL=$_p9k_cache_val[4]
+
+  [[ -n $P9K_GOOGLE_APP_CRED_TYPE ]] && local state=_${(U)P9K_GOOGLE_APP_CRED_TYPE} || local state
+  _p9k_prompt_segment "$0$state" "blue" "white" "GCLOUD_ICON" 0 '' "${${P9K_GOOGLE_APP_CRED_CLIENT_EMAIL%%.*}//\%/%%}"
 }
 
 typeset -gra __p9k_nordvpn_tag=(
-- 
cgit v1.2.3