feat: lazysecrets list's JSON says when an entry was last used, and how often

This commit is contained in:
keb committed 2026-10-14 21:24:01 +02:00
1 parent 0e4580f245
commit 33a9b0864d
3 files changed
+23 -11

No files matched your search

+2 -2
View File
@@ -115,9 +115,9 @@ func listCmd(providers []provider.Provider, st *store.Store, cachePath string, t
} }
f = listing.Run(context.Background(), listing.Options{ f = listing.Run(context.Background(), listing.Options{
Providers: providers, Cache: f, MaxAge: *maxAge, Cached: *cached, Kinds: only, Timeout: timeout, Providers: providers, Cache: f, MaxAge: *maxAge, Cached: *cached, Kinds: only, Timeout: timeout,
Mark: func(link, account string) (bool, bool) { Mark: func(link, account string) (bool, time.Time, int) {
n := st.Node(link) n := st.Node(link)
return n.StarredFor(account), !n.LastUsed.IsZero() return n.StarredFor(account), n.LastUsed, n.Uses
}, },
}, emit) }, emit)
if cachePath != "" && !*cached { if cachePath != "" && !*cached {
+11 -7
View File
@@ -40,9 +40,11 @@ type Entry struct {
// AccountLabel names the az account, when there are several. // AccountLabel names the az account, when there are several.
AccountLabel string `json:"accountLabel,omitempty"` AccountLabel string `json:"accountLabel,omitempty"`
// Starred is starred for this account; Recent is used lately (Recent, // Starred is starred for this account; Recent is used lately (Recent,
// R, in the TUI). // R, in the TUI): LastUsed is when, Uses how often.
Starred bool `json:"starred"` Starred bool `json:"starred"`
Recent bool `json:"recent"` Recent bool `json:"recent"`
LastUsed time.Time `json:"lastUsed,omitzero"`
Uses int `json:"uses,omitempty"`
// Error says why a source's vaults or a vault's secrets couldn't be // Error says why a source's vaults or a vault's secrets couldn't be
// listed. The entry is then that source or vault, again, or with an // listed. The entry is then that source or vault, again, or with an
// account's sources unreadable, the account without a link. // account's sources unreadable, the account without a link.
@@ -61,9 +63,10 @@ type Options struct {
// Lists only needed for other kinds aren't loaded: sources and vaults // Lists only needed for other kinds aren't loaded: sources and vaults
// alone don't load any secrets. Lists that fail are always listed. // alone don't load any secrets. Lists that fail are always listed.
Kinds []string Kinds []string
// Mark tells whether the entry with link is starred for account, and // Mark tells whether the entry with link is starred for account,
// used lately; nil marks nothing. // and when it was last used and how often (zero: not lately); nil
Mark func(link, account string) (starred, recent bool) // marks nothing.
Mark func(link, account string) (starred bool, lastUsed time.Time, uses int)
Timeout func(provider.Kind) time.Duration Timeout func(provider.Kind) time.Duration
Now func() time.Time Now func() time.Time
} }
@@ -225,7 +228,8 @@ func (l *lister) put(p provider.Provider, e Entry) {
} }
e.Account = p.ID() e.Account = p.ID()
if l.o.Mark != nil && e.Link != "" { if l.o.Mark != nil && e.Link != "" {
e.Starred, e.Recent = l.o.Mark(e.Link, e.Account) e.Starred, e.LastUsed, e.Uses = l.o.Mark(e.Link, e.Account)
e.Recent = !e.LastUsed.IsZero()
} }
id := p.ID() + "\x00" + e.Link + "\x00" + e.Error id := p.ID() + "\x00" + e.Link + "\x00" + e.Error
if l.stopped != nil || l.seen[id] { if l.stopped != nil || l.seen[id] {
+10 -2
View File
@@ -229,11 +229,14 @@ func TestListMarks(t *testing.T) {
az := fake.Azure(0) az := fake.Azure(0)
var starredLink string var starredLink string
got, _, _ := run(t, Options{Providers: []provider.Provider{az}, MaxAge: time.Minute, Kinds: []string{"secret"}, got, _, _ := run(t, Options{Providers: []provider.Provider{az}, MaxAge: time.Minute, Kinds: []string{"secret"},
Mark: func(link, account string) (bool, bool) { Mark: func(link, account string) (bool, time.Time, int) {
if starredLink == "" && strings.HasSuffix(link, "/db-password") { if starredLink == "" && strings.HasSuffix(link, "/db-password") {
starredLink = link starredLink = link
} }
return link == starredLink && account == az.ID(), strings.HasSuffix(link, "/api-key") if strings.HasSuffix(link, "/api-key") {
return false, time.Now(), 3
}
return link == starredLink && account == az.ID(), time.Time{}, 0
}}) }})
var starred, recent int var starred, recent int
for _, e := range got { for _, e := range got {
@@ -245,6 +248,11 @@ func TestListMarks(t *testing.T) {
} }
if e.Recent { if e.Recent {
recent++ recent++
if e.Uses != 3 || e.LastUsed.IsZero() {
t.Errorf("%s: %d uses, last %v", e.Link, e.Uses, e.LastUsed)
}
} else if e.Uses != 0 || !e.LastUsed.IsZero() {
t.Errorf("%s not recent, but %d uses, last %v", e.Link, e.Uses, e.LastUsed)
} }
} }
if starred != 1 || recent == 0 { if starred != 1 || recent == 0 {