156 lines
7.3 KiB
Go
156 lines
7.3 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
"go.maximumdirect.net/commandkit"
|
|
)
|
|
|
|
type executionInput struct {
|
|
Selection configurationSelection
|
|
From, Through, Stage commandkit.Supplied[string]
|
|
Force commandkit.Supplied[bool]
|
|
Artifacts commandkit.Supplied[[]string]
|
|
PreviousSession commandkit.Supplied[string]
|
|
plan BoundedPlan
|
|
selectedArtifacts []string
|
|
}
|
|
|
|
type runReport struct {
|
|
Campaign string `json:"campaign"`
|
|
SessionID string `json:"session_id"`
|
|
RunID string `json:"run_id"`
|
|
Stages []string `json:"stages"`
|
|
Executed []string `json:"executed"`
|
|
Skipped []string `json:"skipped"`
|
|
ArtifactTargets []string `json:"artifact_targets"`
|
|
StageResults []RunStageResult `json:"stage_results"`
|
|
AvailableArtifacts []RunArtifact `json:"available_artifacts"`
|
|
Complete bool `json:"complete"`
|
|
}
|
|
|
|
func checkExecutionInput(input *executionInput) error {
|
|
if err := checkConfigurationSelection(input.Selection); err != nil {
|
|
return err
|
|
}
|
|
if input.Stage.Set && (input.From.Set || input.Through.Set) {
|
|
return fmt.Errorf("--stage cannot be combined with --from or --through")
|
|
}
|
|
for _, field := range []struct {
|
|
name string
|
|
value commandkit.Supplied[string]
|
|
}{{"stage", input.Stage}, {"from", input.From}, {"through", input.Through}} {
|
|
if field.value.Set && field.value.Value == "" {
|
|
return fmt.Errorf("--%s requires a stage name", field.name)
|
|
}
|
|
}
|
|
from, through := input.From.Value, input.Through.Value
|
|
if input.Stage.Set {
|
|
from, through = input.Stage.Value, input.Stage.Value
|
|
}
|
|
plan, err := BuildBoundedPlan(from, through)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
selected, err := normalizeArtifactNames(input.Artifacts.Value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(selected) > 0 && !plan.Contains("analyze") && !plan.Contains("publish") {
|
|
return fmt.Errorf("--artifacts requires a selected range containing analyze or publish")
|
|
}
|
|
input.plan, input.selectedArtifacts = plan, selected
|
|
return nil
|
|
}
|
|
|
|
func executionCommand(name, summary string, regenerate bool, deps commandDependencies, executor pipelineExecutor) commandkit.Command {
|
|
spec := executionCommandSpec(name, summary, regenerate)
|
|
spec.Run = func(invocation commandkit.Invocation, input *executionInput) commandkit.Outcome {
|
|
report, err := executeSelectedPipeline(invocation.Context, input, withCommandReporting(deps, invocation), executor)
|
|
if report != nil {
|
|
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
|
|
}
|
|
return operationOutcome(err)
|
|
}
|
|
return commandkit.NewCommand(spec)
|
|
}
|
|
|
|
func executionCommandSpec(name, summary string, regenerate bool) commandkit.CommandSpec[*executionInput] {
|
|
common := newConfigurationFlags(func(input **executionInput) *configurationSelection { return &(*input).Selection })
|
|
flags := []commandkit.Flag[*executionInput]{common.Config, common.Overlay, common.Campaign, common.CampaignFile, common.SessionFile, common.PartyFile, common.SecretsDir,
|
|
commandkit.ValueFlag(commandkit.FlagMeta{Name: "previous-session-id", Usage: "Assert the previous session identity."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).PreviousSession = value }),
|
|
commandkit.RepeatFlag(commandkit.FlagMeta{Name: "artifacts", Usage: "Analysis artifact or family targets (repeatable or comma-separated)."}, []string(nil), stringValue, func(input **executionInput, value commandkit.Supplied[[]string]) { (*input).Artifacts = value }),
|
|
}
|
|
if !regenerate {
|
|
flags = append(flags,
|
|
commandkit.ValueFlag(commandkit.FlagMeta{Name: "stage", Completion: stageCompletion(), Usage: "Select one canonical pipeline stage."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).Stage = value }),
|
|
commandkit.ValueFlag(commandkit.FlagMeta{Name: "from", Completion: stageCompletion(), Usage: "First pipeline stage, inclusive."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).From = value }),
|
|
commandkit.ValueFlag(commandkit.FlagMeta{Name: "through", Completion: stageCompletion(), Usage: "Last pipeline stage, inclusive."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).Through = value }),
|
|
commandkit.BoolFlag(commandkit.FlagMeta{Name: "force", Usage: "Rerun selected stages."}, false, func(input **executionInput, value commandkit.Supplied[bool]) { (*input).Force = value }),
|
|
)
|
|
}
|
|
return commandkit.CommandSpec[*executionInput]{
|
|
Name: name, Summary: summary, NewInput: func() *executionInput { return &executionInput{} }, Flags: flags,
|
|
Arguments: []commandkit.Argument[*executionInput]{commandkit.ValueArgument(commandkit.ArgumentMeta{Name: "SESSION", Usage: "Session identity."}, true, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).Selection.Session = value })},
|
|
CheckInput: func(input *executionInput) error {
|
|
if regenerate {
|
|
input.From = commandkit.Supplied[string]{Value: "extract", Set: true}
|
|
input.Through = commandkit.Supplied[string]{Value: "analyze", Set: true}
|
|
input.Force = commandkit.Supplied[bool]{Value: true, Set: true}
|
|
}
|
|
return checkExecutionInput(input)
|
|
},
|
|
}
|
|
}
|
|
|
|
func executeSelectedPipeline(ctx context.Context, input *executionInput, deps commandDependencies, executor pipelineExecutor) (report *runReport, err error) {
|
|
invocationDeps := normalizeCommandDependencies(deps)
|
|
invocationDeps.secretsBootstrap = &secretsBootstrap{}
|
|
acquired := newConfigurationAcquisition(input.Selection, invocationDeps)
|
|
acquired.previousSessionID = input.PreviousSession.Value
|
|
defer func() {
|
|
err = errors.Join(err, acquired.Close())
|
|
if report != nil {
|
|
report.Complete = err == nil
|
|
}
|
|
}()
|
|
cfg, err := acquired.Domain(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := config.Validate(cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
selected, err := normalizeArtifactSelection(cfg, input.selectedArtifacts)
|
|
if err != nil {
|
|
return nil, &inputError{message: err.Error(), cause: err}
|
|
}
|
|
effective, err := resolveEffectiveArtifacts(cfg, selected)
|
|
if err != nil {
|
|
return nil, &inputError{message: err.Error(), cause: err}
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
summary, err := executor(ctx, cfg, input.plan, RunOptions{Force: input.Force.Value, SelectedArtifacts: selected, EffectiveArtifacts: effective, Reporter: invocationDeps.reporter})
|
|
if summary == nil {
|
|
if err == nil {
|
|
err = fmt.Errorf("pipeline execution returned no run summary")
|
|
}
|
|
return nil, err
|
|
}
|
|
return &runReport{Campaign: config.CampaignID(cfg.Campaign), SessionID: summary.SessionID, RunID: summary.RunID, Stages: input.plan.Names(), Executed: summary.Executed, Skipped: summary.Skipped, ArtifactTargets: effective.Keys(), StageResults: summary.StageResults, AvailableArtifacts: summary.AvailableArtifacts, Complete: err == nil}, err
|
|
}
|
|
|
|
func stageCompletion() commandkit.CompletionHint {
|
|
names := canonicalStageNames()
|
|
choices := make([]commandkit.CompletionChoice, 0, len(names))
|
|
for _, name := range names {
|
|
choices = append(choices, commandkit.CompletionChoice{Value: name})
|
|
}
|
|
return commandkit.CompleteChoices(choices...)
|
|
}
|