From b2c7d17f0a8320895c3b52df8485c645398854e1 Mon Sep 17 00:00:00 2001 From: Ruben Suarez Alvarez Date: Tue, 13 Aug 2024 22:30:03 +0200 Subject: [PATCH] =?UTF-8?q?feat(kubectl-helm-minikube):=20=F0=9F=A7=91?= =?UTF-8?q?=E2=80=8D=F0=9F=92=BB=20add=20helm=20completion=20(#1080)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(kubectl-helm-minikube): 🧑‍💻 add helm completion Like **kubectl**, **helm** supports **bash** and **zsh** command completion. * test(kubectl-helm-minikube): ✅ test helm bash completion * chore(kubectl-helm-minikube): 🔧 update feature version --- .../devcontainer-feature.json | 2 +- src/kubectl-helm-minikube/install.sh | 10 +++++++ .../checkBashCompletion.sh | 27 +++++++++++++++++++ test/kubectl-helm-minikube/test.sh | 10 +++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100755 test/kubectl-helm-minikube/checkBashCompletion.sh diff --git a/src/kubectl-helm-minikube/devcontainer-feature.json b/src/kubectl-helm-minikube/devcontainer-feature.json index dabee64a5..23596516c 100644 --- a/src/kubectl-helm-minikube/devcontainer-feature.json +++ b/src/kubectl-helm-minikube/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "kubectl-helm-minikube", - "version": "1.1.10", + "version": "1.2.0", "name": "Kubectl, Helm, and Minikube", "documentationURL": "https://github.com/devcontainers/features/tree/main/src/kubectl-helm-minikube", "description": "Installs latest version of kubectl, Helm, and optionally minikube. Auto-detects latest versions and installs needed dependencies.", diff --git a/src/kubectl-helm-minikube/install.sh b/src/kubectl-helm-minikube/install.sh index c2f9ba0c9..bf6c8f535 100755 --- a/src/kubectl-helm-minikube/install.sh +++ b/src/kubectl-helm-minikube/install.sh @@ -311,6 +311,16 @@ if [ ${HELM_VERSION} != "none" ]; then echo '(!) Helm installation failed!' exit 1 fi + + # helm bash completion + helm completion bash > /etc/bash_completion.d/helm + + # helm zsh completion + if [ -e "${USERHOME}/.oh-my-zsh" ]; then + mkdir -p "${USERHOME}/.oh-my-zsh/completions" + helm completion zsh > "${USERHOME}/.oh-my-zsh/completions/_helm" + chown -R "${USERNAME}" "${USERHOME}/.oh-my-zsh" + fi fi # Install Minikube, verify checksum diff --git a/test/kubectl-helm-minikube/checkBashCompletion.sh b/test/kubectl-helm-minikube/checkBashCompletion.sh new file mode 100755 index 000000000..376461b52 --- /dev/null +++ b/test/kubectl-helm-minikube/checkBashCompletion.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +command=$1 +expected=$2 + +echo -e "Checking completion for command '$command'..." + +# Send command as a character stream, followed by two tab characters, into an interactive bash shell. +# Also note the 'y' which responds to the possible Bash question "Display all xxx possibilities? (y or n)". +# Bash produces the autocompletion output on stderr, so redirect that to stdout. +# The sed bit captures the lines between Header and Footer (used as output delimiters). +# The first grep removes the "Display all" message (that is atomatically answered to "y" by the script). +# The last grep filters the output to lines containing the expected result. +COMPLETE_OUTPUT=$(echo if false\; then "Header"\; $command$'\t'$'\t'y\; "Footer" fi | bash -i 2>&1 | sed -n '/Header/{:a;n;/Footer/q;p;ba}' | grep -v ^'Display all ') +echo -e "\nCompletion output:\n" +echo -e "$COMPLETE_OUTPUT" +echo -e "\n" + +FILTERED_COMPLETE_OUTPUT=$(echo "$COMPLETE_OUTPUT" | grep "$expected") + +if [ -z "$FILTERED_COMPLETE_OUTPUT" ]; then + echo -e "Completion output does not contains '$expected'." + exit 1 +else + echo -e "Completion output contains '$expected'." + exit 0 +fi diff --git a/test/kubectl-helm-minikube/test.sh b/test/kubectl-helm-minikube/test.sh index 9dc41417a..703ba58d7 100755 --- a/test/kubectl-helm-minikube/test.sh +++ b/test/kubectl-helm-minikube/test.sh @@ -10,5 +10,15 @@ check "kube" kubectl check "helm" helm version check "minikune" minikube version +# By default bash complete is disabled for the root user +# Enable it by replacing current ~/.bashrc with the /etc/skel/.bashrc file +mv ~/.bashrc ~/.bashrc.bak +cp /etc/skel/.bashrc ~/ + +check "helm-bash-completion-contains-version-command" ./checkBashCompletion.sh "helm " "version" + +# Restore original ~/.bashrc +mv ~/.bashrc.bak ~/.bashrc + # Report result reportResults \ No newline at end of file