Move ssh-agent logic to lib script

This commit is contained in:
Cody Ray Hoeft 2018-12-13 21:12:18 -08:00
parent 0c9e0e2f8c
commit 7e1bc4297a
2 changed files with 24 additions and 27 deletions

22
vendor/lib/start-ssh-agent.sh vendored Normal file
View File

@ -0,0 +1,22 @@
# Copied from https://help.github.com/articles/working-with-ssh-key-passphrases
env=~/.ssh/agent.env
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }
agent_start () {
(umask 077; ssh-agent >| "$env")
. "$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 $?)
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
unset env

View File

@ -3,30 +3,5 @@
# To add a new vendor to the path, do something like: # To add a new vendor to the path, do something like:
# export PATH=${CMDER_ROOT}/vendor/whatever:${PATH} # export PATH=${CMDER_ROOT}/vendor/whatever:${PATH}
function launch_ssh_agent { # Uncomment this to have the ssh agent load with the first bash terminal
# Copied from (https://help.github.com/articles/working-with-ssh-key-passphrases) # . "${CMDER_ROOT}/vendor/lib/start-ssh-agent.sh"
env=~/.ssh/agent.env
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }
agent_start () {
(umask 077; ssh-agent >| "$env")
. "$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 $?)
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
unset env
}
# Run ssh-agent automatically when you open your first bash terminal
# launch_ssh_agent