118 lines
3.2 KiB
Go
118 lines
3.2 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type ExecutionMode string
|
|
|
|
const (
|
|
ModeStrict ExecutionMode = "strict"
|
|
ModePermissive ExecutionMode = "permissive"
|
|
)
|
|
|
|
func ParseExecutionMode(value string) (ExecutionMode, error) {
|
|
mode := ExecutionMode(strings.TrimSpace(value))
|
|
if mode != ModeStrict && mode != ModePermissive {
|
|
return "", fmt.Errorf("mode must be strict or permissive")
|
|
}
|
|
return mode, nil
|
|
}
|
|
|
|
type CacheMode string
|
|
|
|
const (
|
|
CacheAuto CacheMode = "auto"
|
|
CacheBypass CacheMode = "bypass"
|
|
CacheRefresh CacheMode = "refresh"
|
|
)
|
|
|
|
func ParseCacheMode(value string) (CacheMode, error) {
|
|
mode := CacheMode(strings.TrimSpace(value))
|
|
switch mode {
|
|
case CacheAuto, CacheBypass, CacheRefresh:
|
|
return mode, nil
|
|
}
|
|
return "", fmt.Errorf("cache must be auto, bypass, or refresh")
|
|
}
|
|
|
|
// ProducerControls contains final operation controls; execution does not inherit settings.
|
|
type ProducerControls struct {
|
|
Mode ExecutionMode `json:"mode"`
|
|
MaxAdditionalTurns int `json:"max_additional_turns"`
|
|
Cache CacheMode `json:"cache"`
|
|
}
|
|
|
|
func ResolveProducerControls(binding ModuleBinding, profile PipelineProfile, invocationCache *CacheMode) (ProducerControls, error) {
|
|
result := ProducerControls{Mode: ModeStrict, MaxAdditionalTurns: 2, Cache: CacheAuto}
|
|
for _, mode := range []*ExecutionMode{profile.Mode, binding.Mode} {
|
|
if mode != nil {
|
|
parsed, err := ParseExecutionMode(string(*mode))
|
|
if err != nil {
|
|
return ProducerControls{}, err
|
|
}
|
|
result.Mode = parsed
|
|
}
|
|
}
|
|
for _, turns := range []*int{profile.MaxAdditionalTurns, binding.MaxAdditionalTurns} {
|
|
if turns != nil {
|
|
if *turns < 0 {
|
|
return ProducerControls{}, fmt.Errorf("max_additional_turns must not be negative")
|
|
}
|
|
result.MaxAdditionalTurns = *turns
|
|
}
|
|
}
|
|
for _, cache := range []*CacheMode{profile.Cache, binding.Cache, invocationCache} {
|
|
if cache != nil {
|
|
parsed, err := ParseCacheMode(string(*cache))
|
|
if err != nil {
|
|
return ProducerControls{}, err
|
|
}
|
|
result.Cache = parsed
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func cloneControl[T any](value *T) *T {
|
|
if value == nil {
|
|
return nil
|
|
}
|
|
copy := *value
|
|
return ©
|
|
}
|
|
|
|
func applyEffectiveProducerControls(resolved *ResolvedPipeline, profile PipelineProfile, invocationCache *CacheMode) error {
|
|
apply := func(binding ModuleBinding, scope string) (ProducerControls, error) {
|
|
controls, err := ResolveProducerControls(binding, profile, invocationCache)
|
|
if err != nil {
|
|
return 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
|
|
}
|