1
0
dotfiles/.config/zsh/rc/ssh-agent.zsh

63 lines
1.6 KiB
Bash
Raw Normal View History

2024-03-04 15:35:55 +03:00
#!/bin/zsh
z-ssh-agent() {
2024-03-04 16:05:08 +03:00
while : ; do
[ -n "${SSH_AGENT_PID}" ] || break
2024-03-04 15:35:55 +03:00
z-proc-exists "${SSH_AGENT_PID}" || break
2024-03-04 16:05:08 +03:00
[ -n "${SSH_AUTH_SOCK}" ] || break
[ -S "${SSH_AUTH_SOCK}" ] || break
2024-03-04 15:35:55 +03:00
## don't bother with ssh agent socket if it already set
2024-03-04 16:05:08 +03:00
return 0
2024-03-04 15:35:55 +03:00
break
done
2024-03-04 15:47:43 +03:00
[ -z "${SSH_AGENT_PID}" ] || kill "${SSH_AGENT_PID}"
unset SSH_AGENT_PID
2024-03-04 15:35:55 +03:00
(( ${+commands[ssh-agent]} )) || return 127
if [ -z "${SSH_AUTH_SOCK}" ] ; then
local sock_dir
if [ "${XDG_RUNTIME_DIR}" = "${TMPDIR}" ] ; then
sock_dir="${ZSHU[d_zdot]}/.cache/ssh"
else
sock_dir="${XDG_RUNTIME_DIR}/ssh"
fi
mkdir -p "${sock_dir}"
SSH_AUTH_SOCK="${sock_dir}/ssh-agent.sock"
fi
2024-03-04 16:05:08 +03:00
local pid_file
pid_file="${SSH_AUTH_SOCK:h}/ssh-agent.pid"
while : ; do
[ -s "${pid_file}" ] || break
SSH_AGENT_PID=$(cat "${pid_file}")
z-proc-exists "${SSH_AGENT_PID}" || break
[ -S "${SSH_AUTH_SOCK}" ] || break
## don't bother with ssh agent socket if it already set
export SSH_AGENT_PID SSH_AUTH_SOCK
return 0
done
unset SSH_AGENT_PID
2024-03-04 15:35:55 +03:00
{
2024-03-04 15:47:43 +03:00
eval "$(ssh-agent -s -a "${SSH_AUTH_SOCK}")"
2024-03-04 15:35:55 +03:00
} >/dev/null
2024-03-04 16:05:08 +03:00
while : ; do
[ -n "${SSH_AGENT_PID}" ] || break
[ -n "${SSH_AUTH_SOCK}" ] || break
[ -S "${SSH_AUTH_SOCK}" ] || break
echo "${SSH_AGENT_PID}" > "${pid_file}"
export SSH_AGENT_PID SSH_AUTH_SOCK
return 0
done
unset SSH_AGENT_PID SSH_AUTH_SOCK
return 1
2024-03-04 15:35:55 +03:00
}