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

+7
View File
@@ -536,6 +536,7 @@ func (m Model) Init() tea.Cmd {
if m.fetchOnStart {
cmds = append(cmds, m.fetchLatest(false))
}
cmds = append(cmds, m.watchBinary())
if m.open != nil {
msg := openRefMsg{ref: *m.open, action: m.openAction}
cmds = append(cmds, func() tea.Msg { return msg })
@@ -600,6 +601,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case installedMsg:
m.handleInstalled(msg)
case binaryTickMsg:
cmds = append(cmds, m.lookAtBinary())
case replacedMsg:
cmds = append(cmds, m.handleReplaced(msg))
case currentMsg:
if msg.gen == m.sourcesGen {
m.current = &msg.source
+38
View File
@@ -15,6 +15,44 @@ import (
type Updater struct {
Latest func(context.Context) (update.Release, error)
Install func(context.Context, update.Release) error
// Replaced says the version of a lazysecrets installed over the
// running one since it last looked; nil doesn't look.
Replaced func(context.Context) (version string, ok bool)
}
// binaryEvery is how often lazysecrets looks whether a newer binary was
// installed over it: a stat.
const binaryEvery = time.Minute
type binaryTickMsg struct{}
type replacedMsg struct{ version string }
func (m Model) watchBinary() tea.Cmd {
if m.updater == nil || m.updater.Replaced == nil {
return nil
}
return tea.Tick(binaryEvery, func(time.Time) tea.Msg { return binaryTickMsg{} })
}
func (m Model) lookAtBinary() tea.Cmd {
replaced := m.updater.Replaced
return func() tea.Msg {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
v, _ := replaced(ctx)
return replacedMsg{version: v}
}
}
// handleReplaced says a newer lazysecrets was installed over this one,
// as an update from U does, and looks again later.
func (m *Model) handleReplaced(msg replacedMsg) tea.Cmd {
if msg.version != "" && msg.version != m.version && msg.version != m.updated {
m.updated = msg.version
m.flash = done("lazysecrets " + msg.version + " was installed · restart lazysecrets to use it")
}
return m.watchBinary()
}
// updateEvery is how often lazysecrets looks for a newer release on its
+22
View File
@@ -130,3 +130,25 @@ func TestStartupWarning(t *testing.T) {
t.Errorf("no warning:\n%s", d.screen())
}
}
// A newer lazysecrets installed over the running one, from anywhere, is
// said as an update from U is.
func TestBinaryReplaced(t *testing.T) {
t.Parallel()
installed := ""
u := &Updater{
Latest: func(context.Context) (update.Release, error) { return update.Release{}, errors.New("offline") },
Install: func(context.Context, update.Release) error { return nil },
Replaced: func(context.Context) (string, bool) { return installed, installed != "" },
}
d := newDriverOpts(t, Options{Version: "v0.3.0", Updater: u})
d.send(binaryTickMsg{})
if strings.Contains(d.screen(), "restart") {
t.Fatalf("nothing installed, yet:\n%s", d.screen())
}
installed = "v0.3.1"
d.send(binaryTickMsg{})
if scr := d.screen(); !strings.Contains(scr, "lazysecrets v0.3.1 was installed · restart lazysecrets to use it") || !strings.Contains(scr, "→ v0.3.1 (restart)") {
t.Errorf("after an install:\n%s", scr)
}
}