Let's imagine you want to save your time writing all kubectl command: kubectl describe pods in shorter way: k d p.
So the solution is to add function to ~/.bashrc:
k() {
cmd_kubectl="command kubectl"
case ${1} in
g)
shift
kubectl_get="${cmd_kubectl} get"
case ${1} in
p)
shift
${kubectl_get} pods "$@"
;;
d)
shift
${kubectl_get} deploy "$@"
;;
n)
shift
${kubectl_get} ns "$@"
;;
i)
shift
${kubectl_get} ing "$@"
;;
j)
shift
${kubectl_get} job "$@"
;;
*)
${kubectl_get} "$@"
;;
esac
;;
d)
shift
kubectl_desc="${cmd_kubectl} describe"
case ${1} in
p)
shift
${kubectl_desc} pods "$@"
;;
d)
shift
${kubectl_desc} deploy "$@"
;;
n)
shift
${kubectl_desc} ns "$@"
;;
i)
shift
${kubectl_desc} ing "$@"
;;
j)
shift
${kubectl_desc} job "$@"
;;
*)
${kubectl_desc} "$@"
;;
esac
;;
*)
${cmd_kubectl} "$@"
;;
esac
}
But I'd like to save an effort and improve it more, so my question is:
How to enable auto-completion on this function to allow fill e.g. the name of the pod?
Usage:
k d p -> Tab -> k d p nginx (result)
I tried to think up thanks following references, but probably I'm not enough experienced Linux user/developer to compose the final solution.
- How to enable kubernetes commands autocomplete
- How to enable bash auto-completion for a function?
- https://askubuntu.com/questions/68175/how-to-create-script-with-auto-complete
Thanks in advance!