refactor(providers): cover art and tags share one registry and settings service (#8002)

coverart and tags each carried a line-for-line copy of the provider
registry and the settings service: reconcile at boot, seal and reveal
API keys, bump the sources version when the enabled set or the
registered set changes. A fix to either had to be made twice.

internal/providers now holds Registry, Service (over a small Store
interface) and EnabledAs[T]. Each family keeps its own tables, provider
set and capabilities: a 40-line Store adapter over its sqlc queries and
its capability views (EnabledAlbumProviders, EnabledTrackTagProviders).
Provider, ProviderSettings, TestableProvider, ProviderInfo and
ProviderUpdatePatch stay as the families' names for the shared types.
The service has unit tests over an in-memory store.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 12:57:19 -05:00
co-authored by Claude Opus 5.5
parent 58fb2bbe9d
commit 487e11f59f
8 changed files with 769 additions and 830 deletions
+112
View File
@@ -0,0 +1,112 @@
// Package providers is the frame the pluggable outside sources hang on:
// cover-art sources (internal/coverart) and tag sources (internal/tags).
//
// Each family keeps its own tables, provider set and capabilities. What they
// share lives here: the compiled-in registry, the settings service that
// reconciles it with the family's settings table (enabled flags, sealed API
// keys, the sources version the enricher stamps), and the admin views of
// it. A fix to key sealing or to the version bump is made once.
package providers
import (
"context"
"errors"
"fmt"
"sync"
)
// Provider is the base shape every source implements.
type Provider interface {
// ID is a stable lowercase token: persisted as the row's source, keyed
// in the family's provider_settings table, and shown in the admin UI.
ID() string
// DisplayName is the label on the admin Settings card.
DisplayName() string
// RequiresAPIKey decides whether the admin UI shows a key field. It
// does not gate the enabled toggle: a source with a usable default
// ships working.
RequiresAPIKey() bool
// DefaultEnabled is whether a fresh install starts with the source on.
// Sources that need a key ship off.
DefaultEnabled() bool
// Configure applies the admin's settings. It is called at boot and on
// every admin change, and must be idempotent and safe to call while the
// enricher is reading.
Configure(Settings) error
}
// Settings is what a provider receives on Configure. An empty APIKey is
// the provider's to interpret: a public default key, or "not configured".
type Settings struct {
Enabled bool
APIKey string
}
// Testable providers can answer "is my config working?" without a full
// enrichment cycle, for the admin Test button.
type Testable interface {
Provider
TestConnection(ctx context.Context) error
}
// ErrProviderNotFound is returned for an ID no provider registered.
var ErrProviderNotFound = errors.New("provider not registered")
// ErrNotTestable is returned when testing a provider that isn't Testable.
var ErrNotTestable = errors.New("provider does not support test connection")
// Registry is one family's compiled-in providers, in registration order.
// Providers register from their file's init().
type Registry struct {
family string
mu sync.Mutex
providers []Provider
}
// NewRegistry returns an empty registry; family names it in panics.
func NewRegistry(family string) *Registry { return &Registry{family: family} }
// Register adds p. A duplicate ID panics: providers are compiled in, so a
// duplicate is a build bug.
func (r *Registry) Register(p Provider) {
r.mu.Lock()
defer r.mu.Unlock()
for _, existing := range r.providers {
if existing.ID() == p.ID() {
panic(fmt.Sprintf("%s: duplicate provider ID %q", r.family, p.ID()))
}
}
r.providers = append(r.providers, p)
}
// All returns a snapshot in registration order.
func (r *Registry) All() []Provider {
r.mu.Lock()
defer r.mu.Unlock()
out := make([]Provider, len(r.providers))
copy(out, r.providers)
return out
}
// ByID returns the registered provider with id, or ErrProviderNotFound.
func (r *Registry) ByID(id string) (Provider, error) {
r.mu.Lock()
defer r.mu.Unlock()
for _, p := range r.providers {
if p.ID() == id {
return p, nil
}
}
return nil, ErrProviderNotFound
}
// ResetForTests empties the registry. Tests only.
func (r *Registry) ResetForTests() {
r.mu.Lock()
defer r.mu.Unlock()
r.providers = nil
}