Refactor keychain plugin for version handling

The new keychain 3.x break the plugin version-detection logic.

This PR makes the plugin works again with the new version.
This commit is contained in:
FiNeXdesign 2026-07-27 10:28:48 +00:00 committed by GitHub
commit 5d0b4ca514
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,37 +4,45 @@
SHORT_HOST=${SHORT_HOST:-${(%):-%m}} SHORT_HOST=${SHORT_HOST:-${(%):-%m}}
function { function {
local agents local -a identities
local -a identities local -a options
local -a options local _keychain_env_sh
local _keychain_env_sh local _keychain_env_sh_gpg
local _keychain_env_sh_gpg local version_string major
# load agents to start. # load identities to manage.
zstyle -s :omz:plugins:keychain agents agents zstyle -a :omz:plugins:keychain identities identities
# load identities to manage. # load additional options
zstyle -a :omz:plugins:keychain identities identities zstyle -a :omz:plugins:keychain options options
# load additional options # Detect major version robustly: case-insensitive, tolerant of
zstyle -a :omz:plugins:keychain options options # "keychain 3.0.0_beta3" / "Keychain 3.0.0" / etc.
version_string=$(keychain --version 2>&1)
if [[ "${version_string:l}" =~ '([0-9]+)\.[0-9]+' ]]; then
major=${match[1]}
else
major=2
fi
# Check keychain version to decide whether to use --agents if (( major >= 3 )); then
local version_string=$(keychain --version 2>&1) # Keychain 3.x: subcommand-based CLI, no --agents.
# start keychain, only use --agents for versions below 2.9.0 keychain add ${^options:-} --host $SHORT_HOST ${^identities}
autoload -Uz is-at-least else
if [[ "$version_string" =~ 'keychain ([0-9]+\.[0-9]+)' ]] && \ autoload -Uz is-at-least
is-at-least 2.9 "$match[1]"; then if [[ "${version_string:l}" =~ 'keychain ([0-9]+\.[0-9]+)' ]] && \
keychain ${^options:-} ${^identities} --host $SHORT_HOST is-at-least 2.9 "$match[1]"; then
else keychain ${^options:-} ${^identities} --host $SHORT_HOST
keychain ${^options:-} --agents ${agents:-gpg} ${^identities} --host $SHORT_HOST else
fi keychain ${^options:-} --agents ${agents:-ssh} ${^identities} --host $SHORT_HOST
fi
fi
# Get the filenames to store/lookup the environment from # Get the filenames to store/lookup the environment from
_keychain_env_sh="$HOME/.keychain/$SHORT_HOST-sh" _keychain_env_sh="$HOME/.keychain/$SHORT_HOST-sh"
_keychain_env_sh_gpg="$HOME/.keychain/$SHORT_HOST-sh-gpg" _keychain_env_sh_gpg="$HOME/.keychain/$SHORT_HOST-sh-gpg"
# Source environment settings. # Source environment settings.
[ -f "$_keychain_env_sh" ] && . "$_keychain_env_sh" [ -f "$_keychain_env_sh" ] && . "$_keychain_env_sh"
[ -f "$_keychain_env_sh_gpg" ] && . "$_keychain_env_sh_gpg" [ -f "$_keychain_env_sh_gpg" ] && . "$_keychain_env_sh_gpg"
} }