Capture selected host configuration sources
This commit is contained in:
@@ -207,6 +207,8 @@ their existing grammar, enablement, and error precedence.
|
||||
|
||||
## Stage 3 — Share host-main selection and capture
|
||||
|
||||
**Completed.**
|
||||
|
||||
Work in `internal/engine/source.go`, `parse.go`, `discovery.go`, and the initial
|
||||
selection/read portion of `acquisition.go`. A focused `prepared_source.go` may
|
||||
hold the new private payload and preparation helpers.
|
||||
|
||||
@@ -43,22 +43,15 @@ func acquireInput(request inputAcquisitionRequest, policy acquisitionPolicy, sta
|
||||
accounting := state.accounting
|
||||
versionState := state.versionState
|
||||
sourceID := "source-" + strconv.Itoa(accounting.sources+1)
|
||||
selectedSource := source
|
||||
selectedSource, failedSource, selectionDiagnostics := selectInputSource(source, sourceID, request.input.filesystem)
|
||||
if len(selectionDiagnostics) != 0 {
|
||||
if failedSource != nil {
|
||||
failedSource.Destination = destination.clone()
|
||||
failedSource.placementMode = request.input.placementMode
|
||||
}
|
||||
return assembledInput{source: selectedSource, sources: sourceInfos(failedSource), versionState: versionState, accounting: accounting}, selectionDiagnostics
|
||||
}
|
||||
if source.kind == sourceSearch {
|
||||
resolved, failedSource, diagnostics := resolveSearchInput(source, sourceID, defaultDiscoveryDirectoryHelpers(), defaultDiscoveryHostProbe())
|
||||
if len(diagnostics) != 0 {
|
||||
if failedSource != nil {
|
||||
failedSource.Destination = destination.clone()
|
||||
failedSource.placementMode = request.input.placementMode
|
||||
}
|
||||
return assembledInput{source: resolved, sources: sourceInfos(failedSource), versionState: versionState, accounting: accounting}, diagnostics
|
||||
}
|
||||
selectedSource = resolved
|
||||
if request.input.filesystem != nil {
|
||||
if diagnostics := filesystemOptionDiagnostics(selectedSource, request.input.filesystem); len(diagnostics) != 0 {
|
||||
return assembledInput{source: selectedSource, versionState: versionState, accounting: accounting}, diagnostics
|
||||
}
|
||||
}
|
||||
if selectedSource.kind == sourceNone && activeOverlay(request.ordinaryOverlay) && request.input.filesystem == nil {
|
||||
return assembledInput{source: selectedSource, versionState: versionState, accounting: accounting}, []Diagnostic{optionDiagnostic(CodeOptionsInvalid, "NoSource overlays require explicit filesystem context")}
|
||||
}
|
||||
|
||||
73
internal/engine/prepared_source.go
Normal file
73
internal/engine/prepared_source.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package engine
|
||||
|
||||
import "go.maximumdirect.net/configkit/internal/document"
|
||||
|
||||
// capturedHostSource contains immutable, passive facts about one selected file.
|
||||
type capturedHostSource struct {
|
||||
document *document.Node
|
||||
bytes int64
|
||||
mainPath string
|
||||
basePath string
|
||||
rootPath string
|
||||
}
|
||||
|
||||
// selectInputSource shares discovery and post-selection option checks with
|
||||
// preparation and ordinary acquisition.
|
||||
func selectInputSource(source Source, sourceID string, context *FilesystemContext) (Source, *SourceInfo, []Diagnostic) {
|
||||
if source.kind != sourceSearch {
|
||||
return source, nil, nil
|
||||
}
|
||||
selected, failed, diagnostics := resolveSearchInput(source, sourceID, defaultDiscoveryDirectoryHelpers(), defaultDiscoveryHostProbe())
|
||||
if len(diagnostics) != 0 {
|
||||
return selected, failed, diagnostics
|
||||
}
|
||||
if context != nil {
|
||||
if diagnostics := filesystemOptionDiagnostics(selected, context); len(diagnostics) != 0 {
|
||||
return selected, nil, diagnostics
|
||||
}
|
||||
}
|
||||
return selected, nil, nil
|
||||
}
|
||||
|
||||
// captureHostMain snapshots a selected host document and canonical context.
|
||||
// Diagnostics retain the same source evidence as ordinary acquisition.
|
||||
func captureHostMain(source Source, context *FilesystemContext) (Source, []SourceInfo, []Diagnostic) {
|
||||
const sourceID = "source-1"
|
||||
if source.kind != sourceFile && source.kind != sourceSearch && source.kind != sourceNone {
|
||||
return Source{}, nil, []Diagnostic{optionDiagnostic(CodeOptionsInvalid, "main preparation requires a host file, search, or absent source")}
|
||||
}
|
||||
if context != nil {
|
||||
if context.FS != nil {
|
||||
return Source{}, nil, []Diagnostic{optionDiagnostic(CodeOptionsInvalid, "main preparation requires a host filesystem context")}
|
||||
}
|
||||
if diagnostics := filesystemOptionDiagnostics(source, context); len(diagnostics) != 0 {
|
||||
return Source{}, nil, diagnostics
|
||||
}
|
||||
}
|
||||
selected, failed, diagnostics := selectInputSource(source, sourceID, context)
|
||||
if len(diagnostics) != 0 {
|
||||
return Source{}, sourceInfos(failed), diagnostics
|
||||
}
|
||||
if selected.kind == sourceNone {
|
||||
if context != nil {
|
||||
_, diagnostic := validateFilesystemContext(selected, context, parsedSource{}, nil)
|
||||
if diagnostic != nil {
|
||||
return Source{}, nil, []Diagnostic{*diagnostic}
|
||||
}
|
||||
}
|
||||
return selected, nil, nil
|
||||
}
|
||||
parsed, diagnostics := parseSource(selected, sourceID, defaultParseLimits())
|
||||
if len(diagnostics) != 0 {
|
||||
return Source{}, sourceInfos(parsed.source), diagnostics
|
||||
}
|
||||
resolved, diagnostic := validateFilesystemContext(selected, context, parsed, nil)
|
||||
if diagnostic != nil {
|
||||
return Source{}, sourceInfos(parsed.source), []Diagnostic{*diagnostic}
|
||||
}
|
||||
selected.captured = &capturedHostSource{
|
||||
document: parsed.document, bytes: parsed.bytes, mainPath: resolved.mainPath,
|
||||
basePath: resolved.basePath, rootPath: resolved.rootPath,
|
||||
}
|
||||
return selected, sourceInfos(parsed.source), nil
|
||||
}
|
||||
85
internal/engine/prepared_source_test.go
Normal file
85
internal/engine/prepared_source_test.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCapturedHostSourceRetainsSelectedDocument(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
first := filepath.Join(dir, "first.yml")
|
||||
second := filepath.Join(dir, "second.yml")
|
||||
if err := os.WriteFile(second, []byte("config: {secrets_dir: secrets}\nvalue: original\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
search := SearchConfig("app", Paths(first, second))
|
||||
prepared, sources, diagnostics := captureHostMain(search, nil)
|
||||
if len(diagnostics) != 0 || len(sources) != 1 || prepared.captured == nil || prepared.captured.mainPath != canonicalTestPath(t, second) || prepared.captured.basePath != canonicalTestPath(t, dir) || prepared.captured.rootPath != canonicalTestPath(t, dir) {
|
||||
t.Fatalf("prepared = %#v, sources = %#v, diagnostics = %#v", prepared, sources, diagnostics)
|
||||
}
|
||||
wider, _, diagnostics := captureHostMain(File(second), &FilesystemContext{Root: filepath.Dir(dir)})
|
||||
if len(diagnostics) != 0 || wider.captured == nil || wider.captured.rootPath != canonicalTestPath(t, filepath.Dir(dir)) {
|
||||
t.Fatalf("wider context = %#v, diagnostics = %#v", wider, diagnostics)
|
||||
}
|
||||
if err := os.WriteFile(first, []byte("value: newer-candidate\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(second, []byte("value: changed\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, source := range []Source{prepared, prepared} {
|
||||
parsed, diagnostics := parseSource(source, "another-source", defaultParseLimits())
|
||||
if len(diagnostics) != 0 || parsed.source.Path != canonicalTestPath(t, second) || len(parsed.document.Mapping) != 2 || parsed.document.Mapping[1].Value.Text != "original" || parsed.document.Mapping[0].Value.Location.SourceID != "another-source" {
|
||||
t.Fatalf("parsed = %#v, diagnostics = %#v", parsed, diagnostics)
|
||||
}
|
||||
parsed.document.Mapping[1].Value.Text = "mutated"
|
||||
}
|
||||
ordinary, diagnostics := parseSource(File(second), "ordinary", defaultParseLimits())
|
||||
if len(diagnostics) != 0 || ordinary.document.Mapping[0].Value.Text != "changed" {
|
||||
t.Fatalf("ordinary file after modification = %#v, diagnostics = %#v", ordinary, diagnostics)
|
||||
}
|
||||
if err := os.Remove(second); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
parsed, diagnostics := parseSource(prepared, "after-removal", defaultParseLimits())
|
||||
if len(diagnostics) != 0 || parsed.document.Mapping[1].Value.Text != "original" {
|
||||
t.Fatalf("after removal: parsed = %#v, diagnostics = %#v", parsed, diagnostics)
|
||||
}
|
||||
selected, _, diagnostics := selectInputSource(search, "source-1", nil)
|
||||
if len(diagnostics) != 0 || selected.path != first {
|
||||
t.Fatalf("fresh discovery = %#v, diagnostics = %#v", selected, diagnostics)
|
||||
}
|
||||
parsed, diagnostics = parseSource(File(first), "fresh", defaultParseLimits())
|
||||
if len(diagnostics) != 0 || parsed.document.Mapping[0].Value.Text != "newer-candidate" {
|
||||
t.Fatalf("fresh file = %#v, diagnostics = %#v", parsed, diagnostics)
|
||||
}
|
||||
limits := defaultParseLimits()
|
||||
limits.source.remaining = prepared.captured.bytes - 1
|
||||
if _, diagnostics := parseSource(prepared, "limited", limits); len(diagnostics) != 1 || diagnostics[0].Code != CodeSourceLimitExceeded {
|
||||
t.Fatalf("captured limit diagnostics = %#v", diagnostics)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCaptureHostSourceReportsSelectionAndParsingFailures(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
invalid := filepath.Join(dir, "invalid.yml")
|
||||
if err := os.WriteFile(invalid, []byte("not: [valid\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, source := range []Source{File(invalid), File(filepath.Join(dir, "missing.yml"))} {
|
||||
prepared, sources, diagnostics := captureHostMain(source, nil)
|
||||
if prepared.kind != sourceInvalid || len(sources) != 1 || len(diagnostics) == 0 || diagnostics[0].Primary == nil {
|
||||
t.Fatalf("prepared = %#v, sources = %#v, diagnostics = %#v", prepared, sources, diagnostics)
|
||||
}
|
||||
}
|
||||
missing := Paths(filepath.Join(dir, "missing.yml"))
|
||||
prepared, sources, diagnostics := captureHostMain(SearchConfig("app", missing), nil)
|
||||
if prepared.kind != sourceNone || len(sources) != 0 || len(diagnostics) != 0 {
|
||||
t.Fatalf("optional search: prepared = %#v, sources = %#v, diagnostics = %#v", prepared, sources, diagnostics)
|
||||
}
|
||||
prepared, _, diagnostics = captureHostMain(SearchConfig("app", missing, SearchOptions{Required: true}), nil)
|
||||
if prepared.kind != sourceInvalid || len(diagnostics) == 0 {
|
||||
t.Fatalf("required search: prepared = %#v, diagnostics = %#v", prepared, diagnostics)
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,7 @@ type Source struct {
|
||||
searchApp string
|
||||
searchRequired bool
|
||||
searchIssues []string
|
||||
captured *capturedHostSource
|
||||
}
|
||||
|
||||
type sourceKind uint8
|
||||
@@ -227,6 +228,15 @@ func readSource(source Source, sourceID string, limits sourceLimits) (sourceRead
|
||||
var input io.Reader
|
||||
switch source.kind {
|
||||
case sourceFile:
|
||||
if source.captured != nil {
|
||||
info.Path = source.captured.mainPath
|
||||
if source.captured.bytes > limit {
|
||||
return sourceRead{info: info}, []Diagnostic{sourceDiagnostic(CodeSourceLimitExceeded, sourceID, "source exceeds the configured byte limit")}
|
||||
}
|
||||
owned := document.Clone(source.captured.document)
|
||||
setDocumentSourceID(owned, sourceID)
|
||||
return sourceRead{document: owned, bytes: source.captured.bytes, info: info}, nil
|
||||
}
|
||||
absolute, err := filepath.Abs(source.path)
|
||||
if err != nil {
|
||||
return sourceRead{info: info}, []Diagnostic{sourceDiagnostic(CodeSourceRead, sourceID, "source path could not be resolved")}
|
||||
|
||||
Reference in New Issue
Block a user