diff options
author | Julien Rottenberg <julien@rottenberg.info> | 2023-02-07 03:33:59 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-07 12:33:59 +0100 |
commit | 379fe0fe131cff7a480f7975b32b0ea6fc7c2370 (patch) | |
tree | 4a7aeea5ee07d340ae18828df805ece676d19380 | |
parent | 3fd63fdf01344bb5f5f13a9c33eb0b7a72fe4771 (diff) | |
download | zsh-379fe0fe131cff7a480f7975b32b0ea6fc7c2370.tar.gz zsh-379fe0fe131cff7a480f7975b32b0ea6fc7c2370.tar.bz2 zsh-379fe0fe131cff7a480f7975b32b0ea6fc7c2370.zip |
feat(azure): add `azure` plugin (#8848)
Co-authored-by: hagridaaron <hagridaaron@gmail.com>
Co-authored-by: Terry <tmoschou@gmail.com>
Closes #8847
-rw-r--r-- | lib/prompt_info_functions.zsh | 1 | ||||
-rw-r--r-- | plugins/azure/README.md | 49 | ||||
-rw-r--r-- | plugins/azure/azure.plugin.zsh | 60 |
3 files changed, 110 insertions, 0 deletions
diff --git a/lib/prompt_info_functions.zsh b/lib/prompt_info_functions.zsh index e5535848b..3dc9b6d10 100644 --- a/lib/prompt_info_functions.zsh +++ b/lib/prompt_info_functions.zsh @@ -18,6 +18,7 @@ function chruby_prompt_info \ vi_mode_prompt_info \ virtualenv_prompt_info \ jenv_prompt_info \ + azure_prompt_info \ tf_prompt_info \ { return 1 diff --git a/plugins/azure/README.md b/plugins/azure/README.md new file mode 100644 index 000000000..f39930851 --- /dev/null +++ b/plugins/azure/README.md @@ -0,0 +1,49 @@ +# azure + +This plugin provides completion support for [azure cli](https://docs.microsoft.com/en-us/cli/azure/) +and a few utilities to manage azure subscriptions and display them in the prompt. + +To use it, add `azure` to the plugins array in your zshrc file. + +```zsh +plugins=(... azure) +``` + +## Plugin commands + + +* `az_subscriptions`: lists the available subscriptions in the `AZURE_CONFIG_DIR` (default: `~/.azure/`). + Used to provide completion for the `azss` function. + +* `azgs`: gets the current value of `$azure_subscription`. + +* `azss [<subscription>]`: sets the `$azure_subscription`. + + +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 + +## Theme + +The plugin creates an `azure_prompt_info` function that you can use in your theme, which displays +the current `$azure_subscription`. It uses two variables to control how that is shown: + +- ZSH_THEME_AZURE_PREFIX: sets the prefix of the azure_subscription. Defaults to `<az:`. + +- ZSH_THEME_azure_SUFFIX: sets the suffix of the azure_subscription. Defaults to `>`. + + +``` +RPROMPT='$(azure_prompt_info)' +``` + +## Develop + +On ubuntu get a working environment with : + +` docker run -it -v $(pwd):/mnt -w /mnt ubuntu bash` + +``` +apt install -y curl jq zsh git vim +sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" +curl -sL https://aka.ms/InstallAzureCLIDeb | bash +```
\ No newline at end of file diff --git a/plugins/azure/azure.plugin.zsh b/plugins/azure/azure.plugin.zsh new file mode 100644 index 000000000..7bb173a5c --- /dev/null +++ b/plugins/azure/azure.plugin.zsh @@ -0,0 +1,60 @@ +# AZ Get Subscritions +function azgs() { + az account show --output tsv --query 'name' 2>/dev/null +} + +# AZ Subscription Selection +alias azss="az account set --subscription" + + +function az_subscriptions() { + az account list --all --output tsv --query '[*].name' 2> /dev/null +} + +function _az_subscriptions() { + reply=($(az_subscriptions)) +} +compctl -K _az_subscriptions azss + +# Azure prompt +function azure_prompt_info() { + [[ ! -f "${AZURE_CONFIG_DIR:-$HOME/.azure/azureProfile.json}" ]] && return + # azgs is too expensive, if we have jq, we enable the prompt + (( $+commands[jq] )) || return 1 + azgs=$(jq -r '.subscriptions[] | select(.isDefault==true) .name' ${AZURE_CONFIG_DIR:-$HOME/.azure/azureProfile.json}) + echo "${ZSH_THEME_AZURE_PREFIX:=<az:}${azgs}${ZSH_THEME_AZURE_SUFFIX:=>}" +} + + +# Load az completions +function _az-homebrew-installed() { + # check if Homebrew is installed + (( $+commands[brew] )) || return 1 + + # speculatively check default brew prefix + if [ -h /usr/local/opt/az ]; then + _brew_prefix=/usr/local/opt/az + else + # ok, it is not in the default prefix + # this call to brew is expensive (about 400 ms), so at least let's make it only once + _brew_prefix=$(brew --prefix azure-cli) + fi +} + + +# get az.completion.sh location from $PATH +_az_zsh_completer_path="$commands[az_zsh_completer.sh]" + +# otherwise check common locations +if [[ -z $_az_zsh_completer_path ]]; then + # Homebrew + if _az-homebrew-installed; then + _az_zsh_completer_path=$_brew_prefix/libexec/bin/az.completion.sh + # Linux + else + _az_zsh_completer_path=/etc/bash_completion.d/azure-cli + fi +fi + +[[ -r $_az_zsh_completer_path ]] && source $_az_zsh_completer_path +unset _az_zsh_completer_path _brew_prefix |