379fe0fe131cff7a480f7975b32b0ea6fc7c2370

Author
Julien Rottenberg <julien@rottenberg.info>
Committer
GitHub <noreply@github.com>
Date

Message

feat(azure): add `azure` plugin (#8848)

Co-authored-by: hagridaaron <hagridaaron@gmail.com>
Co-authored-by: Terry <tmoschou@gmail.com>

Closes #8847

Diff

  1diff --git a/lib/prompt_info_functions.zsh b/lib/prompt_info_functions.zsh
  2index e5535848bcd63b7fabaf71c1ee902d1663b09ab9..3dc9b6d10d042ff97e4733684be7df021cb9a25b 100644
  3--- a/lib/prompt_info_functions.zsh
  4+++ b/lib/prompt_info_functions.zsh
  5@@ -18,6 +18,7 @@ function chruby_prompt_info \
  6   vi_mode_prompt_info \
  7   virtualenv_prompt_info \
  8   jenv_prompt_info \
  9+  azure_prompt_info \
 10   tf_prompt_info \
 11 {
 12   return 1
 13diff --git a/plugins/azure/README.md b/plugins/azure/README.md
 14new file mode 100644
 15index 0000000000000000000000000000000000000000..f3993085145310039fb102bd753267b3f0e02e0b
 16--- /dev/null
 17+++ b/plugins/azure/README.md
 18@@ -0,0 +1,49 @@
 19+# azure
 20+
 21+This plugin provides completion support for [azure cli](https://docs.microsoft.com/en-us/cli/azure/)
 22+and a few utilities to manage azure subscriptions and display them in the prompt.
 23+
 24+To use it, add `azure` to the plugins array in your zshrc file.
 25+
 26+```zsh
 27+plugins=(... azure)
 28+```
 29+
 30+## Plugin commands
 31+
 32+
 33+* `az_subscriptions`: lists the available subscriptions in the  `AZURE_CONFIG_DIR` (default: `~/.azure/`).
 34+  Used to provide completion for the `azss` function.
 35+
 36+* `azgs`: gets the current value of `$azure_subscription`.
 37+
 38+* `azss [<subscription>]`: sets the `$azure_subscription`.
 39+
 40+
 41+NOTE : because azure keeps the state of active subscription in ${AZURE_CONFIG_DIR:-$HOME/.azure/azureProfile.json}, the prompt command requires `jq` to be enabled to parse the file. If jq is not in the path the prompt will show nothing
 42+
 43+## Theme
 44+
 45+The plugin creates an `azure_prompt_info` function that you can use in your theme, which displays
 46+the current `$azure_subscription`. It uses two variables to control how that is shown:
 47+
 48+- ZSH_THEME_AZURE_PREFIX: sets the prefix of the azure_subscription. Defaults to `<az:`.
 49+
 50+- ZSH_THEME_azure_SUFFIX: sets the suffix of the azure_subscription. Defaults to `>`.
 51+
 52+
 53+```
 54+RPROMPT='$(azure_prompt_info)'
 55+```
 56+
 57+## Develop
 58+
 59+On ubuntu get a working environment with :
 60+
 61+` docker run -it -v $(pwd):/mnt -w /mnt ubuntu bash`
 62+
 63+```
 64+apt install -y curl jq zsh git vim
 65+sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
 66+curl -sL https://aka.ms/InstallAzureCLIDeb | bash
 67+```
 68diff --git a/plugins/azure/azure.plugin.zsh b/plugins/azure/azure.plugin.zsh
 69new file mode 100644
 70index 0000000000000000000000000000000000000000..7bb173a5c1e0c127f5a20813c2dda47fa4f1d3c1
 71--- /dev/null
 72+++ b/plugins/azure/azure.plugin.zsh
 73@@ -0,0 +1,60 @@
 74+# AZ Get Subscritions
 75+function azgs() {
 76+  az account show --output tsv --query 'name' 2>/dev/null
 77+}
 78+
 79+# AZ Subscription Selection
 80+alias azss="az account set --subscription"
 81+
 82+
 83+function az_subscriptions() {
 84+  az account list  --all --output tsv --query '[*].name' 2> /dev/null
 85+}
 86+
 87+function _az_subscriptions() {
 88+  reply=($(az_subscriptions))
 89+}
 90+compctl -K _az_subscriptions azss
 91+
 92+# Azure prompt
 93+function azure_prompt_info() {
 94+  [[ ! -f "${AZURE_CONFIG_DIR:-$HOME/.azure/azureProfile.json}" ]] && return
 95+  # azgs is too expensive, if we have jq, we enable the prompt
 96+  (( $+commands[jq] )) || return 1
 97+  azgs=$(jq -r '.subscriptions[] | select(.isDefault==true) .name' ${AZURE_CONFIG_DIR:-$HOME/.azure/azureProfile.json})
 98+  echo "${ZSH_THEME_AZURE_PREFIX:=<az:}${azgs}${ZSH_THEME_AZURE_SUFFIX:=>}"
 99+}
100+
101+
102+# Load az completions
103+function _az-homebrew-installed() {
104+  # check if Homebrew is installed
105+  (( $+commands[brew] )) || return 1
106+
107+  # speculatively check default brew prefix
108+  if [ -h /usr/local/opt/az ]; then
109+    _brew_prefix=/usr/local/opt/az
110+  else
111+    # ok, it is not in the default prefix
112+    # this call to brew is expensive (about 400 ms), so at least let's make it only once
113+    _brew_prefix=$(brew --prefix azure-cli)
114+  fi
115+}
116+
117+
118+# get az.completion.sh location from $PATH
119+_az_zsh_completer_path="$commands[az_zsh_completer.sh]"
120+
121+# otherwise check common locations
122+if [[ -z $_az_zsh_completer_path ]]; then
123+  # Homebrew
124+  if _az-homebrew-installed; then
125+    _az_zsh_completer_path=$_brew_prefix/libexec/bin/az.completion.sh
126+  # Linux
127+  else
128+    _az_zsh_completer_path=/etc/bash_completion.d/azure-cli
129+  fi
130+fi
131+
132+[[ -r $_az_zsh_completer_path ]] && source $_az_zsh_completer_path
133+unset _az_zsh_completer_path _brew_prefix