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/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..ec52facc2 100644 --- a/segments/kubecontext/kubecontext.p9k +++ b/segments/kubecontext/kubecontext.p9k @@ -36,6 +36,12 @@ prompt_kubecontext() { cur_namespace="default" 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/} + fi + local k8s_final_text="" if [[ "$cur_ctx" == "$cur_namespace" ]]; then 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 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