Skip to content

Commit bb06d37

Browse files
bpradiptclaude
andcommitted
Fix kubectl plugin completion for zsh
This commit fixes two issues with kubectl plugin completion: 1. Zsh completion was truncating the completion function because it removed everything after the compdef line instead of just that line 2. kubectl_coco (plugin form) was not registered in the #compdef directive, so 'kubectl coco <TAB>' didn't work Co-Authored-By: Claude Sonnet 4.5 <[email protected]> Signed-off-by: Pradipta Banerjee <[email protected]>
1 parent 8e74280 commit bb06d37

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ install: build
4444
@mkdir -p $(INSTALL_PATH)
4545
@cp $(BINARY_NAME) $(INSTALL_PATH)/
4646
@chmod +x $(INSTALL_PATH)/$(BINARY_NAME)
47+
@ln -sf $(INSTALL_PATH)/$(BINARY_NAME) $(INSTALL_PATH)/kubectl_complete-coco
4748
@echo "Installation complete. You can now use: kubectl coco"
4849

4950
## uninstall: Remove kubectl-coco from $(INSTALL_PATH)
5051
uninstall:
5152
@echo "Uninstalling $(BINARY_NAME)..."
5253
@rm -f $(INSTALL_PATH)/$(BINARY_NAME)
54+
@rm -f $(INSTALL_PATH)/kubectl_complete-coco
5355
@echo "Uninstall complete"
5456

5557
## clean: Remove build artifacts

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ curl -LO "https://github.com/confidential-devhub/cococtl/releases/latest/downloa
3838

3939
# Install
4040
sudo install -m 0755 kubectl-coco-${OS}-${ARCH} /usr/local/bin/kubectl-coco
41+
sudo ln -sf /usr/local/bin/kubectl-coco /usr/local/bin/kubectl_complete-coco
4142

4243
# Verify
4344
kubectl coco --version
@@ -127,12 +128,14 @@ For detailed information, see [TRANSFORMATIONS.md](TRANSFORMATIONS.md).
127128
System-wide (requires sudo):
128129
```bash
129130
sudo install -m 0755 kubectl-coco-${OS}-${ARCH} /usr/local/bin/kubectl-coco
131+
sudo ln -sf /usr/local/bin/kubectl-coco /usr/local/bin/kubectl_complete-coco
130132
```
131133

132134
Or user directory:
133135
```bash
134136
mkdir -p ~/.local/bin
135137
install -m 0755 kubectl-coco-${OS}-${ARCH} ~/.local/bin/kubectl-coco
138+
ln -sf ~/.local/bin/kubectl-coco ~/.local/bin/kubectl_complete-coco
136139
export PATH=$PATH:~/.local/bin # Add to ~/.bashrc or ~/.zshrc
137140
```
138141

@@ -227,6 +230,12 @@ kubectl-coco completion zsh > "${fpath[1]}/_kubectl-coco"
227230

228231
Start a new shell for completion to take effect.
229232

233+
**Note:** The `kubectl_complete-coco` symlink (created during installation) enables `kubectl coco` plugin completion. If `kubectl coco <TAB>` doesn't work, verify:
234+
```bash
235+
ls -la /usr/local/bin/kubectl_complete-coco
236+
# Should point to kubectl-coco
237+
```
238+
230239
## Usage
231240

232241
### Initialize Configuration

cmd/completion.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,30 +136,14 @@ func genZshCompletionWithPlugin(out io.Writer) error {
136136

137137
completion := buf.String()
138138

139-
// Find and remove the last compdef line
140-
lastCompdefIdx := strings.LastIndex(completion, "compdef _kubectl-coco kubectl-coco")
141-
if lastCompdefIdx > 0 {
142-
// Find the start of this line
143-
lineStart := strings.LastIndex(completion[:lastCompdefIdx], "\n")
144-
if lineStart < 0 {
145-
lineStart = 0
146-
} else {
147-
lineStart++ // Move past the newline
148-
}
149-
completion = completion[:lineStart]
150-
}
139+
// Replace both compdef lines to include kubectl_coco for plugin support
140+
// 1. #compdef directive (line 1) - used when file is in fpath
141+
completion = strings.Replace(completion, "#compdef kubectl-coco", "#compdef kubectl-coco kubectl_coco", 1)
142+
// 2. compdef command - used when sourcing file directly
143+
completion = strings.Replace(completion, "compdef _kubectl-coco kubectl-coco", "compdef _kubectl-coco kubectl-coco kubectl_coco", 1)
151144

152145
// Write the modified completion
153-
if _, err := fmt.Fprint(out, completion); err != nil {
154-
return err
155-
}
156-
157-
// Add kubectl plugin completion (kubectl coco and kubectl-coco)
158-
pluginCompletion := `
159-
# kubectl plugin completion
160-
compdef _kubectl-coco kubectl-coco kubectl_coco
161-
`
162-
_, err := fmt.Fprint(out, pluginCompletion)
146+
_, err := fmt.Fprint(out, completion)
163147
return err
164148
}
165149

main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@ package main
33

44
import (
55
"os"
6+
"path/filepath"
7+
"strings"
68

79
"github.com/confidential-devhub/cococtl/cmd"
810
)
911

1012
func main() {
13+
// Handle kubectl plugin completion
14+
// When kubectl calls kubectl_complete-coco, it doesn't pass __complete
15+
// but cobra needs it to trigger completion mode
16+
binaryName := filepath.Base(os.Args[0])
17+
if strings.Contains(binaryName, "kubectl_complete-") {
18+
// Inject __complete as the first argument
19+
os.Args = append([]string{os.Args[0], "__complete"}, os.Args[1:]...)
20+
}
21+
1122
if err := cmd.Execute(); err != nil {
1223
os.Exit(1)
1324
}

0 commit comments

Comments
 (0)