Browse Source

feat(terraform): add terraform version prompt (#11845)

Jin Cheol 9 months ago
parent
commit
b81915d329
2 changed files with 15 additions and 5 deletions
  1. 8 5
      plugins/terraform/README.md
  2. 7 0
      plugins/terraform/terraform.plugin.zsh

+ 8 - 5
plugins/terraform/README.md

@@ -1,7 +1,7 @@
 # Terraform plugin
 
-Plugin for Terraform, a tool from Hashicorp for managing infrastructure safely and efficiently.
-It adds completion for `terraform`, as well as aliases and a prompt function.
+Plugin for Terraform, a tool from Hashicorp for managing infrastructure safely and efficiently. It adds
+completion for `terraform`, as well as aliases and a prompt function.
 
 To use it, add `terraform` to the plugins array of your `~/.zshrc` file:
 
@@ -11,7 +11,7 @@ plugins=(... terraform)
 
 ## Requirements
 
-* [Terraform](https://terraform.io/)
+- [Terraform](https://terraform.io/)
 
 ## Aliases
 
@@ -29,11 +29,12 @@ plugins=(... terraform)
 
 ## Prompt function
 
-You can add the current Terraform workspace in your prompt by adding `$(tf_prompt_info)`
-to your `PROMPT` or `RPROMPT` variable.
+You can add the current Terraform workspace in your prompt by adding `$(tf_prompt_info)`,
+`$(tf_version_prompt_info)` to your `PROMPT` or `RPROMPT` variable.
 
 ```sh
 RPROMPT='$(tf_prompt_info)'
+RPROMPT='$(tf_version_prompt_info)'
 ```
 
 You can also specify the PREFIX and SUFFIX for the workspace with the following variables:
@@ -41,4 +42,6 @@ You can also specify the PREFIX and SUFFIX for the workspace with the following
 ```sh
 ZSH_THEME_TF_PROMPT_PREFIX="%{$fg[white]%}"
 ZSH_THEME_TF_PROMPT_SUFFIX="%{$reset_color%}"
+ZSH_THEME_TF_VERSION_PROMPT_PREFIX="%{$fg[white]%}"
+ZSH_THEME_TF_VERSION_PROMPT_SUFFIX="%{$reset_color%}"
 ```

+ 7 - 0
plugins/terraform/terraform.plugin.zsh

@@ -8,6 +8,13 @@ function tf_prompt_info() {
   echo "${ZSH_THEME_TF_PROMPT_PREFIX-[}${workspace:gs/%/%%}${ZSH_THEME_TF_PROMPT_SUFFIX-]}"
 }
 
+function tf_version_prompt_info() {
+    local terraform_version
+    terraform_version=$(terraform --version | head -n 1 | cut -d ' ' -f 2)
+    echo "${ZSH_THEME_TF_VERSION_PROMPT_PREFIX-[}${terraform_version:gs/%/%%}${ZSH_THEME_TF_VERSION_PROMPT_SUFFIX-]}"
+}
+
+
 alias tf='terraform'
 alias tfa='terraform apply'
 alias tfc='terraform console'