Skip to content

Commit

Permalink
🎨 Consistent colours for environments in prompts (#150)
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Woffenden <jacob.woffenden@digital.justice.gov.uk>
  • Loading branch information
Jacob Woffenden authored Oct 28, 2024
1 parent a188779 commit 47aad5e
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 41 deletions.
4 changes: 4 additions & 0 deletions features/src/aws/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion features/src/aws/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
`'
4 changes: 4 additions & 0 deletions features/src/cloud-platform/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion features/src/cloud-platform/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
`'
4 changes: 4 additions & 0 deletions features/src/kubernetes/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion features/src/kubernetes/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
`'

0 comments on commit 47aad5e

Please sign in to comment.