From f717a91f55f4d5dfb89d8828dc5b36fcc45548c2 Mon Sep 17 00:00:00 2001 From: Alejandro Gandara Date: Wed, 14 Jul 2021 22:52:22 +0100 Subject: [PATCH 1/3] =?UTF-8?q?*=20Added=20new=20module=20for=20showing=20?= =?UTF-8?q?in=20prompt=20the=20active=20terraform=20version.=20*=20Prompt?= =?UTF-8?q?=20shows,=20the=20terraform=20version=20in=20this=20format:=20T?= =?UTF-8?q?erraform=20v0.12.13=20=EF=86=BB.=20If=20only=20if=20a=20valid?= =?UTF-8?q?=20terraform=20command=20is=20used,=20or=20an=20alias=20to=20it?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The use case: We have a need to constantly change between terraform versions depending on what project we are working on, it is easy to by mistake upgrade the wrong project to the latest terraform version. This is why the prompt is shown in red as soon as you type terraform, this is a good way to remind you to double check this is the terraform version you want to use. Future improvements: Detect current terraform version by checking the terraform workspace state version, and change the terraform color prompt from red to green if you're using the same version. --- config/p10k-classic.zsh | 1 + config/p10k-lean-8colors.zsh | 1 + config/p10k-lean.zsh | 1 + config/p10k-rainbow.zsh | 1 + internal/p10k.zsh | 9 +++++++++ 5 files changed, 13 insertions(+) diff --git a/config/p10k-classic.zsh b/config/p10k-classic.zsh index 19c602bc..53dc0e66 100644 --- a/config/p10k-classic.zsh +++ b/config/p10k-classic.zsh @@ -70,6 +70,7 @@ haskell_stack # haskell version from stack (https://haskellstack.org/) kubecontext # current kubernetes context (https://kubernetes.io/) terraform # terraform workspace (https://www.terraform.io) + terraform_version # It shows current active terraform version aws # aws profile (https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html) aws_eb_env # aws elastic beanstalk environment (https://aws.amazon.com/elasticbeanstalk/) azure # azure account name (https://docs.microsoft.com/en-us/cli/azure) diff --git a/config/p10k-lean-8colors.zsh b/config/p10k-lean-8colors.zsh index 9d0ab9a1..93a454d2 100644 --- a/config/p10k-lean-8colors.zsh +++ b/config/p10k-lean-8colors.zsh @@ -70,6 +70,7 @@ haskell_stack # haskell version from stack (https://haskellstack.org/) kubecontext # current kubernetes context (https://kubernetes.io/) terraform # terraform workspace (https://www.terraform.io) + terraform_version # It shows current active terraform version aws # aws profile (https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html) aws_eb_env # aws elastic beanstalk environment (https://aws.amazon.com/elasticbeanstalk/) azure # azure account name (https://docs.microsoft.com/en-us/cli/azure) diff --git a/config/p10k-lean.zsh b/config/p10k-lean.zsh index 9d9dcf75..f6e48806 100644 --- a/config/p10k-lean.zsh +++ b/config/p10k-lean.zsh @@ -70,6 +70,7 @@ haskell_stack # haskell version from stack (https://haskellstack.org/) kubecontext # current kubernetes context (https://kubernetes.io/) terraform # terraform workspace (https://www.terraform.io) + terraform_version # It shows current active terraform version aws # aws profile (https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html) aws_eb_env # aws elastic beanstalk environment (https://aws.amazon.com/elasticbeanstalk/) azure # azure account name (https://docs.microsoft.com/en-us/cli/azure) diff --git a/config/p10k-rainbow.zsh b/config/p10k-rainbow.zsh index 42b69929..71a1bfba 100644 --- a/config/p10k-rainbow.zsh +++ b/config/p10k-rainbow.zsh @@ -70,6 +70,7 @@ haskell_stack # haskell version from stack (https://haskellstack.org/) kubecontext # current kubernetes context (https://kubernetes.io/) terraform # terraform workspace (https://www.terraform.io) + terraform_version # It shows current active terraform version aws # aws profile (https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html) aws_eb_env # aws elastic beanstalk environment (https://aws.amazon.com/elasticbeanstalk/) azure # azure account name (https://docs.microsoft.com/en-us/cli/azure) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 935a86d9..07cc280a 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4887,6 +4887,15 @@ _p9k_prompt_terraform_init() { typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[terraform]' } +function prompt_terraform_version() { + local terraform_version="$(command terraform --version)" + _p9k_prompt_segment "$0$state" $_p9k_color1 red TERRAFORM_ICON 0 '' $terraform_version +} + +_p9k_prompt_terraform_version_init() { + typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[terraform]' +} + function prompt_proxy() { local -U p=( $all_proxy $http_proxy $https_proxy $ftp_proxy From 9f98915167b35bc045090c106b8249f6dadfaf01 Mon Sep 17 00:00:00 2001 From: Alejandro Gandara Date: Thu, 15 Jul 2021 00:44:37 +0100 Subject: [PATCH 2/3] =?UTF-8?q?*=20Added=20new=20module=20for=20showing=20?= =?UTF-8?q?in=20prompt=20the=20active=20terraform=20version.=20*=20Prompt?= =?UTF-8?q?=20shows,=20the=20terraform=20version=20in=20this=20format:=20T?= =?UTF-8?q?erraform=20v0.12.13=20=EF=86=BB.=20If=20only=20if=20a=20valid?= =?UTF-8?q?=20terraform=20command=20is=20used,=20or=20an=20alias=20to=20it?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The use case: We have a need to constantly change between terraform versions depending on what project we are working on, it is easy to by mistake upgrade the wrong project to the latest terraform version. This is why the prompt is shown in red as soon as you type terraform, this is a good way to remind you to double check this is the terraform version you want to use. Future improvements: Detect current terraform version by checking the terraform workspace state version, and change the terraform color prompt from red to green if you're using the same version. --- config/p10k-classic.zsh | 3 +++ config/p10k-rainbow.zsh | 3 +++ 2 files changed, 6 insertions(+) diff --git a/config/p10k-classic.zsh b/config/p10k-classic.zsh index 53dc0e66..0407c354 100644 --- a/config/p10k-classic.zsh +++ b/config/p10k-classic.zsh @@ -1205,6 +1205,9 @@ typeset -g POWERLEVEL9K_TERRAFORM_OTHER_FOREGROUND=38 # typeset -g POWERLEVEL9K_TERRAFORM_OTHER_VISUAL_IDENTIFIER_EXPANSION='⭐' + ################[ terraform_version: It shows active terraform version (https://www.terraform.io) ]################# + typeset -g POWERLEVEL9K_TERRAFORM_VERSION_SHOW_ON_COMMAND='terraform|tf' + #############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]############# # Show kubecontext only when the the command you are typing invokes one of these tools. # Tip: Remove the next line to always show kubecontext. diff --git a/config/p10k-rainbow.zsh b/config/p10k-rainbow.zsh index 71a1bfba..f0d800bd 100644 --- a/config/p10k-rainbow.zsh +++ b/config/p10k-rainbow.zsh @@ -1272,6 +1272,9 @@ typeset -g POWERLEVEL9K_TERRAFORM_OTHER_BACKGROUND=0 # typeset -g POWERLEVEL9K_TERRAFORM_OTHER_VISUAL_IDENTIFIER_EXPANSION='⭐' + ################[ terraform_version: It shows active terraform version (https://www.terraform.io) ]################# + typeset -g POWERLEVEL9K_TERRAFORM_VERSION_SHOW_ON_COMMAND='terraform|tf' + #############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]############# # Show kubecontext only when the the command you are typing invokes one of these tools. # Tip: Remove the next line to always show kubecontext. From dae5f7f1c9ef5116b046f1f9b31a9bdf2154e76b Mon Sep 17 00:00:00 2001 From: Alejandro Gandara Date: Fri, 16 Jul 2021 20:39:23 +0100 Subject: [PATCH 3/3] * Added cache to terraform --version --- internal/p10k.zsh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 07cc280a..510bf3c5 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4888,8 +4888,10 @@ _p9k_prompt_terraform_init() { } function prompt_terraform_version() { - local terraform_version="$(command terraform --version)" - _p9k_prompt_segment "$0$state" $_p9k_color1 red TERRAFORM_ICON 0 '' $terraform_version + _p9k_cached_cmd 0 '' terraform --version + [[ $_p9k__ret == (#b)Terraform\ v([[:digit:].]##)* ]] + local terraform_version=$match[1] + _p9k_prompt_segment "$0$state" $_p9k_color1 red TERRAFORM_ICON 0 '' "tf:$terraform_version" } _p9k_prompt_terraform_version_init() {