diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-27 11:20:23 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-27 11:20:23 +0200 |
| commit | 148c6344a011e0a707016d5825f27b4f3df4dcc4 (patch) | |
| tree | c44e5b976d0ddc90aed6e11afe36b39cec09536b /internal/askcli/task_alias_cache.go | |
| parent | fabc390ff20c217326746140d65c6e7022b66dd8 (diff) | |
refactor: share alias cache lookup
Diffstat (limited to 'internal/askcli/task_alias_cache.go')
| -rw-r--r-- | internal/askcli/task_alias_cache.go | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/internal/askcli/task_alias_cache.go b/internal/askcli/task_alias_cache.go index e8243f0..4a1c5c4 100644 --- a/internal/askcli/task_alias_cache.go +++ b/internal/askcli/task_alias_cache.go @@ -156,15 +156,12 @@ func (c *taskAliasCache) prune(now time.Time) bool { } func (c *taskAliasCache) ensureAlias(uuid string, now time.Time) (string, bool) { - for i := range c.Entries { - if c.Entries[i].UUID != uuid { - continue - } - if c.Entries[i].LastAccessedAt.Equal(now) { - return c.Entries[i].Alias, false + if entry, ok := c.findEntry(func(entry taskAliasCacheEntry) bool { return entry.UUID == uuid }); ok { + if entry.LastAccessedAt.Equal(now) { + return entry.Alias, false } - c.Entries[i].LastAccessedAt = now - return c.Entries[i].Alias, true + entry.LastAccessedAt = now + return entry.Alias, true } alias := encodeTaskAliasID(c.NextID) @@ -179,28 +176,33 @@ func (c *taskAliasCache) ensureAlias(uuid string, now time.Time) (string, bool) return alias, true } -func (c *taskAliasCache) lookupUUIDByAlias(alias string, now time.Time) (string, bool, bool) { +func (c *taskAliasCache) findEntry(match func(taskAliasCacheEntry) bool) (*taskAliasCacheEntry, bool) { for i := range c.Entries { - if c.Entries[i].Alias != alias { - continue + if match(c.Entries[i]) { + return &c.Entries[i], true } - changed := !c.Entries[i].LastAccessedAt.Equal(now) - c.Entries[i].LastAccessedAt = now - return c.Entries[i].UUID, true, changed } - return "", false, false + return nil, false +} + +func (c *taskAliasCache) lookupUUIDByAlias(alias string, now time.Time) (string, bool, bool) { + entry, ok := c.findEntry(func(entry taskAliasCacheEntry) bool { return entry.Alias == alias }) + if !ok { + return "", false, false + } + changed := !entry.LastAccessedAt.Equal(now) + entry.LastAccessedAt = now + return entry.UUID, true, changed } func (c *taskAliasCache) lookupAliasByUUID(uuid string, now time.Time) (string, bool, bool) { - for i := range c.Entries { - if c.Entries[i].UUID != uuid { - continue - } - changed := !c.Entries[i].LastAccessedAt.Equal(now) - c.Entries[i].LastAccessedAt = now - return c.Entries[i].Alias, true, changed + entry, ok := c.findEntry(func(entry taskAliasCacheEntry) bool { return entry.UUID == uuid }) + if !ok { + return "", false, false } - return "", false, false + changed := !entry.LastAccessedAt.Equal(now) + entry.LastAccessedAt = now + return entry.Alias, true, changed } func (c taskAliasCache) save(path string) error { |
