2026-11-20 05:10:04 +00:00
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 {
2026-11-20 05:47:41 +00:00
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" `
2026-11-20 05:10:04 +00:00
}
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 {
2026-11-20 05:15:43 +00:00
spec := executionCommandSpec ( name , summary , regenerate )
spec . Run = func ( invocation commandkit . Invocation , input * executionInput ) commandkit . Outcome {
2026-11-20 05:59:09 +00:00
report , err := executeSelectedPipeline ( invocation . Context , input , withCommandReporting ( deps , invocation ) , executor )
2026-11-20 05:15:43 +00:00
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 ] {
2026-11-20 05:10:04 +00:00
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 ,
2026-11-20 06:32:47 +00:00
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 } ) ,
2026-11-20 05:10:04 +00:00
commandkit . BoolFlag ( commandkit . FlagMeta { Name : "force" , Usage : "Rerun selected stages." } , false , func ( input * * executionInput , value commandkit . Supplied [ bool ] ) { ( * input ) . Force = value } ) ,
)
}
2026-11-20 05:15:43 +00:00
return commandkit . CommandSpec [ * executionInput ] {
2026-11-20 05:10:04 +00:00
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 )
} ,
2026-11-20 05:15:43 +00:00
}
2026-11-20 05:10:04 +00:00
}
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 }
}
2026-11-20 05:53:49 +00:00
if err := ctx . Err ( ) ; err != nil {
return nil , err
}
2026-11-20 05:59:09 +00:00
summary , err := executor ( ctx , cfg , input . plan , RunOptions { Force : input . Force . Value , SelectedArtifacts : selected , EffectiveArtifacts : effective , Reporter : invocationDeps . reporter } )
2026-11-20 05:10:04 +00:00
if summary == nil {
if err == nil {
err = fmt . Errorf ( "pipeline execution returned no run summary" )
}
return nil , err
}
2026-11-20 05:47:41 +00:00
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
2026-11-20 05:10:04 +00:00
}
2026-11-20 06:32:47 +00:00
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 ... )
}