Skip to content

refactor: Extract shared GPG temp-directory setup into a helper#10

Merged
bketelsen merged 1 commit intomainfrom
yeti/improve-e735
Mar 25, 2026
Merged

refactor: Extract shared GPG temp-directory setup into a helper#10
bketelsen merged 1 commit intomainfrom
yeti/improve-e735

Conversation

@bketelsen
Copy link
Copy Markdown

In internal/signer/gpg.go, three methods — SignCleartext (line 83), SignDetachedBinary (line 143), and SignDetachedBinaryFromFile (line 189) — each independently create a temporary GPG home directory, import the private key, and clean up afterward. This is ~15 lines of identical boilerplate repeated three times.

Extract a helper like withGPGHome(fn func(tmpDir string) error) error (or similar) that handles temp dir creation, key import, and deferred cleanup. Each signing method would then only contain its format-specific GPG invocation. This also ensures that any future bug fix to the setup/teardown logic (e.g., setting --no-permission-warning) only needs to happen in one place.

Additionally, when SignDetachedBinaryFromFile is called in a loop (e.g., Pacman signs every package individually in internal/generator/pacman/generator.go:157), a new GPG home is created and the key is re-imported for every single package. Consider caching the GPG home directory for the lifetime of the GPGSigner (creating it lazily on first use, cleaning it up via a Close() method) to avoid redundant key imports.


Automated improvement by yeti improvement-identifier

Extract the repeated temp-directory creation, key import, and cleanup
from SignCleartext, SignDetachedBinary, and SignDetachedBinaryFromFile
into a shared ensureGPGHome() helper with lazy initialization via
sync.Once. The GPG home is created once on first use and reused for
all subsequent CLI signing operations, avoiding redundant key imports
(e.g., when Pacman signs each package individually in a loop). A
Close() method is added for cleanup, called via io.Closer type
assertion in generate.go.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bketelsen bketelsen merged commit 8f5d668 into main Mar 25, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants