7e2aa2c8cbdb13285ce62d07b41fb3c3c09190e0

Author
FiNeXdesign <287009+finex@users.noreply.github.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(keychain): add version handling (#13898)

Co-authored-by: Carlo Sala <carlosalag@protonmail.com>

Diff

 1diff --git a/plugins/keychain/keychain.plugin.zsh b/plugins/keychain/keychain.plugin.zsh
 2index 23cf1af9a8c02dac198251f04ccd0c31f02afe78..f1ca0d934932dd753edca4037b82b0b4f168303e 100644
 3--- a/plugins/keychain/keychain.plugin.zsh
 4+++ b/plugins/keychain/keychain.plugin.zsh
 5@@ -4,37 +4,50 @@
 6 SHORT_HOST=${SHORT_HOST:-${(%):-%m}}
 7 
 8 function {
 9-	local agents
10-	local -a identities
11-	local -a options
12-	local _keychain_env_sh
13-	local _keychain_env_sh_gpg
14-
15-	# load agents to start.
16-	zstyle -s :omz:plugins:keychain agents agents
17-
18-	# load identities to manage.
19-	zstyle -a :omz:plugins:keychain identities identities
20-
21-	# load additional options
22-	zstyle -a :omz:plugins:keychain options options
23-
24-	# Check keychain version to decide whether to use --agents
25-	local version_string=$(keychain --version 2>&1)
26-  # start keychain, only use --agents for versions below 2.9.0
27-	autoload -Uz is-at-least
28-	if [[ "$version_string" =~ 'keychain ([0-9]+\.[0-9]+)' ]] && \
29-      is-at-least 2.9 "$match[1]"; then
30-		keychain ${^options:-} ${^identities} --host $SHORT_HOST
31-	else
32-		keychain ${^options:-} --agents ${agents:-gpg} ${^identities} --host $SHORT_HOST
33-	fi
34-
35-	# Get the filenames to store/lookup the environment from
36-	_keychain_env_sh="$HOME/.keychain/$SHORT_HOST-sh"
37-	_keychain_env_sh_gpg="$HOME/.keychain/$SHORT_HOST-sh-gpg"
38-
39-	# Source environment settings.
40-	[ -f "$_keychain_env_sh" ]     && . "$_keychain_env_sh"
41-	[ -f "$_keychain_env_sh_gpg" ] && . "$_keychain_env_sh_gpg"
42+  local -a identities
43+  local -a options
44+  local _keychain_env_sh
45+  local _keychain_env_sh_gpg
46+  local version_string major
47+
48+  # load identities to manage.
49+  zstyle -a :omz:plugins:keychain identities identities
50+
51+  # load additional options
52+  zstyle -a :omz:plugins:keychain options options
53+
54+  # Detect major version robustly: case-insensitive, tolerant of
55+  # "keychain 3.0.0_beta3" / "Keychain 3.0.0" / etc.
56+  version_string=$(keychain --version 2>&1)
57+  if [[ "${version_string:l}" =~ '([0-9]+)\.[0-9]+' ]]; then
58+    major=${match[1]}
59+  else
60+    major=2
61+  fi
62+
63+  if (( major >= 3 )); then
64+    # Keychain 3.x: subcommand-based CLI, no --agents.
65+    keychain add ${^options:-} --host $SHORT_HOST ${^identities}
66+  else
67+    local agents
68+
69+    # load agents to start (only used by keychain < 2.9).
70+    zstyle -s :omz:plugins:keychain agents agents
71+
72+    autoload -Uz is-at-least
73+    if [[ "${version_string:l}" =~ 'keychain ([0-9]+\.[0-9]+)' ]] && \
74+       is-at-least 2.9 "$match[1]"; then
75+      keychain ${^options:-} ${^identities} --host $SHORT_HOST
76+    else
77+      keychain ${^options:-} --agents ${agents:-gpg} ${^identities} --host $SHORT_HOST
78+    fi
79+  fi
80+
81+  # Get the filenames to store/lookup the environment from
82+  _keychain_env_sh="$HOME/.keychain/$SHORT_HOST-sh"
83+  _keychain_env_sh_gpg="$HOME/.keychain/$SHORT_HOST-sh-gpg"
84+
85+  # Source environment settings.
86+  [ -f "$_keychain_env_sh" ]     && . "$_keychain_env_sh"
87+  [ -f "$_keychain_env_sh_gpg" ] && . "$_keychain_env_sh_gpg"
88 }