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 <marc.khouzam@montreal.ca>
pull/6502/head
Marc Khouzam 5 years ago
parent 681eab83f9
commit b3857b5d80

@ -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 {

Loading…
Cancel
Save