refactor: load settings once per run and reuse shared helpers

The application caches the settings snapshot and reloads it only after
an update through the store, so output and prompt helpers no longer
read and parse config.yml and hosts.yml several times per command.
canPrompt and chooser replace the inline prompt-policy checks.

Checks are tallied by one services.Tally, terminal.Plural replaces the
two app copies, refKind.article/noun replace the comment group's own
words, list commands use servicePage, and web URLs are built by
repositoryURL and webURL in browser.go.

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-12 10:57:39 +02:00
1 parent 9ac5b79e9e
commit 29f62da006
42 files changed
+157 -204

No files matched your search

+23 -1
View File
@@ -14,6 +14,7 @@ import (
"regexp"
"slices"
"strings"
"sync"
"time"
"unicode"
@@ -71,6 +72,27 @@ type options struct {
type application struct {
deps Dependencies
options options
// snapshot caches the settings for one run; generation is the store's generation it was loaded at.
mu sync.Mutex
snapshot *settings.Snapshot
generation uint64
}
// settings returns the settings, loaded once per run and again after tea changed them.
func (a *application) settings() (settings.Snapshot, error) {
a.mu.Lock()
defer a.mu.Unlock()
generation := a.deps.Settings.Generation()
if a.snapshot != nil && a.generation == generation {
return *a.snapshot, nil
}
snapshot, err := a.deps.Settings.Load()
if err != nil {
return snapshot, err
}
a.snapshot, a.generation = &snapshot, generation
return snapshot, nil
}
// NewRoot builds fresh commands. Construction performs no disk, network, or Git IO.
@@ -144,7 +166,7 @@ func NewRoot(deps Dependencies) *cobra.Command {
if offline(cmd) {
return nil
}
snapshot, err := a.deps.Settings.Load()
snapshot, err := a.settings()
if err != nil {
return err
}