1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
package pinentry
import (
"os"
"path/filepath"
"regexp"
"runtime"
)
var gnuPGAgentConfPINEntryProgramRx = regexp.MustCompile(`(?m)^\s*pinentry-program\s+(\S+)`)
// WithBinaryNameFromGnuPGAgentConf sets the name of the pinentry binary by
// reading ~/.gnupg/gpg-agent.conf, if it exists.
func WithBinaryNameFromGnuPGAgentConf() (clientOption ClientOption) {
clientOption = func(*Client) {}
userHomeDir, err := os.UserHomeDir()
if err != nil {
return
}
data, err := os.ReadFile(filepath.Join(userHomeDir, ".gnupg", "gpg-agent.conf"))
if err != nil {
return
}
match := gnuPGAgentConfPINEntryProgramRx.FindSubmatch(data)
if match == nil {
return
}
return func(c *Client) {
c.binaryName = string(match[1])
}
}
// WithGPGTTY sets the tty.
func WithGPGTTY() ClientOption {
if runtime.GOOS == "windows" {
return nil
}
gpgTTY, ok := os.LookupEnv("GPG_TTY")
if !ok {
return nil
}
return WithCommandf("OPTION %s=%s", OptionTTYName, gpgTTY)
}
|