Consolidate selected overlay acquisition
This commit is contained in:
@@ -28,84 +28,51 @@ func validOverlayName(name string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func loadSelectedOverlayDocument(source Source, context *FilesystemContext, main parsedSource, base *composedDocument, sources []SourceInfo, name string, rule *VersionRule, limits importLimits) (*composedDocument, []SourceInfo, []Diagnostic) {
|
||||
assembly := newAssemblyAccumulator(cloneComposedDocument(base))
|
||||
allSources, diagnostics := loadSelectedOverlayInto(source, context, main, assembly, sources, name, rule, limits)
|
||||
if len(diagnostics) != 0 {
|
||||
return nil, allSources, diagnostics
|
||||
}
|
||||
return assembly.current, allSources, nil
|
||||
type selectedOverlayRequest struct {
|
||||
name string
|
||||
filesystem resolvedFilesystemContext
|
||||
protectionRule *VersionRule
|
||||
sequencePolicy sequencePolicy
|
||||
limits importLimits
|
||||
versionState *sourceVersionState
|
||||
}
|
||||
|
||||
func loadSelectedOverlayInto(source Source, context *FilesystemContext, main parsedSource, base *assemblyAccumulator, sources []SourceInfo, name string, rule *VersionRule, limits importLimits) ([]SourceInfo, []Diagnostic) {
|
||||
return loadSelectedOverlayIntoWithVersion(source, context, main, base, sources, name, rule, limits, nil)
|
||||
}
|
||||
|
||||
func loadSelectedOverlayIntoWithVersion(source Source, context *FilesystemContext, main parsedSource, base *assemblyAccumulator, sources []SourceInfo, name string, rule *VersionRule, limits importLimits, versionState *sourceVersionState, policies ...sequencePolicy) ([]SourceInfo, []Diagnostic) {
|
||||
var policy sequencePolicy
|
||||
if len(policies) != 0 {
|
||||
policy = policies[0]
|
||||
}
|
||||
if nativeContext(source, context) {
|
||||
access, basePath, mainPath, diagnostic := nativeFilesystemAccess(source, context)
|
||||
if diagnostic != nil {
|
||||
return cloneSourceInfos(sources), []Diagnostic{*diagnostic}
|
||||
}
|
||||
if diagnostic := validateNativeDirectories(access, basePath, mainPath); diagnostic != nil {
|
||||
return cloneSourceInfos(sources), []Diagnostic{*diagnostic}
|
||||
}
|
||||
if len(policies) == 0 {
|
||||
return loadSelectedOverlayWithAccessAndVersion(access, base, sources, name, rule, limits, versionState)
|
||||
}
|
||||
return loadSelectedOverlayWithAccessAndVersion(access, base, sources, name, rule, limits, versionState, policy)
|
||||
}
|
||||
var rootPath string
|
||||
func resolveOrdinaryOverlayFilesystem(source Source, context *FilesystemContext, main parsedSource, existing *resolvedFilesystemContext) (resolvedFilesystemContext, *Diagnostic) {
|
||||
if source.kind == sourceNone {
|
||||
if context == nil {
|
||||
value := optionDiagnostic(CodeOptionsInvalid, "NoSource overlays require explicit filesystem context")
|
||||
return cloneSourceInfos(sources), []Diagnostic{value}
|
||||
}
|
||||
var err error
|
||||
rootPath, err = canonicalExistingPath(context.Root)
|
||||
if err != nil {
|
||||
value := importDiagnostic(CodeImportInvalid, "configuration root could not be canonicalized", nil)
|
||||
return cloneSourceInfos(sources), []Diagnostic{value}
|
||||
}
|
||||
} else {
|
||||
var diagnostic *Diagnostic
|
||||
rootPath, _, _, diagnostic = importFilesystem(source, context, main)
|
||||
if diagnostic != nil {
|
||||
return cloneSourceInfos(sources), []Diagnostic{*diagnostic}
|
||||
return resolvedFilesystemContext{}, &value
|
||||
}
|
||||
return resolveAbsentFilesystemContext(context)
|
||||
}
|
||||
access, err := openHostAccess(rootPath)
|
||||
if existing != nil {
|
||||
return *existing, nil
|
||||
}
|
||||
return resolveFilesystemContext(source, context, main, nil)
|
||||
}
|
||||
|
||||
func openSelectedOverlayAccess(resolved resolvedFilesystemContext) (*filesystemAccess, *Diagnostic) {
|
||||
access, err := openResolvedFilesystemAccess(resolved)
|
||||
if err != nil {
|
||||
value := globFilesystemDiagnostic(err, "configuration root could not be opened for the selected overlay", Location{})
|
||||
value.Primary = nil
|
||||
return cloneSourceInfos(sources), []Diagnostic{value}
|
||||
return nil, &value
|
||||
}
|
||||
if len(policies) == 0 {
|
||||
return loadSelectedOverlayWithAccessAndVersion(access, base, sources, name, rule, limits, versionState)
|
||||
}
|
||||
return loadSelectedOverlayWithAccessAndVersion(access, base, sources, name, rule, limits, versionState, policy)
|
||||
return access, nil
|
||||
}
|
||||
|
||||
func loadSelectedOverlayWithAccess(access *filesystemAccess, base *assemblyAccumulator, sources []SourceInfo, name string, rule *VersionRule, limits importLimits) ([]SourceInfo, []Diagnostic) {
|
||||
return loadSelectedOverlayWithAccessAndVersion(access, base, sources, name, rule, limits, nil)
|
||||
}
|
||||
|
||||
func loadSelectedOverlayWithAccessAndVersion(access *filesystemAccess, base *assemblyAccumulator, sources []SourceInfo, name string, rule *VersionRule, limits importLimits, versionState *sourceVersionState, policies ...sequencePolicy) ([]SourceInfo, []Diagnostic) {
|
||||
var policy sequencePolicy
|
||||
if len(policies) != 0 {
|
||||
policy = policies[0]
|
||||
}
|
||||
func loadSelectedOverlay(request selectedOverlayRequest, access *filesystemAccess, base *assemblyAccumulator, sources []SourceInfo) ([]SourceInfo, []Diagnostic) {
|
||||
policy := request.sequencePolicy
|
||||
rule := request.protectionRule
|
||||
limits := request.limits
|
||||
versionState := request.versionState
|
||||
closed := false
|
||||
defer func() {
|
||||
if !closed {
|
||||
_ = access.close()
|
||||
}
|
||||
}()
|
||||
overlayPath := access.join(access.rootPath, "overlays/"+name+".yml")
|
||||
overlayPath := access.join(access.rootPath, "overlays/"+request.name+".yml")
|
||||
canonical, err := access.canonical(overlayPath)
|
||||
if err != nil {
|
||||
code := CodeOverlayInvalid
|
||||
@@ -138,7 +105,7 @@ func loadSelectedOverlayWithAccessAndVersion(access *filesystemAccess, base *ass
|
||||
sourceID := "source-" + strconv.Itoa(len(sources)+1)
|
||||
overlayLocation := &Location{SourceID: sourceID}
|
||||
kind := SourceKindFile
|
||||
label := "overlays/" + name + ".yml"
|
||||
label := "overlays/" + request.name + ".yml"
|
||||
if access.native != nil {
|
||||
kind = SourceKindFilesystem
|
||||
label = access.label + "/" + canonical
|
||||
@@ -160,66 +127,44 @@ func loadSelectedOverlayWithAccessAndVersion(access *filesystemAccess, base *ass
|
||||
if base.current.inputNodes+nodeCount > limits.nodes {
|
||||
return sources, []Diagnostic{importDiagnostic(CodeOverlayLimitExceeded, "selected overlay exceeds the aggregate node limit", overlayLocation)}
|
||||
}
|
||||
if diagnostics := overlayDirectiveDiagnostics(overlayRoot, Path{}, len(policies) != 0); len(diagnostics) != 0 {
|
||||
if diagnostics := overlayDirectiveDiagnostics(overlayRoot, Path{}, true); len(diagnostics) != 0 {
|
||||
return sources, diagnostics
|
||||
}
|
||||
var processed sourceVersionProcessingResult
|
||||
var failures []Diagnostic
|
||||
if len(policies) != 0 {
|
||||
if versionState != nil {
|
||||
processed, failures = versionState.processWithDirectiveContext(
|
||||
overlayRoot, sourceVersionContribution, Path{}, sourceID, placementModeMapping, rootDirectiveContextOverlay,
|
||||
)
|
||||
} else {
|
||||
processed = processSourceVersionWithDirectiveContext(
|
||||
overlayRoot, rule, sourceVersionContribution, Path{}, sourceVersionModeDefault, rootDirectiveContextOverlay,
|
||||
)
|
||||
failures = processed.diagnostics
|
||||
}
|
||||
if versionState != nil {
|
||||
processed, failures = versionState.processWithDirectiveContext(
|
||||
overlayRoot, sourceVersionContribution, Path{}, sourceID, placementModeMapping, rootDirectiveContextOverlay,
|
||||
)
|
||||
} else {
|
||||
if versionState != nil && versionState.mode != sourceVersionModeDefault {
|
||||
processed, failures = versionState.process(overlayRoot, sourceVersionContribution, Path{}, sourceID, placementModeMapping)
|
||||
} else {
|
||||
processed = sourceVersionProcessingResult{payload: overlayRoot}
|
||||
}
|
||||
processed = processSourceVersionWithDirectiveContext(
|
||||
overlayRoot, rule, sourceVersionContribution, Path{}, sourceVersionModeDefault, rootDirectiveContextOverlay,
|
||||
)
|
||||
failures = processed.diagnostics
|
||||
}
|
||||
if len(failures) != 0 {
|
||||
return sources, failures
|
||||
}
|
||||
overlayPayload := processed.payload
|
||||
if versionNode := fragmentVersionClaim(overlayPayload, rule); len(policies) == 0 && versionNode != nil {
|
||||
location := publicLocation(versionNode.Location)
|
||||
return sources, []Diagnostic{{
|
||||
Code: CodeOverlayVersion, Severity: SeverityError, Path: rule.Path.clone(),
|
||||
Message: "overlays cannot contribute to or replace the schema-version path", Primary: &location,
|
||||
}}
|
||||
}
|
||||
closeErr := access.close()
|
||||
closed = true
|
||||
if closeErr != nil {
|
||||
return sources, []Diagnostic{importDiagnostic(CodeImportRead, "configuration root could not be closed after reading the selected overlay", nil)}
|
||||
}
|
||||
|
||||
var overlayDiagnostics []Diagnostic
|
||||
if len(policies) != 0 {
|
||||
overlayDiagnostics = base.applyOverlayWithCollectionPolicy(overlayPayload, nil, collectionPolicyScope{}, nil, LayerOverlay, policy, processed.sequences)
|
||||
} else {
|
||||
overlayDiagnostics = base.applyOverlay(overlayPayload, nil, nil, LayerOverlay)
|
||||
}
|
||||
overlayDiagnostics := base.applyOverlayWithCollectionPolicy(overlayPayload, nil, collectionPolicyScope{}, nil, LayerOverlay, policy, processed.sequences)
|
||||
if len(overlayDiagnostics) != 0 {
|
||||
return sources, overlayDiagnostics
|
||||
}
|
||||
if len(policies) != 0 {
|
||||
// The selected overlay retains one application with the actual item
|
||||
// assignments. Raw list indexes cannot identify an upsert's destination.
|
||||
application := base.current.applications[len(base.current.applications)-1]
|
||||
if node := versionClaimFromApplication(application, rule); node != nil {
|
||||
location := publicLocation(node.Location)
|
||||
return sources, []Diagnostic{{
|
||||
Code: CodeOverlayVersion, Severity: SeverityError, Path: rule.Path.clone(),
|
||||
Message: "overlays cannot contribute to or replace the schema-version path", Primary: &location,
|
||||
}}
|
||||
}
|
||||
// The selected overlay retains one application with the actual item
|
||||
// assignments. Raw list indexes cannot identify an upsert's destination.
|
||||
application := base.current.applications[len(base.current.applications)-1]
|
||||
if node := versionClaimFromApplication(application, rule); node != nil {
|
||||
location := publicLocation(node.Location)
|
||||
return sources, []Diagnostic{{
|
||||
Code: CodeOverlayVersion, Severity: SeverityError, Path: rule.Path.clone(),
|
||||
Message: "overlays cannot contribute to or replace the schema-version path", Primary: &location,
|
||||
}}
|
||||
}
|
||||
base.current.inputBytes += int64(len(data))
|
||||
base.current.inputNodes += nodeCount
|
||||
|
||||
Reference in New Issue
Block a user