使用懒加载优化bash启动速度
优化前
bash补全配置
# ~/.bash_profile
source .bash_completion
# ~/.bash_completion
source '/opt/homebrew/etc/profile.d/bash_completion.sh'
source '/Applications/Docker.app/Contents/Resources/etc/docker.bash-completion'
source <(helm completion bash)
source <(tkn completion bash)
source <(istioctl completion bash)
source <(gopass completion bash)
source <(k3d completion bash)
source <(goup completion bash)
source <(kubectl completion bash)
source <(kubecm completion bash)
complete -F __start_kubectl k
complete -F __start_kubecm kcm
bash 启动时间
优化后
bash补全配置
# ~/.bash_profile
source .bash_completion
source <(_lazy_load)
# ~/.bash_completion
__lazy_load_complete_with_binary(){
for i in $@
do
echo "$i() { unset -f $i; source <($i completion bash);$i \$@; }"
done
}
__lazy_load_complete_with_alias(){
echo "alias $2=$1; $1() { unset -f $1; source <($1 completion bash);complete -F __start_$1 $2 ;$1 \$@; }"
}
__lazy_load_complete_with_file(){
echo "$1() { unset -f $1; source $2 ; $1 \$@; }"
}
_lazy_load(){
cat /opt/homebrew/etc/profile.d/bash_completion.sh
__lazy_load_complete_with_file docker '/Applications/Docker.app/Contents/Resources/etc/docker.bash-completion'
__lazy_load_complete_with_alias kubectl k
__lazy_load_complete_with_alias kubecm kcm
__lazy_load_complete_with_binary helm tkn istioctl gopass k3d goup
}
bash 启动时间