about summary refs log tree commit diff
path: root/home/shell/fish/functions/find-ssh-agent.fish
blob: 9e2de8d6b4371c73a7033ab8b4ffdf1c8ec2c114 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function find-ssh-agent --description "find or run ssh-agent"
    # let's avoid storing the agent's socket under /tmp
    set -l ssh_auth_sock $XDG_RUNTIME_DIR/ssh-agent.sock

    if set -q SSH_AGENT_PID; and set -q SSH_AUTH_SOCK
        # if variables already defined, then try to connect to agent
        ssh-add -l &>/dev/null
        test $status -ne 2; and return
    end

    set -l user_id (id -u)
    set -l ssh_agent_pid (pgrep --exact --newest --uid $user_id ssh-agent)

    if test -S $ssh_auth_sock
        set --global --export SSH_AUTH_SOCK $ssh_auth_sock
        set --global --export SSH_AGENT_PID $ssh_agent_pid
    else
        eval (ssh-agent -c -a $ssh_auth_sock)
    end
end