mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-09-16 15:58:25 +00:00
feature: add bash completion for plugin mode of kubectl ctx and kubectl ns
if added to PATH and name `kubectl_complete-ns` and `kubectl_complete-ctx` these two script provide completion for ns and ctx cli in kubectl plugin mode
This commit is contained in:
28
completion/plugins/kubectl_complete-ctx
Executable file
28
completion/plugins/kubectl_complete-ctx
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/bin/bash
|
||||
|
||||
__kubectl_debug()
|
||||
{
|
||||
if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then
|
||||
echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
|
||||
fi
|
||||
}
|
||||
|
||||
_kube_namespaces()
|
||||
{
|
||||
local args lastArgs;
|
||||
|
||||
args=("${@:1}")
|
||||
__kubectl_debug "arguments list ${args[*]}"
|
||||
len=${#args[@]}
|
||||
__kubectl_debug "arguments len is $len"
|
||||
if [[ $len -gt 0 ]]; then
|
||||
lastArgs="${args[$((len-1))]}"
|
||||
else
|
||||
lastArgs=""
|
||||
fi
|
||||
__kubectl_debug "arguments last args on which completion will be done $lastArgs"
|
||||
COMPREPLY=("$(compgen -W "$(kubectl config get-contexts -o name)" -- $lastArgs )");
|
||||
}
|
||||
|
||||
_kube_namespaces "$@"
|
||||
echo "${COMPREPLY[*]}"
|
28
completion/plugins/kubectl_complete-ns
Executable file
28
completion/plugins/kubectl_complete-ns
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/bin/bash
|
||||
|
||||
__kubectl_debug()
|
||||
{
|
||||
if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then
|
||||
echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
|
||||
fi
|
||||
}
|
||||
|
||||
_kube_namespaces()
|
||||
{
|
||||
local args lastArgs;
|
||||
|
||||
args=("${@:1}")
|
||||
__kubectl_debug "arguments list ${args[*]}"
|
||||
len=${#args[@]}
|
||||
__kubectl_debug "arguments len is $len"
|
||||
if [[ $len -gt 0 ]]; then
|
||||
lastArgs="${args[$((len-1))]}"
|
||||
else
|
||||
lastArgs=""
|
||||
fi
|
||||
__kubectl_debug "arguments last args on which completion will be done $lastArgs"
|
||||
COMPREPLY=("$(compgen -W "- $(kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}')" -- $lastArgs )");
|
||||
}
|
||||
|
||||
_kube_namespaces "$@"
|
||||
echo "${COMPREPLY[*]}"
|
Reference in New Issue
Block a user