4a69ee575c9a8ec96fb6378fecae7777c7afba84

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

feat(ssh-agent): allow specifying absolute path to `identities`

Fixes #9650

Diff

 1diff --git a/plugins/ssh-agent/README.md b/plugins/ssh-agent/README.md
 2index f46e8bf6af3319077887c52fcb8fc79f48dedb3c..d1a504b1ece2cf58b92dcdb1124c386d13bc1ba9 100644
 3--- a/plugins/ssh-agent/README.md
 4+++ b/plugins/ssh-agent/README.md
 5@@ -27,6 +27,15 @@ To **load multiple identities** use the `identities` style, For example:
 6 zstyle :omz:plugins:ssh-agent identities id_rsa id_rsa2 id_github
 7 ```
 8 
 9+**NOTE:** the identities may be an absolute path if they are somewhere other than
10+`~/.ssh`. For example:
11+
12+```zsh
13+zstyle :omz:plugins:ssh-agent identities ~/.config/ssh/id_rsa ~/.config/ssh/id_rsa2 ~/.config/ssh/id_github
14+# which can be simplified to
15+zstyle :omz:plugins:ssh-agent identities ~/.config/ssh/{id_rsa,id_rsa2,id_github}
16+```
17+
18 ----
19 
20 To **set the maximum lifetime of the identities**, use the `lifetime` style.
21diff --git a/plugins/ssh-agent/ssh-agent.plugin.zsh b/plugins/ssh-agent/ssh-agent.plugin.zsh
22index 2049145fd2b4cbd5d37c86a3f006c7264ec463b0..c2b9546a2ebba6df423595f451570145ee41d93c 100644
23--- a/plugins/ssh-agent/ssh-agent.plugin.zsh
24+++ b/plugins/ssh-agent/ssh-agent.plugin.zsh
25@@ -23,12 +23,12 @@ function _start_agent() {
26 }
27 
28 function _add_identities() {
29-  local id line sig lines
30+  local id file line sig lines
31   local -a identities loaded_sigs loaded_ids not_loaded
32   zstyle -a :omz:plugins:ssh-agent identities identities
33 
34   # check for .ssh folder presence
35-  if [[ ! -d $HOME/.ssh ]]; then
36+  if [[ ! -d "$HOME/.ssh" ]]; then
37     return
38   fi
39 
40@@ -52,10 +52,12 @@ function _add_identities() {
41 
42   # add identities if not already loaded
43   for id in $identities; do
44+    # if id is an absolute path, make file equal to id
45+    [[ "$id" = /* ]] && file="$id" || file="$HOME/.ssh/$id"
46     # check for filename match, otherwise try for signature match
47-    if [[ ${loaded_ids[(I)$HOME/.ssh/$id]} -le 0 ]]; then
48-      sig="$(ssh-keygen -lf "$HOME/.ssh/$id" | awk '{print $2}')"
49-      [[ ${loaded_sigs[(I)$sig]} -le 0 ]] && not_loaded+=("$HOME/.ssh/$id")
50+    if [[ ${loaded_ids[(I)$file]} -le 0 ]]; then
51+      sig="$(ssh-keygen -lf "$file" | awk '{print $2}')"
52+      [[ ${loaded_sigs[(I)$sig]} -le 0 ]] && not_loaded+=("$file")
53     fi
54   done
55