azure.plugin.zsh 1.8 KB

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