Skip to content

Commit

Permalink
add k alias
Browse files Browse the repository at this point in the history
  • Loading branch information
pmendelski committed Oct 1, 2023
1 parent 9cb2d54 commit 6947000
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
90 changes: 90 additions & 0 deletions bash/.bash/plugins/kubectl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env bash

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')"
if [ ! $? -eq 0 ]; then
echo "Could not read kubectl contexts" >&2
return 1
fi
if [ $# -eq 0 ]; then
echo -n "$contexts"
return 0
fi
local context="$1"
if [ -z "$context" ]; then
unset KUBECTL_CONTEXT
unset KUBECTL_NAMESPACE
echo "Unset kubectl context" >&2
return 0
fi
if [ -z "$contexts" ]; then
echo "No contexts available" >&2
return 1
fi
if echo "$contexts" | grep -qv "$context"; then
echo -n "Context '$context' not found. Available contexts:\n$contexts" >&2
return 1
fi
export KUBECTL_CONTEXT="$context"
echo "Context switched: '$context'" >&2
local namespace="$2"
if [ -n "$namespace" ]; then
kn "$2"
else
unset KUBECTL_NAMESPACE
fi
}

kn() {
local namespaces="$(kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}')"
if [ ! $? -eq 0 ]; then
echo "Could not read kubectl namespaces" >&2
return 1
fi
if [ $# -eq 0 ]; then
echo -n "$namespaces"
return 0
fi
local namespace="$1"
if [ -z "$namespaces" ]; then
echo "No namespaces available" >&2
return 1
fi
if [ -z "$namespace" ]; then
unset KUBECTL_NAMESPACE
echo "Unset kubectl namespace" >&2
return 0
fi
if echo "$namespaces" | grep -qv "$namespace"; then
echo -n "Namespace '$namespace' not found. Available namespaces:\n$namespaces" >&2
return 1
fi
export KUBECTL_NAMESPACE="$namespace"
echo "Context namespace: '$namespace'" >&2
}

if [ -n "$ZSH_VERSION" ]; then
_kc() {
compadd -J args -o nosort -- $(kc)
}
compdef _kc kc
_kn() {
compadd -J args -o nosort -- $(kn)
}
compdef _kn kn
elif [ -n "$BASH_VERSION" ]; then
_kc() {
local curr_arg=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen -W "$(kc)" -- $curr_arg))
}
complete -F _kc kc
_kn() {
local curr_arg=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen -W "$(kn)" -- $curr_arg))
}
complete -F _kn kn
fi
2 changes: 1 addition & 1 deletion nvim/.nvim/lua/plugin/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function _M.config()
-- extend mappings
i = {
["<C-k>"] = lga.quote_prompt(),
["<C-K>"] = lga.quote_prompt({ postfix = " --iglob *." }),
["<C-j>"] = lga.quote_prompt({ postfix = " --iglob *." }),
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions zsh/.zsh/lib/completions.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ zstyle ':completion:*:*:*:*:processes' command "ps -u $USER -o pid,user,comm -w
# disable named-directories autocompletion
zstyle ':completion:*:cd:*' tag-order local-directories directory-stack path-directories

# disable sorting sdkvm
zstyle ':completion:*:sdkvm:*' sort false
# disable sorting - order is responsibility of particular completion
zstyle ':completion:*' sort false

# Don't complete uninteresting users
zstyle ':completion:*:*:*:users' ignored-patterns adm amanda apache at avahi avahi-autoipd
Expand Down

0 comments on commit 6947000

Please sign in to comment.