Merge pull request #6502 from VilledeMontreal/feat-v3/compRenamedBin

v3: Support auto-complete of a renamed helm binary
pull/6508/head
Taylor Thomas 5 years ago committed by GitHub
commit 3fa4465eec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,8 +16,10 @@ limitations under the License.
package main
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@ -77,7 +79,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 %[1]s
else
complete -o default -o nospace -F __start_helm %[1]s
fi
`
fmt.Fprintf(out, renamedBinaryHook, binary)
}
return err
}
func runCompletionZsh(out io.Writer, cmd *cobra.Command) error {
@ -211,9 +231,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

Loading…
Cancel
Save