|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var completionCmd = &cobra.Command{ |
| 14 | + Use: "completion [bash|zsh]", |
| 15 | + Short: "Generate shell completion script", |
| 16 | + Long: `Generate shell completion script for kubectl-coco. |
| 17 | +
|
| 18 | +PREREQUISITES: |
| 19 | +
|
| 20 | + Bash requires bash-completion to be installed: |
| 21 | + macOS: brew install bash-completion@2 |
| 22 | + Linux: Usually pre-installed (apt-get/yum install bash-completion) |
| 23 | +
|
| 24 | + Add to your ~/.bash_profile (macOS): |
| 25 | + [[ -r "/opt/homebrew/etc/profile.d/bash_completion.sh" ]] && . "/opt/homebrew/etc/profile.d/bash_completion.sh" |
| 26 | +
|
| 27 | +BASH: |
| 28 | +
|
| 29 | + Current session: |
| 30 | + $ source <(kubectl-coco completion bash) |
| 31 | +
|
| 32 | + Permanent: |
| 33 | + $ kubectl-coco completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl-coco |
| 34 | + $ source ~/.bash_profile |
| 35 | +
|
| 36 | +ZSH: |
| 37 | +
|
| 38 | + Install completion: |
| 39 | + $ kubectl-coco completion zsh > "${fpath[1]}/_kubectl-coco" |
| 40 | + $ exec zsh |
| 41 | +
|
| 42 | +KUBECTL PLUGIN: |
| 43 | +
|
| 44 | + For 'kubectl coco' completion, install kubectl completion: |
| 45 | + $ kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl |
| 46 | +`, |
| 47 | + DisableFlagsInUseLine: true, |
| 48 | + ValidArgs: []string{"bash", "zsh"}, |
| 49 | + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), |
| 50 | + RunE: func(_ *cobra.Command, args []string) error { |
| 51 | + switch args[0] { |
| 52 | + case "bash": |
| 53 | + return genBashCompletionWithPlugin(os.Stdout) |
| 54 | + case "zsh": |
| 55 | + return genZshCompletionWithPlugin(os.Stdout) |
| 56 | + } |
| 57 | + return nil |
| 58 | + }, |
| 59 | +} |
| 60 | + |
| 61 | +func genBashCompletionWithPlugin(out io.Writer) error { |
| 62 | + // Generate standard completion |
| 63 | + buf := new(bytes.Buffer) |
| 64 | + if err := rootCmd.GenBashCompletion(buf); err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + completion := buf.String() |
| 69 | + |
| 70 | + // Find the last "if [[ $(type -t compopt)" block and remove it |
| 71 | + // We'll replace it with our own that includes kubectl_coco |
| 72 | + lastIfIdx := strings.LastIndex(completion, "if [[ $(type -t compopt)") |
| 73 | + if lastIfIdx > 0 { |
| 74 | + // Find the start of this line |
| 75 | + lineStart := strings.LastIndex(completion[:lastIfIdx], "\n") |
| 76 | + if lineStart < 0 { |
| 77 | + lineStart = 0 |
| 78 | + } else { |
| 79 | + lineStart++ // Move past the newline |
| 80 | + } |
| 81 | + completion = completion[:lineStart] |
| 82 | + } |
| 83 | + |
| 84 | + // Write the modified completion |
| 85 | + if _, err := fmt.Fprint(out, completion); err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + // Add kubectl plugin completion (kubectl coco and kubectl-coco) |
| 90 | + pluginCompletion := ` |
| 91 | +# kubectl plugin completion |
| 92 | +if [[ $(type -t compopt) = "builtin" ]]; then |
| 93 | + complete -o default -F __start_kubectl-coco kubectl-coco kubectl_coco |
| 94 | +else |
| 95 | + complete -o default -o nospace -F __start_kubectl-coco kubectl-coco kubectl_coco |
| 96 | +fi |
| 97 | +
|
| 98 | +# ex: ts=4 sw=4 et filetype=sh |
| 99 | +` |
| 100 | + _, err := fmt.Fprint(out, pluginCompletion) |
| 101 | + return err |
| 102 | +} |
| 103 | + |
| 104 | +func genZshCompletionWithPlugin(out io.Writer) error { |
| 105 | + // Generate standard completion |
| 106 | + buf := new(bytes.Buffer) |
| 107 | + if err := rootCmd.GenZshCompletion(buf); err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + completion := buf.String() |
| 112 | + |
| 113 | + // Find and remove the last compdef line |
| 114 | + lastCompdefIdx := strings.LastIndex(completion, "compdef _kubectl-coco kubectl-coco") |
| 115 | + if lastCompdefIdx > 0 { |
| 116 | + // Find the start of this line |
| 117 | + lineStart := strings.LastIndex(completion[:lastCompdefIdx], "\n") |
| 118 | + if lineStart < 0 { |
| 119 | + lineStart = 0 |
| 120 | + } else { |
| 121 | + lineStart++ // Move past the newline |
| 122 | + } |
| 123 | + completion = completion[:lineStart] |
| 124 | + } |
| 125 | + |
| 126 | + // Write the modified completion |
| 127 | + if _, err := fmt.Fprint(out, completion); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + // Add kubectl plugin completion (kubectl coco and kubectl-coco) |
| 132 | + pluginCompletion := ` |
| 133 | +# kubectl plugin completion |
| 134 | +compdef _kubectl-coco kubectl-coco kubectl_coco |
| 135 | +` |
| 136 | + _, err := fmt.Fprint(out, pluginCompletion) |
| 137 | + return err |
| 138 | +} |
| 139 | + |
| 140 | +func init() { |
| 141 | + rootCmd.AddCommand(completionCmd) |
| 142 | +} |
0 commit comments