Skip to content

Commit 1bc02cd

Browse files
committed
Init release
1 parent 8994448 commit 1bc02cd

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
1-
# azure-subscription-prompt
1+
# Azure Subscription Prompt
2+
3+
This script displays information about the Azure current Subscription and tenant.
4+
5+
This script heavily inspired by [zsh-kubectl-prompt](https://github.com/superbrothers/zsh-kubectl-prompt)
6+
7+
![Azure Subscription Prompt](images/azure-subscription-prompt.png)
8+
9+
10+
## Usage
11+
12+
1. Clone this repository into `$HOME/.oh-my-zsh/custom/plugins.
13+
2. Add plugin name to `.zshrc`
14+
3. Add following lines into `.zshrc`
15+
16+
```bash
17+
autoload -U colors; colors
18+
RPROMPT='%{$fg[blue]%}($ZSH_SUBSCRIPTION_PROMPT)%{$reset_color%}'
19+
```
20+
4. Reload terminal or update `.zshrc`
21+
22+
```bash
23+
source ~/.zshrc
24+
```
25+
26+
## License
27+
28+
This script is released under the MIT License.

azure-prompt.zsh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source ${0:A:h}/azure-prompt.zsh
25.1 KB
Loading

0 commit comments

Comments
 (0)