From fea6bb84a6db99ffa6d70a0a4954bef398ae6ee8 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Sun, 19 May 2019 15:54:23 -0400 Subject: [PATCH 1/2] (helm): update Cobra to version 0.0.4 The version of Cobra used in dev-v3 is older than the one used for the master branch (helm v2). Although the dev-v3 branch was based on master and therefore had the same Cobra version originally, it was changed a couple of times to choose Cobra tagged versions instead. However, the currently used 0.0.3 version of Cobra is older than the version of Cobra used on the master branch. Therefore, some of the improvements made to Cobra and used by helm v2 are not available to helm v3 currently. This commit brings Cobra to its latest available version of 0.0.4. Bringing Cobra up-to-date is essential for upcoming work being prepared for dynamic bash-completion; there are bug fixes in Cobra that are necessary for dynamic bash-completion to work properly. Specifically, spf13/cobra#730 which fixes spf13/cobra#694 is essential to avoid the risk of colliding and possibly breaking kubectl dynamic bash-completion once helm v3 has its own dynamic completion. Signed-off-by: Marc Khouzam --- Gopkg.lock | 6 +++--- Gopkg.toml | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 9b02085ef..81ea186ea 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -879,15 +879,15 @@ version = "v1.4.1" [[projects]] - digest = "1:e01b05ba901239c783dfe56450bcde607fc858908529868259c9a8765dc176d0" + digest = "1:2e72f9cdc8b6f94a145fa1c97e305e1654d40507d04d2fbb0c37bf461a4b85f7" name = "github.com/spf13/cobra" packages = [ ".", "doc", ] pruneopts = "UT" - revision = "ef82de70bb3f60c65fb8eebacbb2d122ef517385" - version = "v0.0.3" + revision = "67fc4837d267bc9bfd6e47f77783fcc3dffc68de" + version = "v0.0.4" [[projects]] digest = "1:c1b1102241e7f645bc8e0c22ae352e8f0dc6484b6cb4d132fa9f24174e0119e2" diff --git a/Gopkg.toml b/Gopkg.toml index 26daeee8a..7490732f6 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -109,3 +109,7 @@ [[constraint]] name = "github.com/xeipuuv/gojsonschema" version = "1.1.0" + +[[constraint]] + name = "github.com/spf13/cobra" + version = "0.0.4" From 097096d47c44b8f0db1fb1f2d9f37c392c0ee901 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Sat, 27 Apr 2019 16:30:04 -0400 Subject: [PATCH 2/2] Add dynamic completion for release names This commit adds dynamic completion for the commands helm status helm uninstall helm history helm test run helm upgrade helm get helm rollback Aliases of commands are automatically taken care of, such as helm delete which is an alias of helm uninstall Support for override flags in completion is included for such dynamic completion. The list of release names to complete is obtained by running helm list $(__helm_override_flags) -a -q -m 1000 -f ${filter} where ${__helm_override_flags} is any user-specified flags part of --kubeconfig --kube-context --home --namespace -n ${filter} is whatever prefix the user may have already typed for the release name Signed-off-by: Marc Khouzam --- cmd/helm/root.go | 66 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/cmd/helm/root.go b/cmd/helm/root.go index 69e9a1f87..f034069fe 100644 --- a/cmd/helm/root.go +++ b/cmd/helm/root.go @@ -29,6 +29,61 @@ import ( "helm.sh/helm/pkg/registry" ) +const ( + bashCompletionFunc = ` +__helm_override_flag_list=(--kubeconfig --kube-context --home --namespace -n) +__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() +{ + __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 -m 1000 -f ${filter} 2>/dev/null); then + COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) ) + fi +} +__helm_custom_func() +{ + __helm_debug "${FUNCNAME[0]}: last_command is $last_command" + case ${last_command} in + helm_uninstall | helm_history | helm_status | helm_test_run |\ + helm_upgrade | helm_rollback | helm_get_*) + __helm_list_releases + return + ;; + *) + ;; + esac +} +` +) + var globalUsage = `The Kubernetes package manager To begin working with Helm, run the 'helm init' command: @@ -53,11 +108,12 @@ Environment: func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string) *cobra.Command { cmd := &cobra.Command{ - Use: "helm", - Short: "The Helm package manager for Kubernetes.", - Long: globalUsage, - SilenceUsage: true, - Args: require.NoArgs, + Use: "helm", + Short: "The Helm package manager for Kubernetes.", + Long: globalUsage, + SilenceUsage: true, + Args: require.NoArgs, + BashCompletionFunction: bashCompletionFunc, } flags := cmd.PersistentFlags()