keychain.plugin.zsh
1637 bytes
1(( $+commands[keychain] )) || return
2
3# Define SHORT_HOST if not defined (%m = host name up to first .)
4SHORT_HOST=${SHORT_HOST:-${(%):-%m}}
5
6function {
7 local -a identities
8 local -a options
9 local _keychain_env_sh
10 local _keychain_env_sh_gpg
11 local version_string major
12
13 # load identities to manage.
14 zstyle -a :omz:plugins:keychain identities identities
15
16 # load additional options
17 zstyle -a :omz:plugins:keychain options options
18
19 # Detect major version robustly: case-insensitive, tolerant of
20 # "keychain 3.0.0_beta3" / "Keychain 3.0.0" / etc.
21 version_string=$(keychain --version 2>&1)
22 if [[ "${version_string:l}" =~ '([0-9]+)\.[0-9]+' ]]; then
23 major=${match[1]}
24 else
25 major=2
26 fi
27
28 if (( major >= 3 )); then
29 # Keychain 3.x: subcommand-based CLI, no --agents.
30 keychain add ${^options:-} --host $SHORT_HOST ${^identities}
31 else
32 local agents
33
34 # load agents to start (only used by keychain < 2.9).
35 zstyle -s :omz:plugins:keychain agents agents
36
37 autoload -Uz is-at-least
38 if [[ "${version_string:l}" =~ 'keychain ([0-9]+\.[0-9]+)' ]] && \
39 is-at-least 2.9 "$match[1]"; then
40 keychain ${^options:-} ${^identities} --host $SHORT_HOST
41 else
42 keychain ${^options:-} --agents ${agents:-gpg} ${^identities} --host $SHORT_HOST
43 fi
44 fi
45
46 # Get the filenames to store/lookup the environment from
47 _keychain_env_sh="$HOME/.keychain/$SHORT_HOST-sh"
48 _keychain_env_sh_gpg="$HOME/.keychain/$SHORT_HOST-sh-gpg"
49
50 # Source environment settings.
51 [ -f "$_keychain_env_sh" ] && . "$_keychain_env_sh"
52 [ -f "$_keychain_env_sh_gpg" ] && . "$_keychain_env_sh_gpg"
53}