Load campaign and session imports from their sources
This commit is contained in:
@@ -1,32 +1,123 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
const maxConfigDocumentBytes int64 = 8 << 20
|
||||
|
||||
type documentResolution struct {
|
||||
campaign *configkit.Result[CampaignConfig]
|
||||
session *configkit.Result[SessionConfig]
|
||||
sources map[string]string
|
||||
}
|
||||
|
||||
func documentResource(resolution *configkit.Resolution, path configkit.Path, value *string, invocationDir string, sources map[string]string) {
|
||||
if info, ok := resolution.EntryInfo(path); ok && len(info.Origin.Locations) == 1 {
|
||||
if source, ok := resolution.Source(info.Origin.Locations[0].SourceID); ok {
|
||||
name := source.Path
|
||||
if name == "" {
|
||||
name = source.Label
|
||||
}
|
||||
sources[path.String()] = name
|
||||
}
|
||||
}
|
||||
resolveHostResource(resolution, path, value, invocationDir)
|
||||
}
|
||||
|
||||
func campaignDefinition(invocationDir string, sources map[string]string) configkit.Definition[CampaignConfig] {
|
||||
return configkit.Definition[CampaignConfig]{Resolve: func(cfg *CampaignConfig, resolution *configkit.Resolution) error {
|
||||
documentResource(resolution, configkit.Keys("session_template_file"), &cfg.SessionTemplateFile, invocationDir, sources)
|
||||
inputs := configkit.Keys("inputs")
|
||||
for _, field := range []struct {
|
||||
name string
|
||||
value *string
|
||||
}{
|
||||
{"speakers_file", &cfg.Inputs.SpeakersFile}, {"autocorrect_file", &cfg.Inputs.AutocorrectFile},
|
||||
{"glossary_file", &cfg.Inputs.GlossaryFile}, {"party_file", &cfg.Inputs.PartyFile},
|
||||
{"spell_catalog_file", &cfg.Inputs.SpellCatalogFile},
|
||||
} {
|
||||
documentResource(resolution, inputs.Key(field.name), field.value, invocationDir, sources)
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
}
|
||||
|
||||
func sessionDefinition(invocationDir string, sources map[string]string) configkit.Definition[SessionConfig] {
|
||||
return configkit.Definition[SessionConfig]{Resolve: func(cfg *SessionConfig, resolution *configkit.Resolution) error {
|
||||
inputs := configkit.Keys("inputs")
|
||||
for _, field := range []struct {
|
||||
name string
|
||||
value *string
|
||||
}{
|
||||
{"audio_dir", &cfg.Inputs.AudioDir}, {"speakers_file", &cfg.Inputs.SpeakersFile},
|
||||
{"autocorrect_file", &cfg.Inputs.AutocorrectFile}, {"glossary_file", &cfg.Inputs.GlossaryFile},
|
||||
{"spell_catalog_file", &cfg.Inputs.SpellCatalogFile},
|
||||
} {
|
||||
documentResource(resolution, inputs.Key(field.name), field.value, invocationDir, sources)
|
||||
}
|
||||
for index := range cfg.Inputs.AudioFiles {
|
||||
documentResource(resolution, inputs.Key("audio_files").Index(index), &cfg.Inputs.AudioFiles[index], invocationDir, sources)
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
}
|
||||
|
||||
func loadCampaignWithConfigKit(path string) (*CampaignConfig, error) {
|
||||
result, err := configkit.Load(configkit.File(path), configkit.Options[CampaignConfig]{
|
||||
Definition: configkit.Definition[CampaignConfig]{},
|
||||
})
|
||||
invocationDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sources := make(map[string]string)
|
||||
result, err := configkit.Load(configkit.File(path), configkit.Options[CampaignConfig]{Imports: true, Definition: campaignDefinition(invocationDir, sources)})
|
||||
if err != nil {
|
||||
return nil, describeConfigKitLoadError("campaign", err)
|
||||
}
|
||||
return &result.Config, nil
|
||||
cfg := result.Config
|
||||
cfg.resolution = &documentResolution{campaign: result, sources: sources}
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func loadSessionWithConfigKit(label string, data []byte) (*SessionConfig, error) {
|
||||
if int64(len(data)) > maxConfigDocumentBytes {
|
||||
return nil, fmt.Errorf("load session config: session file %q exceeds %d-byte limit", label, maxConfigDocumentBytes)
|
||||
}
|
||||
result, err := configkit.Load(configkit.Bytes(label, data), configkit.Options[SessionConfig]{
|
||||
Definition: configkit.Definition[SessionConfig]{},
|
||||
})
|
||||
func loadSessionSource(source configkit.Source, label string, opts SessionLoadOptions, imports bool) (*SessionConfig, string, error) {
|
||||
invocationDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, describeConfigKitLoadError("session", err)
|
||||
return nil, "", err
|
||||
}
|
||||
return &result.Config, nil
|
||||
sources := make(map[string]string)
|
||||
result, err := configkit.Load(source, configkit.Options[SessionConfig]{Imports: imports, Definition: sessionDefinition(invocationDir, sources)})
|
||||
if err != nil {
|
||||
return nil, "", describeConfigKitLoadError("session", err)
|
||||
}
|
||||
acquired := result.Sources()
|
||||
if len(acquired) == 0 {
|
||||
return nil, "", nil
|
||||
}
|
||||
selected := acquired[0].Path
|
||||
if selected == "" {
|
||||
selected = label
|
||||
}
|
||||
cfg := result.Config
|
||||
data, err := json.Marshal(cfg)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("inspect session config: %w", err)
|
||||
}
|
||||
if err := rejectSessionTemplatePlaceholders(selected, string(data)); err != nil {
|
||||
return nil, "", fmt.Errorf("load session config: %w", err)
|
||||
}
|
||||
if err := validateSessionIdentity(selected, &cfg, opts); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
cfg.resolution = &documentResolution{session: result, sources: sources}
|
||||
return &cfg, filepath.Clean(selected), nil
|
||||
}
|
||||
|
||||
// DiscoverSession loads the first selected local candidate, or returns nil when
|
||||
// none exists. A selected but invalid source is an error.
|
||||
func DiscoverSession(candidates []string, opts SessionLoadOptions) (*SessionConfig, string, error) {
|
||||
return loadSessionSource(configkit.SearchConfig("narratio", configkit.SearchPaths(candidates...)), "", opts, true)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user