432 lines
14 KiB
Go
432 lines
14 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
|
)
|
|
|
|
type cliReferenceRequest struct {
|
|
Selector cliReferenceSelector
|
|
Source string
|
|
}
|
|
|
|
type cliReferenceUnbindRequest struct {
|
|
Selector cliReferenceSelector
|
|
}
|
|
|
|
type cliReferenceSelector struct {
|
|
Scope cliReferenceSelectorScope
|
|
LaneID string
|
|
Stage pipeline.ModuleStage
|
|
SlotName string
|
|
}
|
|
|
|
type cliReferenceSelectorScope uint8
|
|
|
|
const (
|
|
cliReferenceScopePipeline cliReferenceSelectorScope = iota
|
|
cliReferenceScopeLane
|
|
cliReferenceScopeChunk
|
|
cliReferenceScopeBinding
|
|
)
|
|
|
|
func parseReferenceFlags(values []string) ([]cliReferenceRequest, error) {
|
|
if len(values) == 0 {
|
|
return nil, nil
|
|
}
|
|
requests := make([]cliReferenceRequest, 0, len(values))
|
|
for _, raw := range values {
|
|
name, source, ok := strings.Cut(raw, "=")
|
|
if !ok {
|
|
return nil, fmt.Errorf("--reference must use slot=path, lane.slot=path, or lane.stage.slot=path")
|
|
}
|
|
if strings.TrimSpace(source) == "" {
|
|
return nil, fmt.Errorf("--reference path must not be empty; use --without-reference to unbind")
|
|
}
|
|
selector, err := parseReferenceSelector(name, "--reference")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
requests = append(requests, cliReferenceRequest{
|
|
Selector: selector,
|
|
Source: strings.TrimSpace(source),
|
|
})
|
|
}
|
|
return requests, nil
|
|
}
|
|
|
|
func parseReferenceUnbindFlags(values []string) ([]cliReferenceUnbindRequest, error) {
|
|
if len(values) == 0 {
|
|
return nil, nil
|
|
}
|
|
requests := make([]cliReferenceUnbindRequest, 0, len(values))
|
|
for _, raw := range values {
|
|
if strings.Contains(raw, "=") {
|
|
return nil, fmt.Errorf("--without-reference must use a reference selector without =path")
|
|
}
|
|
selector, err := parseReferenceSelector(raw, "--without-reference")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
requests = append(requests, cliReferenceUnbindRequest{
|
|
Selector: selector,
|
|
})
|
|
}
|
|
return requests, nil
|
|
}
|
|
|
|
func parseReferenceSelector(raw string, flagName string) (cliReferenceSelector, error) {
|
|
selector := strings.TrimSpace(raw)
|
|
if selector == "" {
|
|
return cliReferenceSelector{}, fmt.Errorf("%s reference slot must not be empty", flagName)
|
|
}
|
|
parts := strings.Split(selector, ".")
|
|
for _, part := range parts {
|
|
if strings.TrimSpace(part) == "" {
|
|
return cliReferenceSelector{}, fmt.Errorf("%s must use non-empty reference selector values", flagName)
|
|
}
|
|
}
|
|
switch len(parts) {
|
|
case 1:
|
|
return cliReferenceSelector{Scope: cliReferenceScopePipeline, SlotName: strings.TrimSpace(parts[0])}, nil
|
|
case 2:
|
|
first := strings.TrimSpace(parts[0])
|
|
slotName := strings.TrimSpace(parts[1])
|
|
if first == string(pipeline.StageChunk) {
|
|
return cliReferenceSelector{Scope: cliReferenceScopeChunk, Stage: pipeline.StageChunk, SlotName: slotName}, nil
|
|
}
|
|
return cliReferenceSelector{Scope: cliReferenceScopeLane, LaneID: first, SlotName: slotName}, nil
|
|
case 3:
|
|
laneID := strings.TrimSpace(parts[0])
|
|
stage := pipeline.ModuleStage(strings.TrimSpace(parts[1]))
|
|
slotName := strings.TrimSpace(parts[2])
|
|
if stage != pipeline.StageExtract && stage != pipeline.StageMerge && stage != pipeline.StageNormalize {
|
|
return cliReferenceSelector{}, fmt.Errorf("%s lane-qualified selector must use lane.extract.slot, lane.merge.slot, or lane.normalize.slot", flagName)
|
|
}
|
|
return cliReferenceSelector{Scope: cliReferenceScopeBinding, LaneID: laneID, Stage: stage, SlotName: slotName}, nil
|
|
default:
|
|
return cliReferenceSelector{}, fmt.Errorf("%s must use slot, chunk.slot, lane.slot, lane.extract.slot, lane.merge.slot, or lane.normalize.slot", flagName)
|
|
}
|
|
}
|
|
|
|
func resolveCLIReferenceRequests(
|
|
cfg config.Config,
|
|
pipelineID string,
|
|
only []string,
|
|
catalog pipeline.ModuleCatalog,
|
|
referenceRequests []cliReferenceRequest,
|
|
unbindRequests []cliReferenceUnbindRequest,
|
|
) ([]pipeline.ReferenceBinding, []pipeline.ReferenceUnbind, error) {
|
|
if len(referenceRequests) == 0 && len(unbindRequests) == 0 {
|
|
return nil, nil, nil
|
|
}
|
|
|
|
targets, err := selectedReferenceTargets(cfg, pipelineID, only, catalog)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
// Broad CLI selectors are only presentation syntax. Collapse them into one
|
|
// highest-specificity action per concrete framework target before pipeline
|
|
// resolution so the generic reference contract stays stage-and-lane exact.
|
|
actions := make(map[cliReferenceTargetKey]resolvedCLIReferenceAction)
|
|
for _, request := range referenceRequests {
|
|
matches, err := resolveCLIReferenceTargets(targets, request.Selector)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
for _, target := range matches {
|
|
candidate := resolvedCLIReferenceAction{
|
|
kind: cliReferenceActionBind,
|
|
selector: request.Selector,
|
|
target: target,
|
|
slotName: request.Selector.SlotName,
|
|
source: request.Source,
|
|
specificity: request.Selector.specificity(),
|
|
}
|
|
if err := mergeCLIReferenceAction(actions, candidate); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
}
|
|
for _, request := range unbindRequests {
|
|
matches, err := resolveCLIReferenceTargets(targets, request.Selector)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
for _, target := range matches {
|
|
candidate := resolvedCLIReferenceAction{
|
|
kind: cliReferenceActionUnbind,
|
|
selector: request.Selector,
|
|
target: target,
|
|
slotName: request.Selector.SlotName,
|
|
specificity: request.Selector.specificity(),
|
|
}
|
|
if err := mergeCLIReferenceAction(actions, candidate); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
resolved := make([]resolvedCLIReferenceAction, 0, len(actions))
|
|
for _, action := range actions {
|
|
resolved = append(resolved, action)
|
|
}
|
|
sort.Slice(resolved, func(i, j int) bool {
|
|
left, right := resolved[i], resolved[j]
|
|
if left.target.laneID != right.target.laneID {
|
|
return left.target.laneID < right.target.laneID
|
|
}
|
|
if left.target.stage != right.target.stage {
|
|
return referenceStageOrder(left.target.stage) < referenceStageOrder(right.target.stage)
|
|
}
|
|
return left.slotName < right.slotName
|
|
})
|
|
|
|
overrides := make([]pipeline.ReferenceBinding, 0, len(resolved))
|
|
unbinds := make([]pipeline.ReferenceUnbind, 0, len(resolved))
|
|
for _, action := range resolved {
|
|
switch action.kind {
|
|
case cliReferenceActionBind:
|
|
overrides = append(overrides, pipeline.ReferenceBinding{
|
|
Stage: action.target.stage,
|
|
LaneID: action.target.laneID,
|
|
SlotName: action.slotName,
|
|
Source: action.source,
|
|
BindingSource: contracts.ReferenceBindingSourceCLI,
|
|
})
|
|
case cliReferenceActionUnbind:
|
|
unbinds = append(unbinds, pipeline.ReferenceUnbind{
|
|
Stage: action.target.stage,
|
|
LaneID: action.target.laneID,
|
|
SlotName: action.slotName,
|
|
})
|
|
}
|
|
}
|
|
return overrides, unbinds, nil
|
|
}
|
|
|
|
type cliReferenceActionKind uint8
|
|
|
|
const (
|
|
cliReferenceActionBind cliReferenceActionKind = iota
|
|
cliReferenceActionUnbind
|
|
)
|
|
|
|
type cliReferenceTargetKey struct {
|
|
stage pipeline.ModuleStage
|
|
laneID string
|
|
slotName string
|
|
}
|
|
|
|
type resolvedCLIReferenceAction struct {
|
|
kind cliReferenceActionKind
|
|
selector cliReferenceSelector
|
|
target selectedReferenceTarget
|
|
slotName string
|
|
source string
|
|
specificity int
|
|
}
|
|
|
|
func mergeCLIReferenceAction(actions map[cliReferenceTargetKey]resolvedCLIReferenceAction, candidate resolvedCLIReferenceAction) error {
|
|
key := cliReferenceTargetKey{stage: candidate.target.stage, laneID: candidate.target.laneID, slotName: candidate.slotName}
|
|
current, ok := actions[key]
|
|
if !ok || candidate.specificity > current.specificity {
|
|
actions[key] = candidate
|
|
return nil
|
|
}
|
|
if candidate.specificity < current.specificity {
|
|
return nil
|
|
}
|
|
if candidate.kind != current.kind {
|
|
return fmt.Errorf("reference target %q slot %q is both bound by %q and unbound by %q at the same specificity", targetLabel(candidate.target), candidate.slotName, current.selector.String(), candidate.selector.String())
|
|
}
|
|
actions[key] = candidate
|
|
return nil
|
|
}
|
|
|
|
func (selector cliReferenceSelector) specificity() int {
|
|
switch selector.Scope {
|
|
case cliReferenceScopePipeline:
|
|
return 0
|
|
case cliReferenceScopeLane:
|
|
return 1
|
|
case cliReferenceScopeChunk, cliReferenceScopeBinding:
|
|
return 2
|
|
default:
|
|
return -1
|
|
}
|
|
}
|
|
|
|
func (selector cliReferenceSelector) String() string {
|
|
switch selector.Scope {
|
|
case cliReferenceScopePipeline:
|
|
return selector.SlotName
|
|
case cliReferenceScopeLane:
|
|
return selector.LaneID + "." + selector.SlotName
|
|
case cliReferenceScopeChunk:
|
|
return "chunk." + selector.SlotName
|
|
case cliReferenceScopeBinding:
|
|
return selector.LaneID + "." + string(selector.Stage) + "." + selector.SlotName
|
|
default:
|
|
return selector.SlotName
|
|
}
|
|
}
|
|
|
|
type selectedReferenceTarget struct {
|
|
laneID string
|
|
stage pipeline.ModuleStage
|
|
module string
|
|
slots map[string]struct{}
|
|
}
|
|
|
|
func selectedReferenceTargets(cfg config.Config, pipelineID string, only []string, catalog pipeline.ModuleCatalog) ([]selectedReferenceTarget, error) {
|
|
profile, ok := lookupCLIReferencePipeline(cfg.Pipelines, pipelineID)
|
|
if !ok {
|
|
return nil, fmt.Errorf("pipeline %q is not configured", strings.TrimSpace(pipelineID))
|
|
}
|
|
profile.ID = strings.TrimSpace(pipelineID)
|
|
layout, err := pipeline.LayoutPipeline(profile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
inventory, err := pipeline.SelectPipelineTargets(layout, only, catalog)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
targets := []selectedReferenceTarget{{stage: pipeline.StageChunk, module: inventory.Chunk.Module, slots: referenceSlotSet(inventory.ChunkSpec.ReferenceSlots)}}
|
|
var lanes []pipeline.PipelineTargetLane
|
|
for _, step := range inventory.Steps {
|
|
for _, lane := range step.Lanes {
|
|
if lane.Selected {
|
|
lanes = append(lanes, lane)
|
|
}
|
|
}
|
|
}
|
|
sort.Slice(lanes, func(i, j int) bool { return lanes[i].ID < lanes[j].ID })
|
|
for _, lane := range lanes {
|
|
targets = append(targets,
|
|
selectedReferenceTarget{laneID: lane.ID, stage: pipeline.StageExtract, module: lane.Extract.Module, slots: referenceSlotSet(lane.ExtractSpec.ReferenceSlots)},
|
|
selectedReferenceTarget{laneID: lane.ID, stage: pipeline.StageMerge, module: lane.Merge.Module, slots: referenceSlotSet(lane.MergeSpec.ReferenceSlots)},
|
|
selectedReferenceTarget{laneID: lane.ID, stage: pipeline.StageNormalize, module: lane.Normalize.Module, slots: referenceSlotSet(lane.NormalizeSpec.ReferenceSlots)},
|
|
)
|
|
}
|
|
return targets, nil
|
|
}
|
|
|
|
func lookupCLIReferencePipeline(profiles map[string]pipeline.PipelineProfile, pipelineID string) (pipeline.PipelineProfile, bool) {
|
|
pipelineID = strings.TrimSpace(pipelineID)
|
|
for rawID, profile := range profiles {
|
|
if strings.TrimSpace(rawID) == pipelineID {
|
|
return profile, true
|
|
}
|
|
}
|
|
return pipeline.PipelineProfile{}, false
|
|
}
|
|
|
|
func referenceSlotSet(slots []contracts.ReferenceSlot) map[string]struct{} {
|
|
slotSet := make(map[string]struct{}, len(slots))
|
|
for _, slot := range slots {
|
|
slotSet[slot.Name] = struct{}{}
|
|
}
|
|
return slotSet
|
|
}
|
|
|
|
func resolveCLIReferenceTargets(targets []selectedReferenceTarget, selector cliReferenceSelector) ([]selectedReferenceTarget, error) {
|
|
slotName := strings.TrimSpace(selector.SlotName)
|
|
if slotName == "" {
|
|
return nil, fmt.Errorf("reference slot must not be empty")
|
|
}
|
|
switch selector.Scope {
|
|
case cliReferenceScopePipeline:
|
|
matches := make([]selectedReferenceTarget, 0, len(targets))
|
|
for _, target := range targets {
|
|
if _, ok := target.slots[slotName]; ok {
|
|
matches = append(matches, target)
|
|
}
|
|
}
|
|
if len(matches) == 0 {
|
|
return nil, fmt.Errorf("reference slot %q is not declared by any selected target", slotName)
|
|
}
|
|
return matches, nil
|
|
case cliReferenceScopeChunk:
|
|
for _, target := range targets {
|
|
if target.stage != pipeline.StageChunk {
|
|
continue
|
|
}
|
|
if _, ok := target.slots[slotName]; !ok {
|
|
return nil, fmt.Errorf("reference slot %q is not declared by chunk module %q", slotName, target.module)
|
|
}
|
|
return []selectedReferenceTarget{target}, nil
|
|
}
|
|
return nil, fmt.Errorf("reference chunk target is not selected")
|
|
case cliReferenceScopeLane:
|
|
laneSelected := false
|
|
matches := make([]selectedReferenceTarget, 0, 3)
|
|
for _, target := range targets {
|
|
if target.laneID != selector.LaneID {
|
|
continue
|
|
}
|
|
laneSelected = true
|
|
if _, ok := target.slots[slotName]; ok {
|
|
matches = append(matches, target)
|
|
}
|
|
}
|
|
if !laneSelected {
|
|
return nil, fmt.Errorf("reference lane %q is not selected", selector.LaneID)
|
|
}
|
|
if len(matches) == 0 {
|
|
return nil, fmt.Errorf("reference slot %q is not declared by selected lane %q", slotName, selector.LaneID)
|
|
}
|
|
return matches, nil
|
|
case cliReferenceScopeBinding:
|
|
laneSelected := false
|
|
for _, target := range targets {
|
|
if target.laneID != selector.LaneID {
|
|
continue
|
|
}
|
|
laneSelected = true
|
|
if target.stage != selector.Stage {
|
|
continue
|
|
}
|
|
if _, ok := target.slots[slotName]; !ok {
|
|
return nil, fmt.Errorf("reference slot %q is not declared by selected %s target %q", slotName, selector.Stage, targetLabel(target))
|
|
}
|
|
return []selectedReferenceTarget{target}, nil
|
|
}
|
|
if !laneSelected {
|
|
return nil, fmt.Errorf("reference lane %q is not selected", selector.LaneID)
|
|
}
|
|
return nil, fmt.Errorf("reference %s target is not selected for lane %q", selector.Stage, selector.LaneID)
|
|
default:
|
|
return nil, fmt.Errorf("reference selector has unknown scope")
|
|
}
|
|
}
|
|
|
|
func targetLabel(target selectedReferenceTarget) string {
|
|
if target.stage == pipeline.StageChunk {
|
|
return "chunk"
|
|
}
|
|
return target.laneID + "." + string(target.stage)
|
|
}
|
|
|
|
func referenceStageOrder(stage pipeline.ModuleStage) int {
|
|
switch stage {
|
|
case pipeline.StageChunk:
|
|
return 0
|
|
case pipeline.StageExtract:
|
|
return 1
|
|
case pipeline.StageMerge:
|
|
return 2
|
|
case pipeline.StageNormalize:
|
|
return 3
|
|
default:
|
|
return 4
|
|
}
|
|
}
|