2026-11-20 04:54:12 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
|
|
|
"go.maximumdirect.net/commandkit"
|
2026-11-20 05:06:26 +00:00
|
|
|
configkit "go.maximumdirect.net/configkit"
|
2026-11-20 04:54:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// configurationAcquisition belongs to one parsed selection, including one side
|
|
|
|
|
// of a comparison. It acquires only reached documents and retains native results.
|
|
|
|
|
type configurationAcquisition struct {
|
|
|
|
|
selection configurationSelection
|
|
|
|
|
deps commandDependencies
|
|
|
|
|
pipeline *config.PipelineConfig
|
|
|
|
|
pipelinePath string
|
|
|
|
|
campaign *config.CampaignConfig
|
|
|
|
|
campaignPath string
|
|
|
|
|
party *config.ResolvedParty
|
|
|
|
|
session *acquiredSession
|
|
|
|
|
previousSessionID string
|
|
|
|
|
domain *config.Config
|
2026-11-20 05:15:43 +00:00
|
|
|
catalog *config.ArtifactCatalog
|
2026-11-20 05:06:26 +00:00
|
|
|
pipelineOverrides []configkit.Override
|
2026-11-20 04:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newConfigurationAcquisition(selection configurationSelection, deps commandDependencies) *configurationAcquisition {
|
|
|
|
|
return &configurationAcquisition{selection: selection, deps: normalizeCommandDependencies(deps)}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func selectedString(value commandkit.Supplied[string]) *string {
|
|
|
|
|
if !value.Set {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
result := value.Value
|
|
|
|
|
return &result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func checkConfigurationSelection(selection configurationSelection) error {
|
|
|
|
|
for _, field := range []struct {
|
|
|
|
|
name string
|
|
|
|
|
value commandkit.Supplied[string]
|
|
|
|
|
}{
|
|
|
|
|
{"config", selection.Config}, {"campaign", selection.Campaign},
|
|
|
|
|
{"campaign-file", selection.CampaignFile}, {"session", selection.Session},
|
|
|
|
|
{"session-file", selection.SessionFile}, {"party-file", selection.PartyFile},
|
|
|
|
|
{"secrets-dir", selection.SecretsDir},
|
|
|
|
|
} {
|
|
|
|
|
if field.value.Set && strings.TrimSpace(field.value.Value) == "" {
|
|
|
|
|
return &inputError{message: fmt.Sprintf("--%s requires a non-empty value", field.name)}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *configurationAcquisition) bootstrap(ctx context.Context) error {
|
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := checkConfigurationSelection(a.selection); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if !a.deps.secretsBootstrap.selectionSet {
|
|
|
|
|
a.deps.secretsBootstrap.selection = selectedString(a.selection.SecretsDir)
|
|
|
|
|
a.deps.secretsBootstrap.selectionSet = true
|
|
|
|
|
}
|
|
|
|
|
return a.deps.secretsBootstrap.load(a.deps.loadSecretsDir, a.deps.diagnostics)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *configurationAcquisition) Pipeline(ctx context.Context) (*config.PipelineConfig, error) {
|
|
|
|
|
if err := a.bootstrap(ctx); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if a.pipeline != nil {
|
|
|
|
|
return a.pipeline, nil
|
|
|
|
|
}
|
|
|
|
|
path, cfg, err := loadPipelineConfigWithDependencies(a.selection.Config.Value,
|
2026-11-20 05:06:26 +00:00
|
|
|
config.PipelineLoadOptions{Overlay: selectedString(a.selection.Overlay), Overrides: a.pipelineOverrides},
|
2026-11-20 04:54:12 +00:00
|
|
|
selectedString(a.selection.SecretsDir), a.deps)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
a.pipeline, a.pipelinePath = cfg, path
|
|
|
|
|
return cfg, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *configurationAcquisition) Campaign(ctx context.Context) (*config.CampaignConfig, error) {
|
|
|
|
|
if err := a.bootstrap(ctx); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if a.campaign != nil {
|
|
|
|
|
return a.campaign, nil
|
|
|
|
|
}
|
|
|
|
|
path := a.selection.CampaignFile.Value
|
|
|
|
|
if !a.selection.CampaignFile.Set {
|
|
|
|
|
pipeline, err := a.Pipeline(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
path, err = resolveCampaignConfigPath(pipeline, a.selection.Campaign.Value, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
path, err := filepath.Abs(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
cfg, err := config.LoadCampaign(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if a.selection.Campaign.Set && config.CampaignID(cfg) != a.selection.Campaign.Value {
|
|
|
|
|
return nil, &config.SessionIdentityError{Source: path, Field: "campaign_id", Expected: a.selection.Campaign.Value, Actual: config.CampaignID(cfg)}
|
|
|
|
|
}
|
|
|
|
|
a.campaign, a.campaignPath = cfg, path
|
|
|
|
|
return cfg, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *configurationAcquisition) Party(ctx context.Context) (*config.ResolvedParty, error) {
|
|
|
|
|
if err := a.bootstrap(ctx); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if a.party != nil {
|
|
|
|
|
return a.party, nil
|
|
|
|
|
}
|
|
|
|
|
var campaign *config.CampaignConfig
|
|
|
|
|
if !a.selection.PartyFile.Set {
|
|
|
|
|
var err error
|
|
|
|
|
campaign, err = a.Campaign(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
party, err := config.ResolveParty(a.campaignPath, campaign, selectedString(a.selection.PartyFile))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
a.party = &party
|
|
|
|
|
return a.party, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *configurationAcquisition) Session(ctx context.Context) (*config.SessionConfig, error) {
|
|
|
|
|
if err := a.bootstrap(ctx); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if a.session != nil {
|
|
|
|
|
return a.session.Config, nil
|
|
|
|
|
}
|
|
|
|
|
campaign, err := a.Campaign(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
base := &config.LoadedPipelineCampaign{Campaign: campaign, CampaignPath: a.campaignPath}
|
|
|
|
|
opts := config.SessionLoadOptions{Campaign: campaign, CampaignPath: a.campaignPath, SessionID: a.selection.Session.Value, PreviousSessionID: a.previousSessionID}
|
|
|
|
|
// Local inspection needs no pipeline or derived catalog. Remote discovery
|
|
|
|
|
// acquires storage configuration only after local absence is established.
|
|
|
|
|
if a.selection.SessionFile.Set {
|
|
|
|
|
path, err := filepath.Abs(a.selection.SessionFile.Value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
session, err := config.LoadSessionWithOptions(path, opts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
a.session = &acquiredSession{Config: session, Path: path, Source: config.SessionSource{Source: "session_config", LocalPath: path}}
|
|
|
|
|
return session, nil
|
|
|
|
|
}
|
|
|
|
|
session, path, err := config.DiscoverSession(config.DefaultSessionConfigSearchPaths, opts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if session != nil {
|
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
a.session = &acquiredSession{Config: session, Path: path, Source: config.SessionSource{Source: "session_config", LocalPath: path}}
|
|
|
|
|
return session, nil
|
|
|
|
|
}
|
|
|
|
|
if a.selection.Session.Value == "" {
|
|
|
|
|
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, "remote session loading requires a session identity")
|
|
|
|
|
}
|
|
|
|
|
pipeline, err := a.Pipeline(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
base.Pipeline, base.PipelinePath = pipeline, a.pipelinePath
|
|
|
|
|
loaded, err := loadRemoteSessionConfig(ctx, base, opts, a.deps)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if loaded != nil && loaded.cleanup != nil {
|
|
|
|
|
err = errors.Join(err, loaded.cleanup())
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
a.session = loaded
|
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
|
return nil, errors.Join(err, a.Close())
|
|
|
|
|
}
|
|
|
|
|
return loaded.Config, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *configurationAcquisition) Domain(ctx context.Context) (*config.Config, error) {
|
|
|
|
|
if err := a.bootstrap(ctx); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if a.domain != nil {
|
|
|
|
|
return a.domain, nil
|
|
|
|
|
}
|
|
|
|
|
pipeline, err := a.Pipeline(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
campaign, err := a.Campaign(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
session, err := a.Session(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
party, err := a.Party(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-11-20 05:15:43 +00:00
|
|
|
catalog, err := a.Catalog(ctx)
|
2026-11-20 04:54:12 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-11-20 05:15:43 +00:00
|
|
|
base := config.LoadedPipelineCampaign{PipelinePath: a.pipelinePath, Pipeline: pipeline, CampaignPath: a.campaignPath, Campaign: campaign, Party: *party, ArtifactCatalog: catalog}
|
2026-11-20 04:54:12 +00:00
|
|
|
cfg, err := config.ResolveLoadedPipelineCampaign(base, a.session.Path, session, a.session.Source)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
a.domain = cfg
|
|
|
|
|
return cfg, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *configurationAcquisition) Close() error {
|
|
|
|
|
if a.session == nil || a.session.cleanup == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
cleanup := a.session.cleanup
|
|
|
|
|
a.session.cleanup = nil
|
|
|
|
|
return cleanup()
|
|
|
|
|
}
|
2026-11-20 05:15:43 +00:00
|
|
|
|
|
|
|
|
func (a *configurationAcquisition) Catalog(ctx context.Context) (*config.ArtifactCatalog, error) {
|
|
|
|
|
pipeline, err := a.Pipeline(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if a.catalog != nil {
|
|
|
|
|
return a.catalog, nil
|
|
|
|
|
}
|
|
|
|
|
var party config.ResolvedParty
|
|
|
|
|
if a.party != nil || a.selection.PartyFile.Set || a.selection.Campaign.Set || a.selection.CampaignFile.Set || (pipeline.Scriptorium != nil && len(pipeline.Scriptorium.ArtifactFamilies) != 0) {
|
|
|
|
|
selected, err := a.Party(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
party = *selected
|
|
|
|
|
}
|
|
|
|
|
catalog, err := config.DeriveArtifactCatalog(pipeline, party)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
a.catalog = catalog
|
|
|
|
|
return catalog, nil
|
|
|
|
|
}
|