From 47aad5e067aa2435ba1d70e86c31a52cdc02775f Mon Sep 17 00:00:00 2001 From: Jacob Woffenden Date: Mon, 28 Oct 2024 17:10:53 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Consistent=20colours=20for=20env?= =?UTF-8?q?ironments=20in=20prompts=20(#150)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jacob Woffenden --- features/src/aws/CHANGELOG.md | 4 ++ features/src/aws/devcontainer-feature.json | 2 +- .../.devcontainer/promptrc.d/aws-sso-cli.sh | 29 +++++++---- features/src/cloud-platform/CHANGELOG.md | 4 ++ .../cloud-platform/devcontainer-feature.json | 2 +- .../promptrc.d/cloud-platform.sh | 52 +++++++++++++------ features/src/kubernetes/CHANGELOG.md | 4 ++ .../src/kubernetes/devcontainer-feature.json | 2 +- .../promptrc.d/kubernetes-cli.sh | 38 +++++++++----- 9 files changed, 96 insertions(+), 41 deletions(-) diff --git a/features/src/aws/CHANGELOG.md b/features/src/aws/CHANGELOG.md index eeb825e..b1913cd 100644 --- a/features/src/aws/CHANGELOG.md +++ b/features/src/aws/CHANGELOG.md @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.0.1] - 2024-10-28 + +- Updated environment colour map + ## [1.0.0] - 2024-05-13 - Promoting to 1.0.0 diff --git a/features/src/aws/devcontainer-feature.json b/features/src/aws/devcontainer-feature.json index a723a68..ae8d967 100644 --- a/features/src/aws/devcontainer-feature.json +++ b/features/src/aws/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "aws", - "version": "1.0.0", + "version": "1.0.1", "name": "AWS", "documentationURL": "https://github.com/ministryofjustice/.devcontainer/tree/main/features/src/aws", "description": "Installs the AWS CLI and AWS SSO CLI", diff --git a/features/src/aws/src/home/vscode/.devcontainer/promptrc.d/aws-sso-cli.sh b/features/src/aws/src/home/vscode/.devcontainer/promptrc.d/aws-sso-cli.sh index 6a9618f..a10cebf 100644 --- a/features/src/aws/src/home/vscode/.devcontainer/promptrc.d/aws-sso-cli.sh +++ b/features/src/aws/src/home/vscode/.devcontainer/promptrc.d/aws-sso-cli.sh @@ -2,13 +2,24 @@ # shellcheck disable=SC2016 PROMPT+='`\ - if [[ ${AWS_SSO_PROFILE} == *"development"* || ${AWS_SSO_PROFILE} == *"test"* ]]; then \ - echo -n "[ aws: %{$fg[green]%}${AWS_SSO_PROFILE}@${AWS_DEFAULT_REGION}%{$reset_color%} ] "; \ - elif [[ ${AWS_SSO_PROFILE} == *"preproduction"* ]]; then \ - echo -n "[ aws: %{$fg[yellow]%}${AWS_SSO_PROFILE}@${AWS_DEFAULT_REGION}%{$reset_color%} ] "; \ - elif [[ ${AWS_SSO_PROFILE} == *"production"* ]]; then \ - echo -n "[ aws: %{$fg[red]%}${AWS_SSO_PROFILE}@${AWS_DEFAULT_REGION}%{$reset_color%} ] "; \ - elif [[ ! -z ${AWS_SSO_PROFILE} ]]; then \ - echo -n "[ aws: %{$fg[blue]%}${AWS_SSO_PROFILE}@${AWS_DEFAULT_REGION}%{$reset_color%} ] "; \ - fi \ + case ${AWS_SSO_PROFILE} in \ + *"development"*|*"sandbox"*) \ + colour=green \ + ;; \ + *"test"*) \ + colour=blue \ + ;; \ + *"preproduction"*) \ + colour=yellow \ + ;; \ + *"production"*) \ + colour=red \ + ;; \ + *) \ + colour=white \ + ;; \ + esac; \ + if [[ ! -z ${AWS_SSO_PROFILE} ]]; then \ + echo -n "[ aws: %{$fg[$colour]%}${AWS_SSO_PROFILE}@${AWS_DEFAULT_REGION}%{$reset_color%} ] "; \ + fi `' diff --git a/features/src/cloud-platform/CHANGELOG.md b/features/src/cloud-platform/CHANGELOG.md index 05967fd..0f325ca 100644 --- a/features/src/cloud-platform/CHANGELOG.md +++ b/features/src/cloud-platform/CHANGELOG.md @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.0.1] - 2024-10-28 + +- Updated environment colour map + ## [1.0.0] - 2024-10-17 ## Changed diff --git a/features/src/cloud-platform/devcontainer-feature.json b/features/src/cloud-platform/devcontainer-feature.json index 80bf0ce..ea23e24 100644 --- a/features/src/cloud-platform/devcontainer-feature.json +++ b/features/src/cloud-platform/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "cloud-platform", - "version": "1.0.0", + "version": "1.0.1", "name": "Cloud Platform", "description": "Installs the Cloud Platform CLI", "options": { diff --git a/features/src/cloud-platform/src/home/vscode/.devcontainer/promptrc.d/cloud-platform.sh b/features/src/cloud-platform/src/home/vscode/.devcontainer/promptrc.d/cloud-platform.sh index 8e57526..9a9b5e6 100644 --- a/features/src/cloud-platform/src/home/vscode/.devcontainer/promptrc.d/cloud-platform.sh +++ b/features/src/cloud-platform/src/home/vscode/.devcontainer/promptrc.d/cloud-platform.sh @@ -1,21 +1,41 @@ #!/usr/bin/env bash # shellcheck disable=SC2016 +# Logic for authentication check is: +# Check context is "cloud-platform-live" and client-id is "REPLACE_WITH_CLIENT_ID", then you aren't authenticated +# Check context is "cloud-platform-live" and client-id is not "REPLACE_WITH_CLIENT_ID" and server version is returned, then you are authenticated +# Check context is "cloud-platform-live" and client-id is not "REPLACE_WITH_CLIENT_ID" and server version is null, then there is a potential issue with authentication + PROMPT+='`\ - kubectlCurrentContext=$(kubectl config current-context 2>/dev/null); \ - kubectlCurrentNamespace=$(kubectl config view --minify --output "jsonpath={..namespace}"); \ - kubectlClientId=$(kubectl config view --output json | jq -r ".users[0].user[\"auth-provider\"].config[\"client-id\"]"); \ - kubectlServerVersion=$(kubectl version --output json 2>/dev/null | jq -r .serverVersion.gitVersion); \ - if [[ "${kubectlCurrentContext}" == "cloud-platform-live" ]] && [[ "${kubectlClientId}" == "REPLACE_WITH_CLIENT_ID" ]]; then \ - echo -n "[ cluster: %{$fg[yellow]%}${kubectlCurrentContext} (requires authentication)%{$reset_color%} ] "; \ - elif [[ "${kubectlCurrentContext}" == "cloud-platform-live" ]] && [[ "${kubectlClientId}" != "REPLACE_WITH_CLIENT_ID" ]] && [[ "${kubectlServerVersion}" != "null" ]]; then \ - echo -n "[ cluster: %{$fg[green]%}${kubectlCurrentContext} (authenticated)%{$reset_color%} ] "; \ - elif [[ "${kubectlCurrentContext}" == "cloud-platform-live" ]] && [[ "${kubectlClientId}" != "REPLACE_WITH_CLIENT_ID" ]] && [[ "${kubectlServerVersion}" == "null" ]]; then - echo -n "[ cluster: %{$fg[red]%}${kubectlCurrentContext} (issue with authentication)%{$reset_color%} ] "; \ - fi \ - && if [[ "${kubectlCurrentContext}" == "cloud-platform-live" ]] && [[ ! -z "${kubectlCurrentNamespace}" ]] && [[ "${kubectlCurrentNamespace}" == *"-prod"* ]]; then \ - echo -n "[ namespace: %{$fg[red]%}${kubectlCurrentNamespace}%{$reset_color%} ] "; \ - elif [[ "${kubectlCurrentContext}" == "cloud-platform-live" ]] && [[ ! -z "${kubectlCurrentNamespace}" ]]; then \ - echo -n "[ namespace: %{$fg[green]%}${kubectlCurrentNamespace}%{$reset_color%} ] "; \ - fi \ + CURRENT_CONTEXT=$(kubectl config current-context 2>/dev/null); \ + CURRENT_NAMESPACE=$(kubectl config view --minify --output "jsonpath={..namespace}" 2>/dev/null); \ + CURRENT_CLIENT_ID=$(kubectl config view --output json | jq -r ".users[0].user[\"auth-provider\"].config[\"client-id\"]"); \ + SERVER_VERSION=$(kubectl version --output json 2>/dev/null | jq -r .serverVersion.gitVersion); \ + if [[ "${CURRENT_CONTEXT}" == "cloud-platform-live" ]] && [[ "${CURRENT_CLIENT_ID}" == "REPLACE_WITH_CLIENT_ID" ]]; then \ + echo -n "[ cluster: %{$fg[yellow]%}${CURRENT_CONTEXT} (requires authentication)%{$reset_color%} ] "; \ + elif [[ "${CURRENT_CONTEXT}" == "cloud-platform-live" ]] && [[ "${CURRENT_CLIENT_ID}" != "REPLACE_WITH_CLIENT_ID" ]] && [[ "${SERVER_VERSION}" != "null" ]]; then \ + echo -n "[ cluster: %{$fg[green]%}${CURRENT_CONTEXT} (authenticated)%{$reset_color%} ] "; \ + elif [[ "${CURRENT_CONTEXT}" == "cloud-platform-live" ]] && [[ "${CURRENT_CLIENT_ID}" != "REPLACE_WITH_CLIENT_ID" ]] && [[ "${SERVER_VERSION}" == "null" ]]; then + echo -n "[ cluster: %{$fg[red]%}${CURRENT_CONTEXT} (issue with authentication)%{$reset_color%} ] "; \ + fi; \ + case ${CURRENT_NAMESPACE} in \ + *"development"*|*"sandbox"*) \ + colour=green \ + ;; \ + *"test"*) \ + colour=blue \ + ;; \ + *"preproduction"*) \ + colour=yellow \ + ;; \ + *"production"*) \ + colour=red \ + ;; \ + *) \ + colour=white \ + ;; \ + esac; \ + if [[ ! -z ${CURRENT_NAMESPACE} ]]; then \ + echo -n "[ namespace: %{$fg[$colour]%}${CURRENT_NAMESPACE}%{$reset_color%} ] "; \ + fi `' diff --git a/features/src/kubernetes/CHANGELOG.md b/features/src/kubernetes/CHANGELOG.md index 6fb0375..3d0d2e4 100644 --- a/features/src/kubernetes/CHANGELOG.md +++ b/features/src/kubernetes/CHANGELOG.md @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.0.2] - 2024-10-28 + +- Updated environment colour map + ## [1.0.1] - 2024-05-23 ### Changed diff --git a/features/src/kubernetes/devcontainer-feature.json b/features/src/kubernetes/devcontainer-feature.json index b97a1d6..6e07de9 100644 --- a/features/src/kubernetes/devcontainer-feature.json +++ b/features/src/kubernetes/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "kubernetes", - "version": "1.0.1", + "version": "1.0.2", "name": "Kubernetes", "description": "Installs the Kubernetes CLI, Helm CLI and Helm Chart Testing CLI", "options": { diff --git a/features/src/kubernetes/src/home/vscode/.devcontainer/promptrc.d/kubernetes-cli.sh b/features/src/kubernetes/src/home/vscode/.devcontainer/promptrc.d/kubernetes-cli.sh index d3b5688..8c3c29a 100644 --- a/features/src/kubernetes/src/home/vscode/.devcontainer/promptrc.d/kubernetes-cli.sh +++ b/features/src/kubernetes/src/home/vscode/.devcontainer/promptrc.d/kubernetes-cli.sh @@ -2,17 +2,29 @@ # shellcheck disable=SC2016 PROMPT+='`\ - export KUBE_CURRENT_CONTEXT=$(kubectl config current-context 2>/dev/null); \ - export KUBE_CURRENT_NAMESPACE=$(kubectl config view --minify --output "jsonpath={..namespace}" 2>/dev/null); \ - if [[ "${KUBE_CURRENT_CONTEXT}" == *"development"* ]]; then \ - echo -n "[ context: %{$fg[green]%}${KUBE_CURRENT_CONTEXT}%{$reset_color%} ] "; \ - elif [[ "${KUBE_CURRENT_CONTEXT}" == *"production"* ]]; then \ - echo -n "[ context: %{$fg[red]%}${KUBE_CURRENT_CONTEXT}%{$reset_color%} ] "; \ - elif [[ ! -z "${KUBE_CURRENT_CONTEXT}" ]]; then \ - echo -n "[ context: %{$fg[yellow]%}${KUBE_CURRENT_CONTEXT}%{$reset_color%} ] "; \ - fi \ - && \ - if [[ ! -z "${KUBE_CURRENT_NAMESPACE}" ]]; then \ - echo -n "[ namespace: %{$fg[white]%}${KUBE_CURRENT_NAMESPACE}%{$reset_color%} ] "; \ - fi \ + CURRENT_CONTEXT=$(kubectl config current-context 2>/dev/null); \ + CURRENT_NAMESPACE=$(kubectl config view --minify --output "jsonpath={..namespace}" 2>/dev/null); \ + case ${CURRENT_CONTEXT} in \ + *"development"*|*"sandbox"*) \ + colour=green \ + ;; \ + *"test"*) \ + colour=blue \ + ;; \ + *"preproduction"*) \ + colour=yellow \ + ;; \ + *"production"*) \ + colour=red \ + ;; \ + *) \ + colour=white \ + ;; \ + esac; \ + if [[ ! -z ${CURRENT_CONTEXT} ]]; then \ + echo -n "[ context: %{$fg[$colour]%}${CURRENT_CONTEXT}%{$reset_color%} ] "; \ + fi; \ + if [[ ! -z ${CURRENT_NAMESPACE} ]]; then \ + echo -n "[ namespace: ${CURRENT_NAMESPACE} ] "; \ + fi `'