Skip to content

Commit

Permalink
upd: kubectl native completion
Browse files Browse the repository at this point in the history
  • Loading branch information
kvaps committed Aug 29, 2023
1 parent 4f8dc47 commit cd80d90
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ sudo mv ./kubectl-use /usr/local/bin/kubectl-use
```
## Autocompletion

Add to your `.zshrc` or `.bashrc` file:
Currently krew does not support for installing completion, thus only curl method is available:

```bash
if command -V kubectl-use >/dev/null; then
source <(kubectl-use -completion)
fi
curl -LO https://github.com/kvaps/kubectl-use/raw/master/kubectl_complete-use
chmod +x ./kubectl_complete-use
sudo mv ./kubectl_complete-use /usr/local/bin/kubectl_complete-use
```
22 changes: 0 additions & 22 deletions kubectl-use
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,7 @@
set -e

case $1 in
-completion)
cat <<\EOT
_kubectl_use() {
local cur prev opts
case "${COMP_LINE}" in
kubectl\ use\ * | kubectl-use\ *)
kubectl config get-contexts -o name
kubectl get ns -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'
return
;;
esac
if command -v __start_kubectl >/dev/null; then
__start_kubectl
fi
}
complete -F _kubectl_use kubectl kubectl-use
EOT
exit 0
;;
-* | "")
echo
echo " Completion:"
echo " source <(kubectl use -completion)"
echo
echo " Usage:"
echo " kubectl use <namespace>"
Expand Down
11 changes: 11 additions & 0 deletions kubectl_complete-use
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
if [ "$#" -le 1 ]; then
if kubectl config get-contexts "$1" >/dev/null 2>&1; then
kubectl get namespaces --context "$1" -o custom-columns=NAME:.metadata.name --no-headers
else
(kubectl config get-contexts -o=name; kubectl get namespaces -o custom-columns=NAME:.metadata.name --no-headers) | grep "$1"
fi
fi
if [ "$#" -eq 2 ]; then
kubectl get namespaces --context "$1" -o custom-columns=NAME:.metadata.name --no-headers | grep "$2"
fi

0 comments on commit cd80d90

Please sign in to comment.