From 6a7f53e2364695e71db8107a21feffe2ddf1a7b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl-Einar=20Bergstr=C3=B8m?= Date: Mon, 19 Oct 2026 21:31:05 +0200 Subject: [PATCH] feat: a running lazysecrets says when a newer one was installed over it (#90) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Karl-Einar Bergstrøm --- cmd/lazysecrets/update.go | 6 ++++- docs/config.md | 5 ++++ internal/ui/app/app.go | 7 +++++ internal/ui/app/update.go | 38 +++++++++++++++++++++++++++ internal/ui/app/update_test.go | 22 ++++++++++++++++ internal/update/binary.go | 47 ++++++++++++++++++++++++++++++++++ internal/update/binary_test.go | 40 +++++++++++++++++++++++++++++ 7 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 internal/update/binary.go create mode 100644 internal/update/binary_test.go diff --git a/cmd/lazysecrets/update.go b/cmd/lazysecrets/update.go index 86c46ea..7df8c3e 100644 --- a/cmd/lazysecrets/update.go +++ b/cmd/lazysecrets/update.go @@ -18,7 +18,7 @@ import ( // default ones. func updater(servers []string) *app.Updater { c := &update.Client{Servers: servers} - return &app.Updater{ + u := &app.Updater{ Latest: c.Latest, Install: func(ctx context.Context, r update.Release) error { exe, err := update.Executable() @@ -28,6 +28,10 @@ func updater(servers []string) *app.Updater { return c.Install(ctx, r, exe) }, } + if b, err := update.Running(); err == nil { + u.Replaced = b.Replaced + } + return u } // updateCmd is lazysecrets update [--check]: the newer release's diff --git a/docs/config.md b/docs/config.md index bfc3a71..28c1e9c 100644 --- a/docs/config.md +++ b/docs/config.md @@ -154,3 +154,8 @@ lazysecrets that's running keeps working, and the next start is the new version. A binary a package manager owns (Homebrew, Nix, a distro package) is left to it. Development builds (`dev-…`) aren't offered releases. + +A lazysecrets that keeps running, like the one lazysecrets.nvim hides, +looks every minute whether a newer one was installed over it, from +anywhere (`install.sh`, `go install`, another lazysecrets), and says so +in the status pane, `→ v0.3.1 (restart)`, until you restart it. diff --git a/internal/ui/app/app.go b/internal/ui/app/app.go index c274f0d..a369021 100644 --- a/internal/ui/app/app.go +++ b/internal/ui/app/app.go @@ -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 diff --git a/internal/ui/app/update.go b/internal/ui/app/update.go index 3726463..c928419 100644 --- a/internal/ui/app/update.go +++ b/internal/ui/app/update.go @@ -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 diff --git a/internal/ui/app/update_test.go b/internal/ui/app/update_test.go index 0ddc409..c62ff7e 100644 --- a/internal/ui/app/update_test.go +++ b/internal/ui/app/update_test.go @@ -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) + } +} diff --git a/internal/update/binary.go b/internal/update/binary.go new file mode 100644 index 0000000..92d21bf --- /dev/null +++ b/internal/update/binary.go @@ -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 +} diff --git a/internal/update/binary_test.go b/internal/update/binary_test.go new file mode 100644 index 0000000..1577ef1 --- /dev/null +++ b/internal/update/binary_test.go @@ -0,0 +1,40 @@ +package update + +import ( + "context" + "os" + "path/filepath" + "testing" + "time" +) + +func TestBinaryReplaced(t *testing.T) { + dir := t.TempDir() + exe := filepath.Join(dir, "lazysecrets") + write := func(version string) { + t.Helper() + tmp := filepath.Join(dir, ".new") + if err := os.WriteFile(tmp, []byte("#!/bin/sh\necho lazysecrets "+version+"\n"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.Rename(tmp, exe); err != nil { // as Install and install.sh do + t.Fatal(err) + } + } + write("v0.3.0") + info, _ := os.Stat(exe) + b := &Binary{path: exe, info: info} + ctx := context.Background() + + if v, ok := b.Replaced(ctx); ok { + t.Errorf("the same file: replaced by %q", v) + } + time.Sleep(10 * time.Millisecond) + write("v0.3.1") + if v, ok := b.Replaced(ctx); !ok || v != "v0.3.1" { + t.Errorf("installed over: %q %t", v, ok) + } + if v, ok := b.Replaced(ctx); ok { + t.Errorf("said again: %q", v) + } +}