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:
RouxAntoine
2023-02-06 01:27:31 +01:00
parent 561793c356
commit a4757692a6
2 changed files with 56 additions and 0 deletions

View 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[*]}"

View 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[*]}"