From a7d6820d53e057023846dc44a21ed3f20949e9f8 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Sat, 9 Mar 2019 15:54:18 -0500 Subject: [PATCH 1/9] Add completion for helm status Signed-off-by: Marc Khouzam --- cmd/helm/helm.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index b815568cb..656308d6d 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -39,6 +39,30 @@ import ( "k8s.io/helm/pkg/tlsutil" ) +const ( + bashCompletionFunc = ` +__helm_status_list() +{ + local out + if out=$(helm list -q 2>/dev/null); then + COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) ) + fi +} + +__helm_custom_func() +{ + case ${last_command} in + helm_status) + __helm_status_list + return + ;; + *) + ;; + esac +} +` +) + var ( tillerTunnel *kube.Tunnel settings helm_env.EnvSettings @@ -103,6 +127,7 @@ func newRootCmd(args []string) *cobra.Command { PersistentPostRun: func(*cobra.Command, []string) { teardown() }, + BashCompletionFunction: bashCompletionFunc, } flags := cmd.PersistentFlags() From 288fb869a2f41df2a74f69ef6e778e3ae379c29c Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Sat, 9 Mar 2019 17:31:43 -0500 Subject: [PATCH 2/9] Add completion for helm delete Signed-off-by: Marc Khouzam --- cmd/helm/helm.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 656308d6d..160fa8141 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -41,7 +41,7 @@ import ( const ( bashCompletionFunc = ` -__helm_status_list() +__helm_list_releases() { local out if out=$(helm list -q 2>/dev/null); then @@ -52,8 +52,8 @@ __helm_status_list() __helm_custom_func() { case ${last_command} in - helm_status) - __helm_status_list + helm_status | helm_delete) + __helm_list_releases return ;; *) From f25729f234ea3e7ed64e2cb90daaaf7c36e7eaf0 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Sat, 9 Mar 2019 22:19:22 -0500 Subject: [PATCH 3/9] Add support for override flags in completion Signed-off-by: Marc Khouzam --- cmd/helm/helm.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 160fa8141..627f9c5dd 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -41,10 +41,38 @@ import ( const ( bashCompletionFunc = ` +__helm_override_flag_list=(--kubeconfig --kube-context --host --tiller-namespace) +__helm_override_flags() +{ + local ${__helm_override_flag_list[*]##*-} two_word_of of var + for w in "${words[@]}"; do + if [ -n "${two_word_of}" ]; then + eval "${two_word_of##*-}=\"${two_word_of}=\${w}\"" + two_word_of= + continue + fi + for of in "${__helm_override_flag_list[@]}"; do + case "${w}" in + ${of}=*) + eval "${of##*-}=\"${w}\"" + ;; + ${of}) + two_word_of="${of}" + ;; + esac + done + done + for var in "${__helm_override_flag_list[@]##*-}"; do + if eval "test -n \"\$${var}\""; then + eval "echo \${${var}}" + fi + done +} + __helm_list_releases() { local out - if out=$(helm list -q 2>/dev/null); then + if out=$(helm list $(__helm_override_flags) -q 2>/dev/null); then COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) ) fi } From d2ab3f5062467b66de8dc9728dd8c7509e8dcb07 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Sat, 6 Apr 2019 12:31:37 -0400 Subject: [PATCH 4/9] Cannot have an = sign as an index Flags sometimes can be used with an = sign, such as --kube-context=prod. In this case, the variable ${flagname} retains the = sign as part of the flag name. However, in zsh completion, an = sign cannot be part of an index of the associative array 'flaghash' or else it causes an error. This commits strips the = sign out when using ${flagname} as an index. Note that this is not a big deal since flaghash is not actually used anywhere in Helm completion. I believe it is made available by the Cobra framework in case some completions choose to use it. Signed-off-by: Marc Khouzam --- cmd/helm/completion.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/helm/completion.go b/cmd/helm/completion.go index 039dcbe5f..53d1c21e5 100644 --- a/cmd/helm/completion.go +++ b/cmd/helm/completion.go @@ -213,6 +213,7 @@ __helm_convert_bash_to_zsh() { -e "s/${LWORD}declare${RWORD}/__helm_declare/g" \ -e "s/\\\$(type${RWORD}/\$(__helm_type/g" \ -e 's/aliashash\["\(.\{1,\}\)"\]/aliashash[\1]/g' \ + -e 's/flaghash\[${flagname/flaghash[${flagname%=/' \ -e 's/FUNCNAME/funcstack/g' \ <<'BASH_COMPLETION_EOF' ` From 6a33235fc8c59aa064c90f59a555ee09153506cd Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Sun, 7 Apr 2019 13:10:46 -0400 Subject: [PATCH 5/9] Also complete history and test with release name Signed-off-by: Marc Khouzam --- cmd/helm/helm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 627f9c5dd..47719810d 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -80,7 +80,7 @@ __helm_list_releases() __helm_custom_func() { case ${last_command} in - helm_status | helm_delete) + helm_delete | helm_history | helm_status | helm_test) __helm_list_releases return ;; From 09801005606d470357c2f2006b0cd5ab7ff58091 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Tue, 23 Apr 2019 22:30:23 -0400 Subject: [PATCH 6/9] List all releases when doing completion Signed-off-by: Marc Khouzam --- cmd/helm/helm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 47719810d..92ff947b8 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -72,7 +72,7 @@ __helm_override_flags() __helm_list_releases() { local out - if out=$(helm list $(__helm_override_flags) -q 2>/dev/null); then + if out=$(helm list $(__helm_override_flags) -a -q 2>/dev/null); then COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) ) fi } From 85fe8a759fc496b6dcc50407a83802821b968308 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Sat, 27 Apr 2019 20:50:17 -0400 Subject: [PATCH 7/9] Dynamic completion for helm upgrade, get, rollback Signed-off-by: Marc Khouzam --- cmd/helm/helm.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 92ff947b8..cd23f8894 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -80,7 +80,8 @@ __helm_list_releases() __helm_custom_func() { case ${last_command} in - helm_delete | helm_history | helm_status | helm_test) + helm_delete | helm_history | helm_status | helm_test |\ + helm_upgrade | helm_rollback | helm_get_*) __helm_list_releases return ;; From be82cb240c11a5b6ce8dce7314295476308c7e8a Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Fri, 17 May 2019 22:52:23 -0400 Subject: [PATCH 8/9] Add a filter to the helm list call for completion Signed-off-by: Marc Khouzam --- cmd/helm/helm.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index b3f16fb7f..8d4652984 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -71,14 +71,18 @@ __helm_override_flags() __helm_list_releases() { - local out - if out=$(helm list $(__helm_override_flags) -a -q 2>/dev/null); then + __helm_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}" + local out filter + # Use ^ to map from the start of the release name + filter="^${words[c]}" + if out=$(helm list $(__helm_override_flags) -a -q ${filter} 2>/dev/null); then COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) ) fi } __helm_custom_func() { + __helm_debug "${FUNCNAME[0]}: c is $c words[@] is ${words[@]}" case ${last_command} in helm_delete | helm_history | helm_status | helm_test |\ helm_upgrade | helm_rollback | helm_get_*) From 1bd6281761f84a29dfe713d5f51ff9c9aecbe06d Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Wed, 5 Jun 2019 16:02:29 -0400 Subject: [PATCH 9/9] Revert "Cannot have an = sign as an index" This reverts commit d2ab3f5062467b66de8dc9728dd8c7509e8dcb07. The reverted commit turned out to be a workaroud another problem. The real fix is submitted in PR #5680 Signed-off-by: Marc Khouzam --- cmd/helm/completion.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/helm/completion.go b/cmd/helm/completion.go index 53d1c21e5..039dcbe5f 100644 --- a/cmd/helm/completion.go +++ b/cmd/helm/completion.go @@ -213,7 +213,6 @@ __helm_convert_bash_to_zsh() { -e "s/${LWORD}declare${RWORD}/__helm_declare/g" \ -e "s/\\\$(type${RWORD}/\$(__helm_type/g" \ -e 's/aliashash\["\(.\{1,\}\)"\]/aliashash[\1]/g' \ - -e 's/flaghash\[${flagname/flaghash[${flagname%=/' \ -e 's/FUNCNAME/funcstack/g' \ <<'BASH_COMPLETION_EOF' `