1+ setopt prompt_subst
2+ autoload -U add-zsh-hook
3+
4+ function () {
5+ local subscription_id separator binary
6+
7+ # Specify the separator between Tenant and Subscription
8+ zstyle -s ' :zsh-subscription-prompt:' separator separator
9+ if [[ -z " $separator " ]]; then
10+ zstyle ' :zsh-subscription-prompt:' separator ' /'
11+ fi
12+
13+ # Display the current subscription if `subscription` is true
14+ zstyle -s ' :zsh-subscription-prompt:' subscription_id subscription_id
15+ if [[ -z " $subscription_id " ]]; then
16+ zstyle ' :zsh-subscription-prompt:' subscription_id true
17+ fi
18+
19+ # Specify the binary to get the information
20+ zstyle -s ' :zsh-subscription-binary:' binary binary
21+ if [[ -z " $binary " ]]; then
22+ zstyle ' :zsh-subscription-prompt:' binary " jq"
23+ fi
24+ }
25+
26+ add-zsh-hook precmd _zsh_subscription_prompt_precmd
27+ function _zsh_subscription_prompt_precmd() {
28+ local updated_at now tenant subscription_id subscription_name separator modified_time_fmt binary
29+
30+ # Check if binary is present
31+ zstyle -s ' :zsh-subscription-prompt:' binary binary
32+ if ! command -v " $binary " > /dev/null; then
33+ ZSH_SUBSCRIPTION_PROMPT=" ${binary} command not found"
34+ return 1
35+ fi
36+
37+ zstyle -s ' :zsh-subscription-prompt:' modified_time_fmt modified_time_fmt
38+ if [[ -z " $modified_time_fmt " ]]; then
39+ # Check the stat command because it has a different syntax between GNU coreutils and FreeBSD.
40+ if stat --help > /dev/null 2>&1 ; then
41+ modified_time_fmt=' -c%y' # GNU coreutils
42+ else
43+ modified_time_fmt=' -f%m' # FreeBSD
44+ fi
45+ zstyle ' :zsh-subscription-prompt:' modified_time_fmt $modified_time_fmt
46+ fi
47+
48+ zstyle ' :zsh-subscription-prompt:' updated_at " $now "
49+
50+ azure_config=" $HOME /.azure/azureProfile.json"
51+ if [ ! -f $azure_config ]; then
52+ return 1
53+ fi
54+
55+ subscription_name=` $binary -r ' .subscriptions[] | select(.isDefault == true) | .name' $azure_config `
56+ tenant=` $binary -r ' .subscriptions[] | select(.isDefault == true) | .tenantId' $azure_config `
57+ # Specify the entry before prompt (default empty)
58+ zstyle -s ' :zsh-subscription-prompt:' preprompt preprompt
59+ # Specify the entry after prompt (default empty)
60+ zstyle -s ' :zsh-subscription-prompt:' postprompt postprompt
61+
62+ # Set environment variable without tenant
63+ zstyle -s ' :zsh-kubectl-prompt:' tenant tenant
64+ if [[ " $tenant " == true ]]; then
65+ ZSH_SUBSCRIPTION_PROMPT=" ${preprompt}${tenant}${separator}${subscription_name}${postprompt} "
66+ return 0
67+ fi
68+
69+ zstyle -s ' :zsh-subscription-prompt:' separator separator
70+ ZSH_SUBSCRIPTION_PROMPT=" ${preprompt}${subscription_name}${postprompt} "
71+
72+ return 0
73+ }
0 commit comments