Created
October 25, 2019 10:52
-
-
Save vonEdfa/839976f41bd51179b8ec10a1d9accb83 to your computer and use it in GitHub Desktop.
My fav bash configs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Lazy load NVM | |
declare -a NODE_GLOBALS=(`find ~/.nvm/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`) | |
NODE_GLOBALS+=("node") | |
NODE_GLOBALS+=("nvm") | |
export NVM_DIR="$HOME/.nvm" | |
load_nvm() { | |
#export NVM_DIR="$HOME/.nvm" | |
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm | |
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion} | |
} | |
nvm_loader() { | |
echo -n "Lazy loading nvm... " | |
load_nvm | |
echo -en "\033[2K\r" | |
} | |
for cmd in "${NODE_GLOBALS[@]}"; do | |
eval "${cmd}(){ unset -f ${NODE_GLOBALS[@]}; nvm_loader; ${cmd} \$@; }" | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run SSH agent when we log in | |
ssh_env="$HOME/.ssh/agent.env" | |
agent_load_env () { test -f "$ssh_env" && . "$ssh_env" >| /dev/null ; } | |
agent_start () { | |
(umask 077; ssh-agent >| "$ssh_env") | |
. "$ssh_env" >| /dev/null ; } | |
agent_load_env | |
# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running | |
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?) | |
lazy_load_agent=yes | |
init_agent() { | |
if [ ! "$SSH_AUTH_SOCK" ] || [ "$agent_run_state" = 2 ]; then | |
agent_start | |
ssh-add | |
elif [ "$SSH_AUTH_SOCK" ] && [ "$agent_run_state" = 1 ]; then | |
ssh-add | |
fi | |
} | |
lazy_agent() { | |
echo "Lazy loading ssh-agent..." | |
init_agent | |
#echo -en "\033[2K\r" | |
} | |
if [ ! "$lazy_load_agent" = yes ]; then | |
init_agent | |
# Always load when we're using vscode through WSL (high chance of git being used) | |
elif [ "$(pgrep -fc vscode)" -gt 0 ] && ([ ! "$SSH_AUTH_SOCK" ] || [ "$agent_run_state" -gt 0 ]); then | |
echo "Noticed vscode is running, starting ssh-agent..." | |
init_agent | |
#echo -en "\033[2K\r" | |
elif [ ! "$SSH_AUTH_SOCK" ] || [ "$agent_run_state" -gt 0 ]; then | |
LAZY_SSH_GLOBALS=("ssh") | |
for cmd in "${LAZY_SSH_GLOBALS[@]}"; do | |
eval "${cmd}(){ unset -f ${LAZY_SSH_GLOBALS[@]}; lazy_agent; ${cmd} \$@; }" | |
done | |
fi | |
unset ssh_env lazy_load_agent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment