Allow setting copyright header file for generated completions

This commit is contained in:
Maciej Szulik 2017-01-17 16:52:49 +01:00
parent e1900f704b
commit 56147c5174
2 changed files with 11 additions and 13 deletions

View File

@ -301,7 +301,7 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cob
Commands: []*cobra.Command{ Commands: []*cobra.Command{
NewCmdLabel(f, out), NewCmdLabel(f, out),
NewCmdAnnotate(f, out), NewCmdAnnotate(f, out),
NewCmdCompletion(f, out), NewCmdCompletion(f, out, ""),
}, },
}, },
} }

View File

@ -26,7 +26,7 @@ import (
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
) )
const boilerPlate = ` const defaultBoilerPlate = `
# Copyright 2016 The Kubernetes Authors. # Copyright 2016 The Kubernetes Authors.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
@ -86,7 +86,7 @@ var (
} }
) )
func NewCmdCompletion(f cmdutil.Factory, out io.Writer) *cobra.Command { func NewCmdCompletion(f cmdutil.Factory, out io.Writer, boilerPlate string) *cobra.Command {
shells := []string{} shells := []string{}
for s := range completion_shells { for s := range completion_shells {
shells = append(shells, s) shells = append(shells, s)
@ -98,7 +98,7 @@ func NewCmdCompletion(f cmdutil.Factory, out io.Writer) *cobra.Command {
Long: completion_long, Long: completion_long,
Example: completion_example, Example: completion_example,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
err := RunCompletion(out, cmd, args) err := RunCompletion(out, boilerPlate, cmd, args)
cmdutil.CheckErr(err) cmdutil.CheckErr(err)
}, },
ValidArgs: shells, ValidArgs: shells,
@ -107,7 +107,7 @@ func NewCmdCompletion(f cmdutil.Factory, out io.Writer) *cobra.Command {
return cmd return cmd
} }
func RunCompletion(out io.Writer, cmd *cobra.Command, args []string) error { func RunCompletion(out io.Writer, boilerPlate string, cmd *cobra.Command, args []string) error {
if len(args) == 0 { if len(args) == 0 {
return cmdutil.UsageError(cmd, "Shell not specified.") return cmdutil.UsageError(cmd, "Shell not specified.")
} }
@ -119,22 +119,20 @@ func RunCompletion(out io.Writer, cmd *cobra.Command, args []string) error {
return cmdutil.UsageError(cmd, "Unsupported shell type %q.", args[0]) return cmdutil.UsageError(cmd, "Unsupported shell type %q.", args[0])
} }
if len(boilerPlate) == 0 {
boilerPlate = defaultBoilerPlate
}
if _, err := out.Write([]byte(boilerPlate)); err != nil {
return err
}
return run(out, cmd.Parent()) return run(out, cmd.Parent())
} }
func runCompletionBash(out io.Writer, kubectl *cobra.Command) error { func runCompletionBash(out io.Writer, kubectl *cobra.Command) error {
_, err := out.Write([]byte(boilerPlate))
if err != nil {
return err
}
return kubectl.GenBashCompletion(out) return kubectl.GenBashCompletion(out)
} }
func runCompletionZsh(out io.Writer, kubectl *cobra.Command) error { func runCompletionZsh(out io.Writer, kubectl *cobra.Command) error {
_, err := out.Write([]byte(boilerPlate))
if err != nil {
return err
}
zsh_initialization := ` zsh_initialization := `
__kubectl_bash_source() { __kubectl_bash_source() {
alias shopt=':' alias shopt=':'