[英]Enable tab auto completion for kubectl in bash function
Given a bash function in .bashrc such as给定 .bashrc 中的 bash 函数,例如
kgp () {
kubectl get po -n $1 $2
}
Is it possible to have kubectl auto complete work for k8s resources such as namespaces/pods?是否可以让 kubectl 自动完成 k8s 资源(例如命名空间/pod)的工作? As an example if I use
例如,如果我使用
kubectl get po -n nsprefix podprefix
I can tab auto complete the prefix.我可以自动完成前缀。 Whereas with the positional parameters when I call
而当我调用位置参数时
kgp nsprefix podprefix
I have to type out the entire resource name.我必须输入整个资源名称。
Yes, that's because bash-completion only understands known commands , not aliases or new functions that you have made up.是的,那是因为 bash-completion 只理解已知命令,而不是别名或您编写的新函数。 You will experience the same thing with a trivial example of
alias whee=/bin/ls
and then whee <TAB>
will do nothing because it doesn't "recurse" into that alias, and for sure does not attempt to call your function in order to find out what arguments it could possibly accept.您将通过
alias whee=/bin/ls
的一个简单示例遇到同样的事情,然后whee <TAB>
将什么也不做,因为它不会“递归”到该别名中,并且肯定不会尝试调用您的函数为了找出它可能接受的论点。 That could potentially be catastrophic这可能是灾难性的
You're welcome to create a new complete
handler for your custom kgp
, but that's the only way you'll get the desired behavior欢迎您为自定义
kgp
创建一个新的complete
处理程序,但这是获得所需行为的唯一方法
_kgp_completer() {
local cur prev words cword
COMPREPLY=()
_get_comp_words_by_ref -n : cur prev words cword
if [[ $cword == 1 ]] && [[ -z "$cur" ]]; then
COMPREPLY=( $(echo ns1 ns2 ns3) )
elif [[ $cword == 2 ]] && [[ -z "$cur" ]]; then
COMPREPLY=( $(echo pod1 pod2 pod3) )
fi
echo "DEBUG: cur=$cur prev=$prev words=$words cword=$cword COMPREPLY=${COMPREPLY[@]}" >&2
}
complete -F _kgp_completer kgp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.