Add --with-short-names option

This allows linking as "kctx" and "kns" to prevent overlap with kubectl Tab
completion when user hits `ku<Tab>`. Fixes #3.

Also fixed variables KUBECTX and PREV leaking to zsh shell once the _kubectx
function is invoked.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan 2017-05-16 21:51:01 -07:00
parent da3f814280
commit a376b02e88
No known key found for this signature in database
GPG Key ID: 5C02521D7B216AD6
5 changed files with 14 additions and 13 deletions

View File

@ -3,15 +3,16 @@ class Kubectx < Formula
homepage "https://github.com/ahmetb/kubectx" homepage "https://github.com/ahmetb/kubectx"
url "https://github.com/ahmetb/kubectx/archive/v0.2.0.tar.gz" url "https://github.com/ahmetb/kubectx/archive/v0.2.0.tar.gz"
sha256 "28069aff84aaba1aa38f42d3b27e64e460a5c0651fb56b1748f44fd832d912e3" sha256 "28069aff84aaba1aa38f42d3b27e64e460a5c0651fb56b1748f44fd832d912e3"
head "https://github.com/ahmetb/kubectx.git", :branch => "master" head "https://github.com/ahmetb/kubectx.git", :branch => "short-names"
bottle :unneeded bottle :unneeded
option "with-short-names", "link as \"kctx\" and \"kns\" instead"
def install def install
bin.install "kubectx" bin.install "kubectx" => build.with?("short-names") ? "kctx" : "kubectx"
bin.install "kubens" bin.install "kubens" => build.with?("short-names") ? "kns" : "kubens"
include.install "utils.bash" include.install "utils.bash"
bash_completion.install "completion/kubectx.bash" => "kubectx" bash_completion.install "completion/kubectx.bash" => "kubectx"
bash_completion.install "completion/kubens.bash" => "kubens" bash_completion.install "completion/kubens.bash" => "kubens"
zsh_completion.install "completion/kubectx.zsh" => "_kubectx" zsh_completion.install "completion/kubectx.zsh" => "_kubectx"
@ -19,7 +20,7 @@ class Kubectx < Formula
end end
test do test do
system "which", "kubectx" system "which", build.with?("short-names") ? "kctx" : "kubectx"
system "which", "kubens" system "which", build.with?("short-names") ? "kns" : "kubens"
end end
end end

View File

@ -5,4 +5,4 @@ _kube_contexts()
COMPREPLY=( $(compgen -W "- $(kubectl config get-contexts --output='name')" -- $curr_arg ) ); COMPREPLY=( $(compgen -W "- $(kubectl config get-contexts --output='name')" -- $curr_arg ) );
} }
complete -F _kube_contexts kubectx complete -F _kube_contexts kubectx kctx

View File

@ -1,10 +1,10 @@
#compdef kubectx #compdef kubectx kctx=kubectx
KUBECTX="${HOME}/.kube/kubectx" local KUBECTX="${HOME}/.kube/kubectx"
PREV="" PREV=""
if [ -f "$KUBECTX" ]; then if [ -f "$KUBECTX" ]; then
# show '-' only if there's a saved previous context # show '-' only if there's a saved previous context
PREV=$(cat "${KUBECTX}") local PREV=$(cat "${KUBECTX}")
_arguments "1: :((-\:Back\ to\ ${PREV} \ _arguments "1: :((-\:Back\ to\ ${PREV} \
$(kubectl config get-contexts --output='name')))" $(kubectl config get-contexts --output='name')))"
else else

View File

@ -5,4 +5,4 @@ _kube_namespaces()
COMPREPLY=( $(compgen -W "- $(kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}')" -- $curr_arg ) ); COMPREPLY=( $(compgen -W "- $(kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}')" -- $curr_arg ) );
} }
complete -F _kube_namespaces kubens complete -F _kube_namespaces kubens kns

View File

@ -1,2 +1,2 @@
#compdef kubens #compdef kubens kns=kubens
_arguments "1: :(- $(kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}'))" _arguments "1: :(- $(kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}'))"