feat: a running lazysecrets says when a newer one was installed over it
CI / Checks (pull_request) Successful in 41s
PR title / Conventional title (pull_request) Successful in 1s

Every minute it looks whether its binary was replaced, from anywhere
(install.sh, go install, another lazysecrets): a stat, and the new
binary's --version when it changed. It shows → vX (restart), as after
U, so a long-running one (lazysecrets.nvim's) doesn't run an old
version unnoticed.
This commit is contained in:
keb committed 2026-10-19 21:30:18 +02:00
1 parent d0a8b1390d
commit d22e6fd7ac
7 files changed
+164 -1

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
package update
import (
"context"
"os"
"os/exec"
"strings"
)
// Binary is the binary a running lazysecrets started from, to notice a
// newer one installed over it (by install.sh, go install, a package
// manager or another lazysecrets): the running one keeps its old code
// until it restarts.
type Binary struct {
path string // as started, not resolved: a symlink a package manager moves is followed
info os.FileInfo
}
// Running is the binary this process runs, as it is now.
func Running() (*Binary, error) {
exe, err := os.Executable()
if err != nil {
return nil, err
}
info, err := os.Stat(exe)
if err != nil {
return nil, err
}
return &Binary{path: exe, info: info}, nil
}
// Replaced is the version of a binary installed over b since it was last
// looked at, as its --version says; ok is false while it's the same
// file. A look costs a stat; --version runs only when it changed.
func (b *Binary) Replaced(ctx context.Context) (version string, ok bool) {
info, err := os.Stat(b.path)
if err != nil || (os.SameFile(b.info, info) && info.ModTime().Equal(b.info.ModTime())) {
return "", false
}
out, err := exec.CommandContext(ctx, b.path, "--version").Output()
if err != nil {
return "", false // mid-install, or broken: look again later
}
b.info = info
v, ok := strings.CutPrefix(strings.TrimSpace(string(out)), "lazysecrets ")
return v, ok
}