3f0672ff946a508329e382261e6eb8f837106e38
- Author
- Marc Cornellà <hello@mcornella.com>
- Committer
- Marc Cornellà <hello@mcornella.com>
- Date
Message
refactor(ssh-agent): clean up and reorganize code
Diff
1diff --git a/plugins/ssh-agent/ssh-agent.plugin.zsh b/plugins/ssh-agent/ssh-agent.plugin.zsh
2index acad4e83e7473a30693eea0b3734e97a087fafea..2049145fd2b4cbd5d37c86a3f006c7264ec463b0 100644
3--- a/plugins/ssh-agent/ssh-agent.plugin.zsh
4+++ b/plugins/ssh-agent/ssh-agent.plugin.zsh
5@@ -1,12 +1,25 @@
6+# Get the filename to store/lookup the environment from
7+ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
8+
9 function _start_agent() {
10+ # Check if ssh-agent is already running
11+ if [[ -f "$ssh_env_cache" ]]; then
12+ . "$ssh_env_cache" > /dev/null
13+
14+ {
15+ [[ "$USERNAME" = root ]] && command ps ax || command ps x
16+ } | command grep ssh-agent | command grep -q $SSH_AGENT_PID && return 0
17+ fi
18+
19+ # Set a maximum lifetime for identities added to ssh-agent
20 local lifetime
21 zstyle -s :omz:plugins:ssh-agent lifetime lifetime
22
23 # start ssh-agent and setup environment
24 echo Starting ssh-agent...
25- ssh-agent -s ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! $_ssh_env_cache
26- chmod 600 $_ssh_env_cache
27- . $_ssh_env_cache > /dev/null
28+ ssh-agent -s ${lifetime:+-t} ${lifetime} | sed '/^echo/d' >! "$ssh_env_cache"
29+ chmod 600 "$ssh_env_cache"
30+ . "$ssh_env_cache" > /dev/null
31 }
32
33 function _add_identities() {
34@@ -71,26 +84,12 @@ function _add_identities() {
35 ssh-add "${args[@]}" ${^not_loaded}
36 }
37
38-# Get the filename to store/lookup the environment from
39-_ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
40-
41 # test if agent-forwarding is enabled
42-zstyle -b :omz:plugins:ssh-agent agent-forwarding _agent_forwarding
43-
44-if [[ $_agent_forwarding == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
45- # Add a nifty symlink for screen/tmux if agent forwarding
46- [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USERNAME-screen
47-elif [[ -f "$_ssh_env_cache" ]]; then
48- # Source SSH settings, if applicable
49- . $_ssh_env_cache > /dev/null
50- if [[ $USERNAME == "root" ]]; then
51- FILTER="ax"
52- else
53- FILTER="x"
54- fi
55- ps $FILTER | grep ssh-agent | grep -q $SSH_AGENT_PID || {
56- _start_agent
57- }
58+zstyle -b :omz:plugins:ssh-agent agent-forwarding agent_forwarding
59+
60+# Add a nifty symlink for screen/tmux if agent forwarding
61+if [[ $agent_forwarding = "yes" && -n "$SSH_AUTH_SOCK" && ! -L "$SSH_AUTH_SOCK" ]]; then
62+ ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USERNAME-screen
63 else
64 _start_agent
65 fi
66@@ -104,15 +103,15 @@ fi
67 ret=\$?
68
69 command rm -rf '$ZSH_CACHE_DIR/ssh-agent.lock'
70- unset _agent_forwarding _ssh_env_cache
71+ unset agent_forwarding ssh_env_cache
72 unfunction _start_agent _add_identities 2>/dev/null
73
74 return \$ret
75 " EXIT INT QUIT
76
77 _add_identities
78+
79 }
80
81-# tidy up after ourselves
82-unset _agent_forwarding _ssh_env_cache
83+unset agent_forwarding ssh_env_cache
84 unfunction _start_agent _add_identities