mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2025-12-12 01:52:31 +01:00
FEAT(terraform-workspace-prompt): Improved terraform plugins with support for agnoster theme prompt.
This commit is contained in:
parent
4586808f86
commit
15cd818cd1
3 changed files with 536 additions and 0 deletions
|
|
@ -0,0 +1,35 @@
|
|||
# Global variables to cache the last known .terraform directory and its corresponding workspace name
|
||||
__TERRAFORM_WORKSPACE_CACHE=""
|
||||
__TERRAFORM_DIRECTORY_CACHE=""
|
||||
|
||||
# Function to find the .terraform directory in the current or any parent directory
|
||||
__find_terraform_directory() {
|
||||
local current_dir="$PWD"
|
||||
while [[ "$current_dir" != "/" ]]; do
|
||||
if [[ -d "${current_dir}/.terraform" ]]; then
|
||||
echo "${current_dir}/.terraform"
|
||||
return
|
||||
fi
|
||||
current_dir="$(dirname "$current_dir")"
|
||||
done
|
||||
}
|
||||
|
||||
# Function to update the Terraform workspace prompt based on the current working directory, utilizing the global cache variables
|
||||
__update_terraform_workspace_prompt() {
|
||||
local terraform_dir="$(__find_terraform_directory)"
|
||||
if [[ -n "$terraform_dir" && "$terraform_dir" != "$__TERRAFORM_DIRECTORY_CACHE" ]]; then
|
||||
__TERRAFORM_DIRECTORY_CACHE="$terraform_dir"
|
||||
local workspace="$(terraform -chdir="$(dirname "${terraform_dir%/}")" workspace show 2>/dev/null)"
|
||||
__TERRAFORM_WORKSPACE_CACHE="$workspace"
|
||||
elif [[ -z "$terraform_dir" ]]; then
|
||||
__TERRAFORM_DIRECTORY_CACHE=""
|
||||
__TERRAFORM_WORKSPACE_CACHE=""
|
||||
fi
|
||||
}
|
||||
|
||||
# Hooks to call the update function when the working directory changes or before each prompt is displayed
|
||||
autoload -Uz add-zsh-hook
|
||||
add-zsh-hook chpwd __update_terraform_workspace_prompt
|
||||
add-zsh-hook precmd __update_terraform_workspace_prompt
|
||||
|
||||
complete -o nospace -C $(which terraform) terraform
|
||||
Loading…
Add table
Add a link
Reference in a new issue