package servicekit import ( "context" "fmt" "log/slog" "slices" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/applystatus" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/secret" ) // RuntimeConfigSource reads the control plane's stored settings. type RuntimeConfigSource interface { RuntimeConfig(ctx context.Context, base config.Config) (config.Config, map[string]int64, error) } // RuntimeConfig layers the control plane's stored settings over base, lets // adjust apply the caller's own live settings, then validates the result and // resolves its provider secrets. The outcome is reported for every layered // kind in reportKinds (every kind when nil), so a refused document keeps the // version still running on the record and a process never reports a kind it // does not re-apply live. func RuntimeConfig(ctx context.Context, cp RuntimeConfigSource, status *applystatus.Reporter, secrets *secret.Registry, log *slog.Logger, base config.Config, adjust func(*config.Config), reportKinds []string) (config.Config, map[string]int64, error) { cfg, versions, err := cp.RuntimeConfig(ctx, base) if err != nil { return config.Config{}, nil, err } if adjust != nil { adjust(&cfg) } report := func(applyErr error) { for kind, version := range versions { if reportKinds != nil && !slices.Contains(reportKinds, kind) { continue } status.Report(ctx, kind, version, applyErr) } } if err := configuration.Validate(&cfg); err != nil { wrapped := fmt.Errorf("validate database settings: %w", err) report(wrapped) return config.Config{}, nil, wrapped } if err := ResolveProviderSecrets(&cfg, secrets, log); err != nil { report(err) return config.Config{}, nil, err } report(nil) return cfg, versions, nil }