Skip to content

Commit

Permalink
improve k alias
Browse files Browse the repository at this point in the history
  • Loading branch information
pmendelski committed Oct 1, 2023
1 parent 6947000 commit 96dce2b
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions bash/.bash/plugins/kubectl.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bash

alias k='kubectl
"--context=${KUBECTL_CONTEXT:-$(kubectl config current-context)}"
${KUBECTL_NAMESPACE/[[:alnum:]-]*/--namespace=${KUBECTL_NAMESPACE}}'
alias k='kubectl "--context=${KUBECTL_CONTEXT:-$(kubectl config current-context)}" ${KUBECTL_NAMESPACE/[[:alnum:]-]*/--namespace=${KUBECTL_NAMESPACE}}'

kc() {
local contexts="$(kubectl config get-contexts --output='name')"
Expand All @@ -12,6 +10,9 @@ kc() {
fi
if [ $# -eq 0 ]; then
echo -n "$contexts"
echo -e "\n\nActive contexts:" >&2
echo "kubectl: $(kubectl config current-context)" >&2
echo " k: $KUBECTL_CONTEXT" >&2
return 0
fi
local context="$1"
Expand All @@ -25,7 +26,7 @@ kc() {
echo "No contexts available" >&2
return 1
fi
if echo "$contexts" | grep -qv "$context"; then
if ! echo "$contexts" | grep -q "$context"; then
echo -n "Context '$context' not found. Available contexts:\n$contexts" >&2
return 1
fi
Expand All @@ -47,6 +48,9 @@ kn() {
fi
if [ $# -eq 0 ]; then
echo -n "$namespaces"
echo -e "\n\nActive namepsaces:" >&2
echo "kubectl: $(kubectl config view --minify -o jsonpath='{..namespace}')" >&2
echo " k: $KUBECTL_NAMESPACE" >&2
return 0
fi
local namespace="$1"
Expand All @@ -59,7 +63,7 @@ kn() {
echo "Unset kubectl namespace" >&2
return 0
fi
if echo "$namespaces" | grep -qv "$namespace"; then
if ! echo "$namespaces" | grep -q "$namespace"; then
echo -n "Namespace '$namespace' not found. Available namespaces:\n$namespaces" >&2
return 1
fi
Expand All @@ -69,22 +73,22 @@ kn() {

if [ -n "$ZSH_VERSION" ]; then
_kc() {
compadd -J args -o nosort -- $(kc)
compadd -J args -o nosort -- $(kc 2>/dev/null)
}
compdef _kc kc
_kn() {
compadd -J args -o nosort -- $(kn)
compadd -J args -o nosort -- $(kn 2>/dev/null)
}
compdef _kn kn
elif [ -n "$BASH_VERSION" ]; then
_kc() {
local curr_arg=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen -W "$(kc)" -- $curr_arg))
COMPREPLY=($(compgen -W "$(kc 2>/dev/null)" -- $curr_arg))
}
complete -F _kc kc
_kn() {
local curr_arg=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen -W "$(kn)" -- $curr_arg))
COMPREPLY=($(compgen -W "$(kn 2/dev/null)" -- $curr_arg))
}
complete -F _kn kn
fi

0 comments on commit 96dce2b

Please sign in to comment.