From 681eab83f92a083cf77266787de29f1e79444867 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Tue, 24 Sep 2019 09:17:24 -0400 Subject: [PATCH 1/3] feat(comp): have zsh completion generation re-use bash code This allows that any addition to the bash completion logic be automatically added to the zsh logic. Signed-off-by: Marc Khouzam --- cmd/helm/completion.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cmd/helm/completion.go b/cmd/helm/completion.go index 29919ab1e..ebe1cb02a 100644 --- a/cmd/helm/completion.go +++ b/cmd/helm/completion.go @@ -16,7 +16,6 @@ limitations under the License. package main import ( - "bytes" "io" "github.com/pkg/errors" @@ -211,9 +210,7 @@ __helm_convert_bash_to_zsh() { ` out.Write([]byte(zshInitialization)) - buf := new(bytes.Buffer) - cmd.Root().GenBashCompletion(buf) - out.Write(buf.Bytes()) + runCompletionBash(out, cmd) zshTail := ` BASH_COMPLETION_EOF From b3857b5d80b6e0c916a17ada6270878039c4e5e2 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Tue, 24 Sep 2019 09:22:47 -0400 Subject: [PATCH 2/3] feat(comp): auto-complete of a renamed helm binary If a user renames the helm binary, the shell needs to be told that this new name corresponds to the helm completion function. For example if the user decides to rename the helm v3 binary to 'helm3', then the following command extra command would need be run by the user: complete -o default -F __start_helm helm3. This commit automates this by adding this extra command to the generated shell completion script by looking at what binary was used to call the helm completion command. For example, if the user renames the binary to helm3 and then calls helm3 completion bash the completion script will include a hook for the binary 'helm3' to the completion script. A binary rename is one of the two options recommended when users need to run both helm2 and helm3. Signed-off-by: Marc Khouzam --- cmd/helm/completion.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/cmd/helm/completion.go b/cmd/helm/completion.go index ebe1cb02a..7417bc93b 100644 --- a/cmd/helm/completion.go +++ b/cmd/helm/completion.go @@ -17,6 +17,8 @@ package main import ( "io" + "os" + "path/filepath" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -76,7 +78,25 @@ func runCompletion(out io.Writer, cmd *cobra.Command, args []string) error { } func runCompletionBash(out io.Writer, cmd *cobra.Command) error { - return cmd.Root().GenBashCompletion(out) + err := cmd.Root().GenBashCompletion(out) + + // In case the user renamed the helm binary (e.g., to be able to run + // both helm2 and helm3), we hook the new binary name to the completion function + if binary := filepath.Base(os.Args[0]); binary != "helm" { + renamedBinaryHook := ` +# Hook the command used to generate the completion script +# to the helm completion function to handle the case where +# the user renamed the helm binary +if [[ $(type -t compopt) = "builtin" ]]; then + complete -o default -F __start_helm ` + binary + ` +else + complete -o default -o nospace -F __start_helm ` + binary + ` +fi +` + out.Write([]byte(renamedBinaryHook)) + } + + return err } func runCompletionZsh(out io.Writer, cmd *cobra.Command) error { From c225b603072a55f5fb2cbb7f34619f1a30a3516b Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Thu, 26 Sep 2019 21:19:54 -0400 Subject: [PATCH 3/3] Avoid string concatenation Signed-off-by: Marc Khouzam --- cmd/helm/completion.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/helm/completion.go b/cmd/helm/completion.go index 7417bc93b..7122b92be 100644 --- a/cmd/helm/completion.go +++ b/cmd/helm/completion.go @@ -16,6 +16,7 @@ limitations under the License. package main import ( + "fmt" "io" "os" "path/filepath" @@ -88,12 +89,12 @@ func runCompletionBash(out io.Writer, cmd *cobra.Command) error { # to the helm completion function to handle the case where # the user renamed the helm binary if [[ $(type -t compopt) = "builtin" ]]; then - complete -o default -F __start_helm ` + binary + ` + complete -o default -F __start_helm %[1]s else - complete -o default -o nospace -F __start_helm ` + binary + ` + complete -o default -o nospace -F __start_helm %[1]s fi ` - out.Write([]byte(renamedBinaryHook)) + fmt.Fprintf(out, renamedBinaryHook, binary) } return err