Skip to content

Commit 23e8b74

Browse files
authored
Merge pull request #11 from bpradipt/completion
Add command completion support
2 parents a9312d9 + ce638e4 commit 23e8b74

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,82 @@ make build
151151
sudo make install
152152
```
153153

154+
## Shell Completion
155+
156+
kubectl-coco supports command autocompletion for bash and zsh shells.
157+
158+
### Bash
159+
160+
**Prerequisites:**
161+
162+
Install bash-completion.
163+
164+
For MacOS:
165+
166+
```bash
167+
brew install bash-completion@2
168+
169+
# Add to your ~/.bash_profile:
170+
[[ -r "/opt/homebrew/etc/profile.d/bash_completion.sh" ]] && . "/opt/homebrew/etc/profile.d/bash_completion.sh"
171+
172+
# Reload profile:
173+
source ~/.bash_profile
174+
```
175+
176+
For Linux:
177+
178+
```bash
179+
# Ubuntu/Debian:
180+
apt-get install bash-completion
181+
182+
# CentOS/RHEL:
183+
yum install bash-completion
184+
```
185+
186+
**Installation:**
187+
188+
For current session:
189+
```bash
190+
source <(kubectl-coco completion bash)
191+
```
192+
193+
For all sessions (permanent):
194+
```bash
195+
# MacOS:
196+
kubectl-coco completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl-coco
197+
198+
# Linux:
199+
kubectl-coco completion bash > /etc/bash_completion.d/kubectl-coco
200+
201+
# Then restart your shell
202+
```
203+
204+
**For kubectl plugin (`kubectl coco`):**
205+
206+
Install kubectl completion first:
207+
208+
```bash
209+
# MacOS:
210+
kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl
211+
212+
# Linux:
213+
kubectl completion bash > /etc/bash_completion.d/kubectl
214+
```
215+
216+
### Zsh
217+
218+
Enable completion if not already enabled:
219+
```bash
220+
echo "autoload -U compinit; compinit" >> ~/.zshrc
221+
```
222+
223+
Install kubectl-coco completion:
224+
```bash
225+
kubectl-coco completion zsh > "${fpath[1]}/_kubectl-coco"
226+
```
227+
228+
Start a new shell for completion to take effect.
229+
154230
## Usage
155231

156232
### Initialize Configuration

claude.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ make clean
4545
# Build and test locally
4646
./kubectl-coco init --help
4747
./kubectl-coco apply --help
48+
./kubectl-coco completion bash # Generate shell completion
4849

4950
# Test transformation without applying
5051
./kubectl-coco apply -f examples/pod.yaml --skip-apply

cmd/completion.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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

Comments
 (0)