azure.plugin.zsh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # AZ Get Subscriptions
  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. # if so, we assume it's default way to install brew
  27. if [[ ${commands[brew]:t2} == bin/brew ]]; then
  28. _brew_prefix="${commands[brew]:h:h}" # remove trailing /bin/brew
  29. else
  30. # ok, it is not in the default prefix
  31. # this call to brew is expensive (about 400 ms), so at least let's make it only once
  32. _brew_prefix=$(brew --prefix)
  33. fi
  34. }
  35. # get az.completion.sh location from $PATH
  36. _az_zsh_completer_path="$commands[az_zsh_completer.sh]"
  37. # otherwise check common locations
  38. if [[ -z $_az_zsh_completer_path ]]; then
  39. # Homebrew
  40. if _az-homebrew-installed; then
  41. _az_zsh_completer_path=$_brew_prefix/etc/bash_completion.d/az
  42. # Linux
  43. else
  44. _az_zsh_completer_path=/etc/bash_completion.d/azure-cli
  45. fi
  46. fi
  47. [[ -r $_az_zsh_completer_path ]] && autoload -U +X bashcompinit && bashcompinit && source $_az_zsh_completer_path
  48. unset _az_zsh_completer_path _brew_prefix