Files
notarius/internal/framework/pipeline/validation_policy.go

98 lines
3.0 KiB
Go

package pipeline
import (
"fmt"
"strings"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
)
func ParseExecutionMode(value string) (contracts.ExecutionMode, error) {
mode := contracts.ExecutionMode(strings.TrimSpace(value))
if mode != contracts.ModeStrict && mode != contracts.ModePermissive {
return "", fmt.Errorf("mode must be strict or permissive")
}
return mode, nil
}
func ParseCacheMode(value string) (contracts.CacheMode, error) {
mode := contracts.CacheMode(strings.TrimSpace(value))
switch mode {
case contracts.CacheAuto, contracts.CacheBypass, contracts.CacheRefresh:
return mode, nil
}
return "", fmt.Errorf("cache must be auto, bypass, or refresh")
}
func ResolveProducerControls(binding ModuleBinding, profile PipelineProfile, invocationCache *contracts.CacheMode) (contracts.ProducerControls, error) {
result := contracts.ProducerControls{Mode: contracts.ModeStrict, MaxAdditionalTurns: 2, Cache: contracts.CacheAuto}
for _, mode := range []*contracts.ExecutionMode{profile.Mode, binding.Mode} {
if mode != nil {
parsed, err := ParseExecutionMode(string(*mode))
if err != nil {
return contracts.ProducerControls{}, err
}
result.Mode = parsed
}
}
for _, turns := range []*int{profile.MaxAdditionalTurns, binding.MaxAdditionalTurns} {
if turns != nil {
if *turns < 0 {
return contracts.ProducerControls{}, fmt.Errorf("max_additional_turns must not be negative")
}
result.MaxAdditionalTurns = *turns
}
}
for _, cache := range []*contracts.CacheMode{profile.Cache, binding.Cache, invocationCache} {
if cache != nil {
parsed, err := ParseCacheMode(string(*cache))
if err != nil {
return contracts.ProducerControls{}, err
}
result.Cache = parsed
}
}
return result, nil
}
func cloneControl[T any](value *T) *T {
if value == nil {
return nil
}
copy := *value
return &copy
}
func applyEffectiveProducerControls(resolved *ResolvedPipeline, profile PipelineProfile, invocationCache *contracts.CacheMode) error {
apply := func(binding ModuleBinding, scope string) (contracts.ProducerControls, error) {
controls, err := ResolveProducerControls(binding, profile, invocationCache)
if err != nil {
return contracts.ProducerControls{}, fmt.Errorf("pipeline %q %s: %w", resolved.ID, scope, err)
}
return controls, nil
}
var err error
resolved.ChunkControls, err = apply(resolved.Chunk, "chunk")
if err != nil {
return err
}
for stepIndex := range resolved.Steps {
for laneIndex := range resolved.Steps[stepIndex].ArtifactLanes {
lane := &resolved.Steps[stepIndex].ArtifactLanes[laneIndex]
lane.ExtractControls, err = apply(lane.Extract, fmt.Sprintf("lane %q extract", lane.ID))
if err != nil {
return err
}
lane.MergeControls, err = apply(lane.Merge, fmt.Sprintf("lane %q merge", lane.ID))
if err != nil {
return err
}
lane.NormalizeControls, err = apply(lane.Normalize, fmt.Sprintf("lane %q normalize", lane.ID))
if err != nil {
return err
}
}
}
return nil
}