From c9b6a0b53c91194ef83804cabc6f4d3717e39fc5 Mon Sep 17 00:00:00 2001 From: Joseph Campos Date: Tue, 2 Apr 2019 11:14:34 -0700 Subject: [PATCH 1/7] Adding the ability to strip cluster name from AWS ARN if using EKS --- segments/kubecontext/README.md | 9 ++++++++- segments/kubecontext/kubecontext.p9k | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/segments/kubecontext/README.md b/segments/kubecontext/README.md index f033428f9..4ec5b4d0b 100644 --- a/segments/kubecontext/README.md +++ b/segments/kubecontext/README.md @@ -21,4 +21,11 @@ P9K_KUBECONTEXT_BACKGROUND='blue' ### Customize Icon The main Icon can be changed by setting `P9K_KUBECONTEXT_ICON="my_icon"`. To change the -icon color only, set `P9K_KUBECONTEXT_ICON_COLOR="red"`. \ No newline at end of file +icon color only, set `P9K_KUBECONTEXT_ICON_COLOR="red"`. + +### Strip Cluster and Namespace from AWS ARN (If using EKS) + +You may extract the clutser name and namespace from the AWS ARN that is supplied when you use an EKS cluster. This is useful when you really just want to see the cluster name and do not want the full ARN. +``` +P9K_KUBECONTEXT_STRIPEKS=true +``` \ No newline at end of file diff --git a/segments/kubecontext/kubecontext.p9k b/segments/kubecontext/kubecontext.p9k index 65c69f46c..ce84b7bee 100644 --- a/segments/kubecontext/kubecontext.p9k +++ b/segments/kubecontext/kubecontext.p9k @@ -45,6 +45,11 @@ prompt_kubecontext() { k8s_final_text="$cur_ctx/$cur_namespace" fi + # Extract cluster name and namespace from AWS ARN, if enabled. + if [[ "$P9K_KUBECONTEXT_STRIPEKS" == true ]]; then + k8s_final_text="$(echo $k8s_final_text | cut -d':' -f 6 | sed 's/cluster\/*//')" + fi + p9k::prepare_segment "$0" "" $1 "$2" $3 "$k8s_final_text" fi } From bd8616781d1fbfabc47ff0fd873fa216aa1ea464 Mon Sep 17 00:00:00 2001 From: Joseph Campos Date: Tue, 2 Apr 2019 11:26:15 -0700 Subject: [PATCH 2/7] Had the field extract in wrong spot, this is better --- segments/kubecontext/kubecontext.p9k | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/segments/kubecontext/kubecontext.p9k b/segments/kubecontext/kubecontext.p9k index ce84b7bee..6a9f916a7 100644 --- a/segments/kubecontext/kubecontext.p9k +++ b/segments/kubecontext/kubecontext.p9k @@ -36,6 +36,11 @@ prompt_kubecontext() { cur_namespace="default" fi + # Extract cluster name and namespace from AWS ARN, if enabled. + if [[ "$P9K_KUBECONTEXT_STRIPEKS" == true ]]; then + cur_ctx="$(echo $cur_ctx | cut -d':' -f 6 | sed 's/cluster\/*//')" + fi + local k8s_final_text="" if [[ "$cur_ctx" == "$cur_namespace" ]]; then @@ -45,11 +50,6 @@ prompt_kubecontext() { k8s_final_text="$cur_ctx/$cur_namespace" fi - # Extract cluster name and namespace from AWS ARN, if enabled. - if [[ "$P9K_KUBECONTEXT_STRIPEKS" == true ]]; then - k8s_final_text="$(echo $k8s_final_text | cut -d':' -f 6 | sed 's/cluster\/*//')" - fi - p9k::prepare_segment "$0" "" $1 "$2" $3 "$k8s_final_text" fi } From 7278e24c09dbafefff763ecde99276b2347a7c39 Mon Sep 17 00:00:00 2001 From: Joseph Campos Date: Wed, 3 Apr 2019 14:10:49 -0700 Subject: [PATCH 3/7] Adding strip eks test, and providing mock AWS ARN string --- segments/kubecontext/kubecontext.spec | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/segments/kubecontext/kubecontext.spec b/segments/kubecontext/kubecontext.spec index 91a774985..bf8a794f4 100755 --- a/segments/kubecontext/kubecontext.spec +++ b/segments/kubecontext/kubecontext.spec @@ -101,4 +101,44 @@ function testKubeContextPrintsNothingIfKubectlNotAvailable() { unalias kubectl } +function mockKubectlEKS() { + case "$1" in + 'version') + echo 'non-empty text' + ;; + 'config') + case "$2" in + 'view') + case "$3" in + '-o=jsonpath={.current-context}') + echo 'arn:aws:eks:us-east-1:123456789012:cluster/eksname' + ;; + '-o=jsonpath={.contexts'*) + echo '' + ;; + *) + echo "Mock value missed" + exit 1 + ;; + esac + ;; + esac + ;; + esac +} + +function testStripEKS() { + alias kubectl=mockKubectlEKS + + P9K_KUBECONTEXT_STRIPEKS=true + + local -a P9K_LEFT_PROMPT_ELEMENTS + P9K_LEFT_PROMPT_ELEMENTS=(kubecontext) + + assertEquals "%K{004} %F{015}⎈ %F{015}eksname/default %k%F{004}%f " "$(__p9k_build_left_prompt)" + + unset P9K_LEFT_PROMPT_ELEMENTS + unalias kubectl +} + source shunit2/shunit2 From d566802d2bab73c15abc94cf9c5e3d72ea506bed Mon Sep 17 00:00:00 2001 From: Joseph Campos Date: Wed, 3 Apr 2019 14:41:49 -0700 Subject: [PATCH 4/7] Replacing external binaries with string maniuplation. --- segments/kubecontext/kubecontext.p9k | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/segments/kubecontext/kubecontext.p9k b/segments/kubecontext/kubecontext.p9k index 6a9f916a7..a7e0df446 100644 --- a/segments/kubecontext/kubecontext.p9k +++ b/segments/kubecontext/kubecontext.p9k @@ -38,7 +38,8 @@ prompt_kubecontext() { # Extract cluster name and namespace from AWS ARN, if enabled. if [[ "$P9K_KUBECONTEXT_STRIPEKS" == true ]]; then - cur_ctx="$(echo $cur_ctx | cut -d':' -f 6 | sed 's/cluster\/*//')" + cur_ctx=${cur_ctx#*:*:*:*:*:} + cur_ctx=${cur_ctx:8} fi local k8s_final_text="" From 9934af4ee46c5bf109547c6feb7dd5f6f78bc31e Mon Sep 17 00:00:00 2001 From: Joseph Campos Date: Wed, 3 Apr 2019 14:47:25 -0700 Subject: [PATCH 5/7] Updating to more compact string maniuplation that @dritter provided. --- segments/kubecontext/kubecontext.p9k | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/segments/kubecontext/kubecontext.p9k b/segments/kubecontext/kubecontext.p9k index a7e0df446..7f12c44af 100644 --- a/segments/kubecontext/kubecontext.p9k +++ b/segments/kubecontext/kubecontext.p9k @@ -38,8 +38,7 @@ prompt_kubecontext() { # Extract cluster name and namespace from AWS ARN, if enabled. if [[ "$P9K_KUBECONTEXT_STRIPEKS" == true ]]; then - cur_ctx=${cur_ctx#*:*:*:*:*:} - cur_ctx=${cur_ctx:8} + cur_ctx=${${cur_ctx##*\:}/cluster\//} fi local k8s_final_text="" From c20e1533de65d352b0120a929c099fa24fc84689 Mon Sep 17 00:00:00 2001 From: Joseph Campos Date: Wed, 3 Apr 2019 16:59:06 -0700 Subject: [PATCH 6/7] Updating to cleaner syntax and adding example string to reference when working on the pattern matching --- segments/kubecontext/kubecontext.p9k | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/segments/kubecontext/kubecontext.p9k b/segments/kubecontext/kubecontext.p9k index 7f12c44af..ec52facc2 100644 --- a/segments/kubecontext/kubecontext.p9k +++ b/segments/kubecontext/kubecontext.p9k @@ -37,8 +37,9 @@ prompt_kubecontext() { fi # Extract cluster name and namespace from AWS ARN, if enabled. + # Example eks cluster string: arn:aws:eks:us-east-1:123456789012:cluster/eksname if [[ "$P9K_KUBECONTEXT_STRIPEKS" == true ]]; then - cur_ctx=${${cur_ctx##*\:}/cluster\//} + cur_ctx=${cur_ctx#*cluster/} fi local k8s_final_text="" From 9245dd977d7dcb43d58d32adaed6e41712c650e6 Mon Sep 17 00:00:00 2001 From: Joseph Campos Date: Tue, 16 Apr 2019 13:53:26 -0700 Subject: [PATCH 7/7] Adding terraform segment and tests --- README.md | 2 ++ segments/terraform/README.md | 27 ++++++++++++++++ segments/terraform/terraform.p9k | 36 ++++++++++++++++++++++ segments/terraform/terraform.spec | 51 +++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 segments/terraform/README.md create mode 100644 segments/terraform/terraform.p9k create mode 100755 segments/terraform/terraform.spec diff --git a/README.md b/README.md index 3193cc8da..6df4d9f01 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,8 @@ The segments that are currently available are: * [`java_version`](segments/java_version/README.md) - Show the current Java version. * **Haskell Segments:** * [`stack_project`](segments/stack_project/README.md) - Show if you are in a Haskell Stack project directory. +* **Terraform Segments:** + * [`terraform`](segments/terraform/README.md) - Display the selected terraform workspace. **Cloud Segments:** * **AWS Segments:** diff --git a/segments/terraform/README.md b/segments/terraform/README.md new file mode 100644 index 000000000..6390701af --- /dev/null +++ b/segments/terraform/README.md @@ -0,0 +1,27 @@ +# Terraform Workspace + +Displays the currently selected workspace within your state file. Works by inspecting your .terraform/environment file. + +![](segment.png) + +## Installation + +To use this segment, you need to activate it by adding `terraform` to your +`P9K_LEFT_PROMPT_ELEMENTS` or `P9K_RIGHT_PROMPT_ELEMENTS` array, depending +where you want to show this segment. + +## Configuration + +### Color Customization + +You can change the foreground and background color of this segment by setting. +``` +P9K_TERRAFORM_FOREGROUND='red' +P9K_TERRAFORM_BACKGROUND='blue' +``` + +### Customize Icon + +The main Icon can be changed by setting `P9K_TERRAFORM_ICON="my_icon"`. To change the +icon color only, set `P9K_TERRAFORM_ICON_COLOR="red"`. Using a globe for now until a +terraform specific icon is included in font packages. diff --git a/segments/terraform/terraform.p9k b/segments/terraform/terraform.p9k new file mode 100644 index 000000000..2a5ee7853 --- /dev/null +++ b/segments/terraform/terraform.p9k @@ -0,0 +1,36 @@ +# vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8 +################################################################ +# @title powerlevel9k Segment - Terraform Workspace +# @source [powerlevel9k](https://github.com/bhilburn/powerlevel9k) +## + +(){ + # Set the right locale to protect special characters + local LC_ALL="" LC_CTYPE="en_US.UTF-8" + ################################################################ + # Register segment + # Parameters: + # segment_name context background foreground Generic Flat/Awesome-Patched Awesome-FontConfig Awesome-Mapped-FontConfig NerdFont + # ⎈ ⎈ ⎈ ⎈ ⎈ + p9k::register_segment "TERRAFORM" "" "purple" "black" "TF: " $'\uF484' $'\uF484' $'\uF484' $'\uF484' +} + +################################################################ +# @description +# Display the select Terraform Workspace. +## +# @args +# $1 string Alignment - left | right +# $2 integer Segment index +# $3 boolean Whether the segment should be joined +## +prompt_terraform() { + local tf_workspace="" + + if [[ -e .terraform/environment ]]; then + tf_workspace=$(< ./.terraform/environment) + tf_workspace=${tf_workspace%%[[:space:]]} + fi + + p9k::prepare_segment "$0" "" $1 "$2" $3 "$tf_workspace" +} diff --git a/segments/terraform/terraform.spec b/segments/terraform/terraform.spec new file mode 100755 index 000000000..40c3418dd --- /dev/null +++ b/segments/terraform/terraform.spec @@ -0,0 +1,51 @@ +#!/usr/bin/env zsh +#vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8 + +# Required for shunit2 to run correctly +setopt shwordsplit +SHUNIT_PARENT=$0 + +function setUp() { + export TERM="xterm-256color" + local -a P9K_RIGHT_PROMPT_ELEMENTS + P9K_RIGHT_PROMPT_ELEMENTS=() + # Load Powerlevel9k + source powerlevel9k.zsh-theme + source segments/terraform/terraform.p9k +} + +function testTerraformEnvironment() { + local -a P9K_LEFT_PROMPT_ELEMENTS + P9K_LEFT_PROMPT_ELEMENTS=(terraform) + + # Setup mock data, there is a .terraform/environment file showing the selected + # environment/workspace so we will key off of that as its faster than running + # a 'terraform workspace list' + if [ -d '.terraform' ]; then + echo "Bailing out, theres an active .terraform directory here!" + else + touch placeholder.tf # This is needed because we only run prompt when your inside a directory with .tf files in it + mkdir .terraform + touch ./.terraform/environment + echo "workspace-name" > ./.terraform/environment + + assertEquals "%K{093} %F{000}TF: %F{000}workspace-name %k%F{093}%f " "$(__p9k_build_left_prompt)" + + rm placeholder.tf + rm -rf .terraform + fi + + unset P9K_LEFT_PROMPT_ELEMENTS +} + +function testTerraformNoEnvironment() { + local -a P9K_LEFT_PROMPT_ELEMENTS + P9K_LEFT_PROMPT_ELEMENTS=(terraform) + + # No .terraform or workspace detected so show none. + assertEquals "%k%FNONE%f " "$(__p9k_build_left_prompt)" + + unset P9K_LEFT_PROMPT_ELEMENTS +} + +source shunit2/shunit2