penguin/utils

my env utils

commit dbd16e238a3f51987c0be042141dff5a2676d3df

author斟酌 鵬兄 <tgckpg@gmail.com>
date2026-06-26T14:03:34Z
subjectUpdate 15_gpg-agent
commit dbd16e238a3f51987c0be042141dff5a2676d3df
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2026-06-26T14:03:34Z

    Update 15_gpg-agent
---
 bash/sources/15_gpg-agent | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/bash/sources/15_gpg-agent b/bash/sources/15_gpg-agent
index 56bef87..958d09d 100644
--- a/bash/sources/15_gpg-agent
+++ b/bash/sources/15_gpg-agent
@@ -1,21 +1,26 @@
 #!/bin/bash
 
-which gpg-agent 2>&1 > /dev/null
-if [ $? -eq 0 ]; then
-    GPG_ENV="$HOME/.gnupg/environment"
+if command -v gpg-connect-agent >/dev/null 2>&1; then
+	export GPG_TTY
+	GPG_TTY="$(tty 2>/dev/null || true)"
 
-    function __start_agent {
-        gpg-agent --daemon > "${GPG_ENV}"
-        chmod 600 "${GPG_ENV}"
-        . "${GPG_ENV}" > /dev/null
-    }
+	gpg-connect-agent /bye >/dev/null 2>&1 || true
 
-    if [ -f "${GPG_ENV}" ]; then
-        . "${GPG_ENV}" > /dev/null
+	if [ -n "$GPG_TTY" ]; then
+		gpg-connect-agent updatestartuptty /bye >/dev/null 2>&1 || true
+	fi
+fi
+
+# Optional: use gpg-agent as ssh-agent only when configured.
+if command -v gpgconf >/dev/null 2>&1 &&
+	[ -f "${GNUPGHOME:-$HOME/.gnupg}/gpg-agent.conf" ] &&
+	grep -Eq '^[[:space:]]*enable-ssh-support([[:space:]]|$)' "${GNUPGHOME:-$HOME/.gnupg}/gpg-agent.conf"
+then
+	_rbash_gpg_ssh_sock="$(gpgconf --list-dirs agent-ssh-socket 2>/dev/null || true)"
+
+	if [ -n "$_rbash_gpg_ssh_sock" ]; then
+		export SSH_AUTH_SOCK="$_rbash_gpg_ssh_sock"
+	fi
 
-        gpg-agent > /dev/null 2>&1
-        [ "$?" -ne 0 ] && { __start_agent; }
-    else
-        __start_agent;
-    fi
+	unset _rbash_gpg_ssh_sock
 fi