Expose native configuration inspection scopes
This commit is contained in:
@@ -3,10 +3,8 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -35,52 +33,6 @@ func NormalizedConfigurationPath(path string) string {
|
||||
return normalizedProvenancePath(path)
|
||||
}
|
||||
|
||||
// EffectivePipelineSourceRecord identifies one effective logical field and a
|
||||
// safe source that contributed to it.
|
||||
type EffectivePipelineSourceRecord struct {
|
||||
Path string
|
||||
Role string
|
||||
Source string
|
||||
}
|
||||
|
||||
// EmptyImportSelection describes a native optional selection that acquired no source.
|
||||
type EmptyImportSelection struct {
|
||||
Source string
|
||||
Path string
|
||||
Selector string
|
||||
Line int
|
||||
Column int
|
||||
}
|
||||
|
||||
func emptyImportSelections[T any](result *configkit.Result[T]) []EmptyImportSelection {
|
||||
if result == nil {
|
||||
return nil
|
||||
}
|
||||
var selections []EmptyImportSelection
|
||||
for _, source := range result.Sources() {
|
||||
name := source.Path
|
||||
if name == "" {
|
||||
name = source.Label
|
||||
}
|
||||
for _, item := range source.EmptyImportSelections {
|
||||
selections = append(selections, EmptyImportSelection{Source: name, Path: item.Path.String(), Selector: string(item.Selector), Line: item.Location.Line, Column: item.Location.Column})
|
||||
}
|
||||
}
|
||||
return selections
|
||||
}
|
||||
|
||||
// EffectiveEmptyImports reports only optional selections that acquired nothing.
|
||||
func EffectiveEmptyImports(pipeline *PipelineConfig, campaign *CampaignConfig) []EmptyImportSelection {
|
||||
var result []EmptyImportSelection
|
||||
if pipeline != nil && pipeline.resolution != nil {
|
||||
result = append(result, emptyImportSelections(pipeline.resolution.authoring)...)
|
||||
}
|
||||
if campaign != nil && campaign.resolution != nil {
|
||||
result = append(result, emptyImportSelections(campaign.resolution.campaign)...)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// EffectivePipelineValueRecord identifies one normalized, secret-free
|
||||
// effective configuration value. Values use deterministic compact JSON
|
||||
// representations so command output can be compared without raw YAML layout.
|
||||
@@ -92,7 +44,7 @@ type EffectivePipelineValueRecord struct {
|
||||
// EffectivePipelineValues projects a fully resolved pipeline into sorted
|
||||
// logical configuration values. Mappings are flattened, while sequences remain
|
||||
// atomic values. The projection uses the same normalized effective mapping as
|
||||
// config show and therefore excludes loader directives and family declarations.
|
||||
// persistence and informational identity; it is not a native inspection view.
|
||||
func EffectivePipelineValues(cfg *PipelineConfig) ([]EffectivePipelineValueRecord, error) {
|
||||
if cfg == nil {
|
||||
return nil, fmt.Errorf("pipeline config is required")
|
||||
@@ -100,124 +52,6 @@ func EffectivePipelineValues(cfg *PipelineConfig) ([]EffectivePipelineValueRecor
|
||||
return projectionValueRecords(projectEffectivePipeline(cfg))
|
||||
}
|
||||
|
||||
// EffectivePipelineSources projects pipeline ownership after resolution.
|
||||
// It never includes effective values or raw secret
|
||||
// material, only logical paths and source identifiers.
|
||||
func EffectivePipelineSources(cfg *PipelineConfig, party ResolvedParty) ([]EffectivePipelineSourceRecord, error) {
|
||||
if cfg == nil {
|
||||
return nil, fmt.Errorf("pipeline config is required")
|
||||
}
|
||||
values, err := EffectivePipelineValues(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paths := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
paths = append(paths, value.Path)
|
||||
}
|
||||
owners, err := effectivePipelineOwners(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
publishCount := 0
|
||||
if cfg.Publish != nil {
|
||||
publishCount = len(cfg.Publish.Outputs)
|
||||
}
|
||||
records := make([]EffectivePipelineSourceRecord, 0, len(paths)+publishCount)
|
||||
for _, path := range paths {
|
||||
if path == "publish.outputs" {
|
||||
continue
|
||||
}
|
||||
records = appendPipelineOwners(records, path, owners[path], cfg)
|
||||
}
|
||||
records = appendPublishOutputSources(records, cfg, owners)
|
||||
sort.Slice(records, func(left, right int) bool {
|
||||
if records[left].Path != records[right].Path {
|
||||
return records[left].Path < records[right].Path
|
||||
}
|
||||
if records[left].Role != records[right].Role {
|
||||
return records[left].Role < records[right].Role
|
||||
}
|
||||
return records[left].Source < records[right].Source
|
||||
})
|
||||
return records, nil
|
||||
}
|
||||
|
||||
// EffectiveCampaignSources projects selected campaign and party ownership
|
||||
// without retaining or rendering campaign values. Players are derived from the
|
||||
// canonical party source.
|
||||
func EffectiveCampaignSources(campaignPath string, campaign *CampaignConfig, party ResolvedParty) []EffectivePipelineSourceRecord {
|
||||
if campaign == nil {
|
||||
return nil
|
||||
}
|
||||
fields := []string{"campaign_id", "inputs.autocorrect_file", "inputs.glossary_file", "inputs.speakers_file", "inputs.party_file"}
|
||||
if strings.TrimSpace(campaign.Inputs.SpellCatalogFile) != "" {
|
||||
fields = append(fields, "inputs.spell_catalog_file")
|
||||
}
|
||||
var records []EffectivePipelineSourceRecord
|
||||
for _, field := range fields {
|
||||
sources := campaignDefinitionSources(campaign, configkit.Keys(strings.Split(field, ".")...))
|
||||
if len(sources) == 0 {
|
||||
sources = []string{campaignPath}
|
||||
}
|
||||
for _, source := range sources {
|
||||
records = append(records, EffectivePipelineSourceRecord{Path: "campaign." + field, Role: "campaign", Source: normalizedProvenancePath(source)})
|
||||
}
|
||||
}
|
||||
partySource := normalizedProvenancePath(party.Source.Path)
|
||||
records = append(records,
|
||||
EffectivePipelineSourceRecord{Path: "campaign.inputs.party_file", Role: "party", Source: partySource},
|
||||
EffectivePipelineSourceRecord{Path: "derived.players", Role: "party", Source: partySource},
|
||||
)
|
||||
sort.Slice(records, func(left, right int) bool {
|
||||
if records[left].Path != records[right].Path {
|
||||
return records[left].Path < records[right].Path
|
||||
}
|
||||
if records[left].Role != records[right].Role {
|
||||
return records[left].Role < records[right].Role
|
||||
}
|
||||
return records[left].Source < records[right].Source
|
||||
})
|
||||
return records
|
||||
}
|
||||
|
||||
func campaignDefinitionSources(campaign *CampaignConfig, path configkit.Path) []string {
|
||||
if campaign.resolution == nil || campaign.resolution.campaign == nil {
|
||||
return nil
|
||||
}
|
||||
result := campaign.resolution.campaign
|
||||
var ids []string
|
||||
add := func(origin configkit.Origin) {
|
||||
for _, location := range origin.Locations {
|
||||
ids = append(ids, location.SourceID)
|
||||
}
|
||||
for _, step := range origin.Inheritance {
|
||||
for _, location := range step.Locations {
|
||||
ids = append(ids, location.SourceID)
|
||||
}
|
||||
}
|
||||
}
|
||||
if info, ok := result.Metadata().Lookup(path); ok {
|
||||
add(info.Origin)
|
||||
}
|
||||
for _, change := range result.Metadata().History(path) {
|
||||
if change.Origin.Layer == configkit.LayerMain || change.Origin.Layer == configkit.LayerOverlay {
|
||||
add(change.Origin)
|
||||
}
|
||||
}
|
||||
var sources []string
|
||||
for _, id := range nonEmptySortedStrings(ids) {
|
||||
if source, ok := result.Source(id); ok {
|
||||
name := source.Path
|
||||
if name == "" {
|
||||
name = source.Label
|
||||
}
|
||||
sources = append(sources, name)
|
||||
}
|
||||
}
|
||||
return nonEmptySortedStrings(sources)
|
||||
}
|
||||
|
||||
// MarshalEffectivePipeline materializes completed configuration for persistence.
|
||||
// Credentials retain reference names; domain catalogs remain separate.
|
||||
func MarshalEffectivePipeline(cfg *PipelineConfig) ([]byte, error) {
|
||||
@@ -247,222 +81,6 @@ func MarshalEffectivePipeline(cfg *PipelineConfig) ([]byte, error) {
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func effectivePipelineOwners(cfg *PipelineConfig) (map[string][]string, error) {
|
||||
owners := make(map[string][]string)
|
||||
if cfg.ConfigKitResult() == nil {
|
||||
return owners, nil
|
||||
}
|
||||
paths, err := pipelineMetadataPaths(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pathsByID := pipelineSourcePaths(cfg)
|
||||
metadata := cfg.ConfigKitResult().Metadata()
|
||||
for _, value := range paths {
|
||||
info, ok := metadata.Lookup(configkit.Keys(value.segments...))
|
||||
sources := []string{pipelineDefaultOwnershipSource}
|
||||
if ok {
|
||||
sources = ownershipSources(info.Origin, pathsByID)
|
||||
if info.Origin.Layer == configkit.LayerResolution {
|
||||
for _, change := range metadata.History(configkit.Keys(value.segments...)) {
|
||||
if change.Origin.Layer != configkit.LayerResolution {
|
||||
continue
|
||||
}
|
||||
// Native Previous evidence identifies the input immediately before
|
||||
// resolution, including composite and inherited origins.
|
||||
for _, previous := range change.Previous {
|
||||
if previous.Layer == configkit.LayerMain || previous.Layer == configkit.LayerOverlay {
|
||||
sources = append(sources, ownershipSources(previous, pathsByID)...)
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
sources = nonEmptySortedStrings(sources)
|
||||
}
|
||||
}
|
||||
owners[value.record.Path] = sources
|
||||
}
|
||||
if cfg.Scriptorium != nil {
|
||||
for key, artifact := range cfg.Scriptorium.Artifacts {
|
||||
paths := artifactDependencyOwnershipPaths(key, artifact)
|
||||
if sources := sourcesForPipelinePaths(owners, paths); len(sources) != 0 {
|
||||
sort.Strings(sources)
|
||||
owners["scriptorium.artifacts."+key+".depends_on"] = sources
|
||||
}
|
||||
}
|
||||
}
|
||||
return owners, nil
|
||||
}
|
||||
|
||||
func artifactDependencyOwnershipPaths(artifactKey string, artifact ScriptoriumArtifactConfig) []string {
|
||||
var paths []string
|
||||
for _, dependency := range artifact.DependsOn {
|
||||
for _, origin := range artifact.dependencyOrigins[dependency] {
|
||||
prefix := "scriptorium.artifacts." + artifactKey
|
||||
if origin.family != "" {
|
||||
prefix = "scriptorium.artifact_families." + origin.family
|
||||
}
|
||||
switch {
|
||||
case origin.inputName != "":
|
||||
paths = append(paths, prefix+".inputs."+origin.inputName+".source")
|
||||
case origin.family != "":
|
||||
paths = append(paths, prefix+".member_dependencies")
|
||||
default:
|
||||
paths = append(paths, prefix+".depends_on")
|
||||
}
|
||||
}
|
||||
}
|
||||
return paths
|
||||
}
|
||||
|
||||
func generatedArtifactFamilyForPath(path string, families ArtifactFamilyCatalog) (string, bool) {
|
||||
const prefix = "scriptorium.artifacts."
|
||||
if !strings.HasPrefix(path, prefix) {
|
||||
return "", false
|
||||
}
|
||||
memberPath := strings.TrimPrefix(path, prefix)
|
||||
memberKey, _, _ := strings.Cut(memberPath, ".")
|
||||
member, exists := families.Members[memberKey]
|
||||
if !exists {
|
||||
return "", false
|
||||
}
|
||||
_, exists = families.Families[member.Family]
|
||||
return member.Family, exists
|
||||
}
|
||||
|
||||
func appendGeneratedArtifactSources(records []EffectivePipelineSourceRecord, path, familyKey string, families ArtifactFamilyCatalog, owners map[string][]string, partySources func(string) []string) []EffectivePipelineSourceRecord {
|
||||
for _, source := range generatedArtifactSources(path, familyKey, families, owners) {
|
||||
role := "family"
|
||||
if source == pipelineDefaultOwnershipSource {
|
||||
role = "default"
|
||||
}
|
||||
records = append(records, EffectivePipelineSourceRecord{Path: path, Role: role, Source: normalizedProvenanceSource(source)})
|
||||
}
|
||||
memberPath := strings.TrimPrefix(path, "scriptorium.artifacts.")
|
||||
memberKey, _, _ := strings.Cut(memberPath, ".")
|
||||
for _, source := range partySources(families.Members[memberKey].CharacterID) {
|
||||
records = append(records, EffectivePipelineSourceRecord{Path: path, Role: "party", Source: normalizedProvenancePath(source)})
|
||||
}
|
||||
return records
|
||||
}
|
||||
|
||||
func generatedArtifactSources(path, familyKey string, families ArtifactFamilyCatalog, owners map[string][]string) []string {
|
||||
const artifactPrefix = "scriptorium.artifacts."
|
||||
memberPath := strings.TrimPrefix(path, artifactPrefix)
|
||||
_, suffix, _ := strings.Cut(memberPath, ".")
|
||||
familyPrefix := "scriptorium.artifact_families." + familyKey + "."
|
||||
candidates := []string{familyPrefix + suffix}
|
||||
switch {
|
||||
case suffix == "output_path":
|
||||
candidates = []string{familyPrefix + "output_path_pattern"}
|
||||
case suffix == "depends_on":
|
||||
candidates = []string{path, familyPrefix + "depends_on", familyPrefix + "member_dependencies"}
|
||||
case strings.HasPrefix(suffix, "vars."):
|
||||
variable := strings.TrimPrefix(suffix, "vars.")
|
||||
candidates = []string{familyPrefix + "vars." + variable, familyPrefix + "member_vars." + variable}
|
||||
}
|
||||
sources := sourcesForPipelinePaths(owners, candidates)
|
||||
if len(sources) != 0 {
|
||||
return sources
|
||||
}
|
||||
if family, ok := families.Families[familyKey]; ok && len(family.Sources) != 0 {
|
||||
return append([]string(nil), family.Sources...)
|
||||
}
|
||||
return []string{pipelineDefaultOwnershipSource}
|
||||
}
|
||||
|
||||
func sourcesForPipelinePaths(owners map[string][]string, paths []string) []string {
|
||||
seen := make(map[string]struct{})
|
||||
var sources []string
|
||||
for _, path := range paths {
|
||||
for _, source := range owners[path] {
|
||||
if _, exists := seen[source]; exists {
|
||||
continue
|
||||
}
|
||||
seen[source] = struct{}{}
|
||||
sources = append(sources, source)
|
||||
}
|
||||
}
|
||||
return sources
|
||||
}
|
||||
|
||||
func appendPipelineOwners(records []EffectivePipelineSourceRecord, path string, sources []string, cfg *PipelineConfig) []EffectivePipelineSourceRecord {
|
||||
if len(sources) == 0 {
|
||||
sources = []string{pipelineDefaultOwnershipSource}
|
||||
}
|
||||
for _, source := range sources {
|
||||
records = append(records, EffectivePipelineSourceRecord{
|
||||
Path: path, Role: pipelineSourceRole(cfg, source), Source: normalizedProvenanceSource(source),
|
||||
})
|
||||
}
|
||||
return records
|
||||
}
|
||||
|
||||
func appendPublishOutputSources(records []EffectivePipelineSourceRecord, cfg *PipelineConfig, owners map[string][]string) []EffectivePipelineSourceRecord {
|
||||
if cfg == nil || cfg.Publish == nil {
|
||||
return records
|
||||
}
|
||||
for index := range cfg.Publish.Outputs {
|
||||
path := fmt.Sprintf("publish.outputs[%d]", index)
|
||||
records = appendPipelineOwners(records, path, owners["publish.outputs"], cfg)
|
||||
}
|
||||
return records
|
||||
}
|
||||
|
||||
func appendGeneratedPublishSources(records []EffectivePipelineSourceRecord, path, familyKey string, families ArtifactFamilyCatalog, owners map[string][]string, party ResolvedParty) []EffectivePipelineSourceRecord {
|
||||
familyPrefix := "scriptorium.artifact_families." + familyKey + ".publish."
|
||||
sources := sourcesForPipelinePaths(owners, []string{familyPrefix + "enabled", familyPrefix + "required", familyPrefix + "dest_pattern"})
|
||||
if len(sources) == 0 {
|
||||
if family, ok := families.Families[familyKey]; ok && len(family.Sources) != 0 {
|
||||
sources = append([]string(nil), family.Sources...)
|
||||
} else {
|
||||
sources = []string{pipelineDefaultOwnershipSource}
|
||||
}
|
||||
}
|
||||
for _, source := range sources {
|
||||
role := "family"
|
||||
if source == pipelineDefaultOwnershipSource {
|
||||
role = "default"
|
||||
}
|
||||
records = append(records, EffectivePipelineSourceRecord{Path: path, Role: role, Source: normalizedProvenanceSource(source)})
|
||||
}
|
||||
if source := normalizedProvenancePath(party.Source.Path); source != "" {
|
||||
records = append(records, EffectivePipelineSourceRecord{Path: path, Role: "party", Source: source})
|
||||
}
|
||||
return records
|
||||
}
|
||||
|
||||
func pipelineSourceRole(cfg *PipelineConfig, source string) string {
|
||||
if source == pipelineDefaultOwnershipSource {
|
||||
return "default"
|
||||
}
|
||||
if source == "resolution" {
|
||||
return "resolution"
|
||||
}
|
||||
if cfg == nil || cfg.resolution == nil {
|
||||
return "pipeline"
|
||||
}
|
||||
if source == cfg.resolution.rootPath {
|
||||
return "root"
|
||||
}
|
||||
if cfg.resolution.selectedOverlay != nil && source == cfg.resolution.selectedOverlay.overlayPath {
|
||||
return "overlay"
|
||||
}
|
||||
for _, imported := range cfg.resolution.imports {
|
||||
if source == imported {
|
||||
return "import"
|
||||
}
|
||||
}
|
||||
return "pipeline"
|
||||
}
|
||||
|
||||
func normalizedProvenanceSource(source string) string {
|
||||
if source == pipelineDefaultOwnershipSource || source == "resolution" {
|
||||
return source
|
||||
}
|
||||
return normalizedProvenancePath(source)
|
||||
}
|
||||
|
||||
func normalizedProvenancePath(path string) string {
|
||||
if strings.TrimSpace(path) == "" {
|
||||
return ""
|
||||
@@ -473,10 +91,3 @@ func normalizedProvenancePath(path string) string {
|
||||
}
|
||||
return filepath.Clean(abs)
|
||||
}
|
||||
|
||||
func campaignInputSourcePath(campaignPath, configuredPath string) string {
|
||||
if filepath.IsAbs(configuredPath) {
|
||||
return normalizedProvenancePath(configuredPath)
|
||||
}
|
||||
return normalizedProvenancePath(filepath.Join(filepath.Dir(campaignPath), configuredPath))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user