2026-11-22 03:32:31 +00:00
package cli
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"text/template"
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
"gitea.maximumdirect.net/eric/notarius/internal/preflight"
"go.maximumdirect.net/ckconfig"
"go.maximumdirect.net/commandkit"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
)
type pipelineInspectionInput struct {
Selection ckconfig . Selection
PipelineID string
}
func configurationSelectionFlags [ I any ] ( destination func ( * I ) * ckconfig . Selection ) [ ] commandkit . Flag [ I ] {
return [ ] commandkit . Flag [ I ] {
commandkit . ValueFlag ( commandkit . FlagMeta { Name : "config" , Usage : "Select the application configuration." , Completion : commandkit . CompleteFiles ( ) } , ( * string ) ( nil ) , func ( value string ) ( string , error ) {
if strings . TrimSpace ( value ) == "" {
return "" , fmt . Errorf ( "--config must not be empty" )
}
return value , nil
} , func ( input * I , value commandkit . Supplied [ string ] ) { destination ( input ) . Config = value } ) ,
commandkit . ValueFlag ( commandkit . FlagMeta { Name : "overlay" , Usage : "Select a configuration overlay." , Completion : commandkit . CompleteFiles ( ) } , ( * string ) ( nil ) , func ( value string ) ( string , error ) { return value , nil } , func ( input * I , value commandkit . Supplied [ string ] ) { destination ( input ) . Overlay = value } ) ,
commandkit . RepeatFlag ( commandkit . FlagMeta { Name : "set" , Usage : "Override POINTER=JSON." } , [ ] configkit . Override ( nil ) , parseConfigurationOverride , func ( input * I , value commandkit . Supplied [ [ ] configkit . Override ] ) {
destination ( input ) . Overrides = value . Value
} ) ,
}
}
func parseConfigurationOverride ( raw string ) ( configkit . Override , error ) {
pointer , encoded , ok := strings . Cut ( raw , "=" )
if ! ok {
return configkit . Override { } , fmt . Errorf ( "override requires POINTER=JSON" )
}
path , err := configkit . ParsePath ( pointer )
if err != nil {
return configkit . Override { } , err
}
value , err := commandkit . ParseCLIJSON ( encoded )
if err != nil {
return configkit . Override { } , err
}
return configkit . Override { Path : path , Value : value , Flag : "--set" } , nil
}
2026-11-22 03:51:08 +00:00
func pipelineCommands ( newState func ( ) * nativeLoaders , catalog func ( ) ( pipeline . ModuleCatalog , error ) , additional ... commandkit . Command ) commandkit . Command {
2026-11-22 03:32:31 +00:00
command := func ( operation , summary string ) commandkit . Command {
var arguments [ ] commandkit . Argument [ pipelineInspectionInput ]
if operation != "list" {
arguments = [ ] commandkit . Argument [ pipelineInspectionInput ] { commandkit . ValueArgument ( commandkit . ArgumentMeta { Name : "pipeline-id" , Usage : "Configured pipeline identifier." } , true , func ( value string ) ( string , error ) {
value = strings . TrimSpace ( value )
if value == "" {
return "" , fmt . Errorf ( "pipeline id must not be empty" )
}
return value , nil
} , func ( input * pipelineInspectionInput , value commandkit . Supplied [ string ] ) {
input . PipelineID = value . Value
} ) }
}
return commandkit . NewCommand ( commandkit . CommandSpec [ pipelineInspectionInput ] { Name : operation , Summary : summary ,
Flags : configurationSelectionFlags ( func ( input * pipelineInspectionInput ) * ckconfig . Selection { return & input . Selection } ) , Arguments : arguments ,
CheckInput : func ( input pipelineInspectionInput ) error { return checkConfigSelection ( input . Selection ) } ,
Run : func ( invocation commandkit . Invocation , input pipelineInspectionInput ) commandkit . Outcome {
state := newState ( )
result , err := state . application ( invocation . Context , input . Selection )
if err != nil {
return pipelineConfigurationFailure ( err )
}
if operation == "list" {
ids := make ( [ ] string , 0 , len ( result . Config . Pipelines ) )
for id := range result . Config . Pipelines {
ids = append ( ids , id )
}
sort . Strings ( ids )
return reportPipelineInspection ( invocation , commandkit . Report { Value : ids } )
}
loaded , err := config . FromResult ( result )
if err != nil {
return pipelineInvalidConfiguration ( err )
}
modules , err := catalog ( )
if err != nil {
return commandkit . Failure ( err . Error ( ) ) . WithCause ( err )
}
effective , err := loaded . RuntimeConfig ( ) . Resolve ( config . ResolveInput { PipelineID : input . PipelineID , Catalog : modules } )
if err != nil {
return pipelineInvalidConfiguration ( err )
}
if operation == "show" {
return reportPipelineInspection ( invocation , commandkit . Report { Value : describePipeline ( effective . ResolvedPipeline , modules ) } )
}
var engine * promptkit . Engine
if preflight . RequiresEngine ( effective . ResolvedPipeline ) {
engine , err = state . engine ( invocation . Context , input . Selection )
if err != nil {
return pipelineConfigurationFailure ( fmt . Errorf ( "pipeline %q native resources: %w" , input . PipelineID , err ) )
}
}
if err := preflight . Validate ( invocation . Context , effective . ResolvedPipeline , modules , engine ) ; err != nil {
2026-11-22 04:05:44 +00:00
return pipelineResourceFailure ( err )
2026-11-22 03:32:31 +00:00
}
return reportPipelineInspection ( invocation , commandkit . Report { Value : struct {
Pipeline string ` json:"pipeline" `
Valid bool ` json:"valid" `
} { input . PipelineID , true } , TextTemplate : pipelineValidationTemplate } )
} ,
} )
}
2026-11-22 03:51:08 +00:00
children := [ ] commandkit . Command { command ( "list" , "List configured pipeline identifiers." ) , command ( "show" , "Show a resolved pipeline composition." ) , command ( "validate" , "Validate known pipeline dependencies offline." ) }
return commandkit . Group ( "pipeline" , "Inspect, validate, or execute configured pipelines." , append ( children , additional ... ) ... )
2026-11-22 03:32:31 +00:00
}
var pipelineValidationTemplate = template . Must ( commandkit . ParseTextTemplate ( "pipeline-validation" , "Pipeline {{printf \"%q\" .Pipeline}} is valid.\n" , nil ) )
func reportPipelineInspection ( invocation commandkit . Invocation , report commandkit . Report ) commandkit . Outcome {
if err := invocation . Reporter ( ) . Report ( report ) ; err != nil {
2026-11-22 04:05:44 +00:00
return commandkit . Failure ( "Could not report pipeline inspection." )
2026-11-22 03:32:31 +00:00
}
return commandkit . Success ( )
}
func pipelineConfigurationFailure ( err error ) commandkit . Outcome {
2026-11-22 04:05:44 +00:00
outcome := commandkit . Failure ( err . Error ( ) ) . WithCause ( err )
2026-11-22 03:32:31 +00:00
if ckconfig . ClassifyConfigurationError ( err ) == commandkit . OutcomeUsage {
2026-11-22 04:05:44 +00:00
outcome = commandkit . Usage ( err . Error ( ) ) . WithCause ( err )
}
var load * configkit . LoadError
if errors . As ( err , & load ) {
_ , explanation := ckconfig . AdaptDiagnostics ( err . Error ( ) + "\n" , load . Sources ( ) , load . Diagnostics ( ) )
outcome = outcome . WithExplanation ( explanation . Message , explanation . Details ... )
}
return outcome
}
func pipelineResourceFailure ( err error ) commandkit . Outcome {
if preflightInputError ( err ) {
2026-11-22 03:32:31 +00:00
return commandkit . Usage ( err . Error ( ) ) . WithCause ( err )
}
2026-11-22 04:05:44 +00:00
return pipelineConfigurationFailure ( err )
}
// Only identifiable missing native selections are invalid preflight input.
// Unknown failures and any operational branch of a joined error remain failures.
func preflightInputError ( err error ) bool {
if err == nil || errors . Is ( err , context . Canceled ) || errors . Is ( err , context . DeadlineExceeded ) {
return false
}
if _ , ok := err . ( * preflight . SelectionError ) ; ok {
return true
}
if joined , ok := err . ( interface { Unwrap ( ) [ ] error } ) ; ok {
found := false
for _ , child := range joined . Unwrap ( ) {
if child != nil {
found = true
if ! preflightInputError ( child ) {
return false
}
}
}
return found
}
if wrapped , ok := err . ( interface { Unwrap ( ) error } ) ; ok {
return preflightInputError ( wrapped . Unwrap ( ) )
}
return false
2026-11-22 03:32:31 +00:00
}
func pipelineInvalidConfiguration ( err error ) commandkit . Outcome {
if errors . Is ( err , context . Canceled ) || errors . Is ( err , context . DeadlineExceeded ) {
return commandkit . Failure ( err . Error ( ) ) . WithCause ( err )
}
return commandkit . Usage ( err . Error ( ) ) . WithCause ( err )
}