azure.plugin.zsh
1816 bytes
1# AZ Get Subscriptions
2function azgs() {
3 az account show --output tsv --query 'name' 2>/dev/null
4}
5
6# AZ Subscription Selection
7alias azss="az account set --subscription"
8
9
10function az_subscriptions() {
11 az account list --all --output tsv --query '[*].name' 2> /dev/null
12}
13
14function _az_subscriptions() {
15 reply=($(az_subscriptions))
16}
17compctl -K _az_subscriptions azss
18
19# Azure prompt
20function azure_prompt_info() {
21 [[ ! -f "${AZURE_CONFIG_DIR:-$HOME/.azure}/azureProfile.json" ]] && return
22 # azgs is too expensive, if we have jq, we enable the prompt
23 (( $+commands[jq] )) || return 1
24 azgs=$(jq -r '.subscriptions[] | select(.isDefault==true) .name' "${AZURE_CONFIG_DIR:-$HOME/.azure}/azureProfile.json")
25 echo "${ZSH_THEME_AZURE_PREFIX:=<az:}${azgs}${ZSH_THEME_AZURE_SUFFIX:=>}"
26}
27
28
29# Load az completions
30function _az-homebrew-installed() {
31 # check if Homebrew is installed
32 (( $+commands[brew] )) || return 1
33
34 # if so, we assume it's default way to install brew
35 if [[ ${commands[brew]:t2} == bin/brew ]]; then
36 _brew_prefix="${commands[brew]:h:h}" # remove trailing /bin/brew
37 else
38 # ok, it is not in the default prefix
39 # this call to brew is expensive (about 400 ms), so at least let's make it only once
40 _brew_prefix=$(brew --prefix)
41 fi
42}
43
44
45# get az.completion.sh location from $PATH
46_az_zsh_completer_path="$commands[az_zsh_completer.sh]"
47
48# otherwise check common locations
49if [[ -z $_az_zsh_completer_path ]]; then
50 # Homebrew
51 if _az-homebrew-installed; then
52 _az_zsh_completer_path=$_brew_prefix/etc/bash_completion.d/az
53 # Linux
54 else
55 _az_zsh_completer_path=/etc/bash_completion.d/azure-cli
56 fi
57fi
58
59[[ -r $_az_zsh_completer_path ]] && autoload -U +X bashcompinit && bashcompinit && source $_az_zsh_completer_path
60unset _az_zsh_completer_path _brew_prefix