2026-11-20 05:02:46 +00:00
package app
import (
2026-11-20 06:32:47 +00:00
"bytes"
2026-11-20 05:02:46 +00:00
"context"
"errors"
2026-11-20 06:32:47 +00:00
"io"
2026-11-20 05:02:46 +00:00
"gitea.maximumdirect.net/eric/narratio/internal/config"
"go.maximumdirect.net/ckconfig"
"go.maximumdirect.net/commandkit"
configkit "go.maximumdirect.net/configkit"
)
type configurationInspectionState struct {
2026-11-20 06:32:47 +00:00
pendingDiagnostics * bytes . Buffer
deps commandDependencies
independent bool
acquisitions map [ configurationSelection ] * configurationAcquisition
2026-11-20 05:02:46 +00:00
}
func ( s * configurationInspectionState ) acquisition ( selection ckconfig . Selection , input configurationSelection ) * configurationAcquisition {
input . Config , input . Overlay = selection . Config , selection . Overlay
2026-11-20 05:06:26 +00:00
if s . independent || len ( selection . Overrides ) != 0 {
acquired := newConfigurationAcquisition ( input , s . deps )
acquired . pipelineOverrides = append ( [ ] configkit . Override ( nil ) , selection . Overrides ... )
return acquired
}
2026-11-20 05:02:46 +00:00
acquired := s . acquisitions [ input ]
if acquired == nil {
acquired = newConfigurationAcquisition ( input , s . deps )
s . acquisitions [ input ] = acquired
}
return acquired
}
func configurationCommand ( deps commandDependencies ) commandkit . Command {
flags := newConfigurationFlags ( func ( input * configurationSelection ) * configurationSelection { return input } )
app := ckconfig . FromConfigKit ( "app" , "Application pipeline configuration." , func ( ctx context . Context , selection ckconfig . Selection , input configurationSelection , state * configurationInspectionState ) ( * configkit . Result [ config . PipelineConfig ] , error ) {
acquired := state . acquisition ( selection , input )
cfg , err := acquired . Pipeline ( ctx )
if err != nil {
return nil , err
}
return cfg . ConfigKitResult ( ) , nil
} )
2026-11-20 05:06:26 +00:00
app . Features . Overrides = true
2026-11-20 05:02:46 +00:00
campaign := ckconfig . FromConfigKit ( "campaign" , "Campaign configuration." , func ( ctx context . Context , selection ckconfig . Selection , input configurationSelection , state * configurationInspectionState ) ( * configkit . Result [ config . CampaignConfig ] , error ) {
acquired := state . acquisition ( selection , input )
cfg , err := acquired . Campaign ( ctx )
if err != nil {
return nil , err
}
return cfg . ConfigKitResult ( ) , nil
} )
session := ckconfig . FromConfigKit ( "session" , "Session configuration and campaign defaults." , func ( ctx context . Context , selection ckconfig . Selection , input configurationSelection , state * configurationInspectionState ) ( result * configkit . Result [ config . SessionConfig ] , err error ) {
acquired := state . acquisition ( selection , input )
defer func ( ) { err = errors . Join ( err , acquired . Close ( ) ) } ( )
cfg , err := acquired . Session ( ctx )
if err != nil {
return nil , err
}
return cfg . ConfigKitResult ( ) , nil
} )
party := ckconfig . FromConfigKit ( "party" , "Selected party configuration." , func ( ctx context . Context , selection ckconfig . Selection , input configurationSelection , state * configurationInspectionState ) ( * configkit . Result [ config . PartyConfig ] , error ) {
acquired := state . acquisition ( selection , input )
cfg , err := acquired . Party ( ctx )
if err != nil {
return nil , err
}
return cfg . Canonical . ConfigKitResult ( ) , nil
} )
2026-11-20 05:06:26 +00:00
scopes := [ ] ckconfig . Scope [ configurationSelection , * configurationInspectionState ] { app , campaign , session , party }
// Comparison sides share bootstrap services, but always load their own results.
for index := range scopes {
2026-11-20 06:32:47 +00:00
acquire := scopes [ index ] . Acquire
scopes [ index ] . Acquire = func ( ctx context . Context , request ckconfig . Request , input configurationSelection , state * configurationInspectionState ) ( commandkit . Acquired [ ckconfig . View ] , error ) {
result , err := acquire ( ctx , request , input , state )
result . Diagnostics = append ( result . Diagnostics , state . bootstrapDiagnostics ( ) ... )
2026-11-20 06:50:56 +00:00
var identity * config . SessionIdentityError
if errors . As ( err , & identity ) {
result . Explanation . Message = identity . Error ( )
}
2026-11-20 06:32:47 +00:00
return result , err
}
2026-11-20 05:06:26 +00:00
compare := scopes [ index ] . Compare
scopes [ index ] . Compare = func ( ctx context . Context , request ckconfig . ComparisonRequest [ configurationSelection ] , state * configurationInspectionState ) ( commandkit . Acquired [ [ ] configkit . Difference ] , error ) {
separate := * state
separate . independent = true
2026-11-20 06:32:47 +00:00
result , err := compare ( ctx , request , & separate )
result . Diagnostics = append ( result . Diagnostics , state . bootstrapDiagnostics ( ) ... )
2026-11-20 06:50:56 +00:00
var identity * config . SessionIdentityError
if errors . As ( err , & identity ) {
result . Explanation . Message = identity . Error ( )
}
2026-11-20 06:32:47 +00:00
return result , err
2026-11-20 05:06:26 +00:00
}
}
2026-11-20 05:02:46 +00:00
return ckconfig . Command ( ckconfig . Suite [ configurationSelection , * configurationInspectionState ] {
NewState : func ( ) * configurationInspectionState {
invocationDeps := normalizeCommandDependencies ( deps )
invocationDeps . secretsBootstrap = & secretsBootstrap { }
2026-11-20 06:32:47 +00:00
pending := & bytes . Buffer { }
invocationDeps . diagnostics = pending
return & configurationInspectionState { deps : invocationDeps , pendingDiagnostics : pending , acquisitions : make ( map [ configurationSelection ] * configurationAcquisition ) }
2026-11-20 05:02:46 +00:00
} ,
CheckInput : func ( _ string , selection ckconfig . Selection , input configurationSelection ) error {
input . Config , input . Overlay = selection . Config , selection . Overlay
return checkConfigurationSelection ( input )
} ,
Selectors : [ ] ckconfig . Selector [ configurationSelection ] {
2026-11-20 05:06:26 +00:00
{ Binding : flags . Campaign , AgainstName : "against-campaign" , Scopes : [ ] string { "campaign" , "session" , "party" } } ,
{ Binding : flags . CampaignFile , AgainstName : "against-campaign-file" , Scopes : [ ] string { "campaign" , "session" , "party" } } ,
{ Binding : flags . Session , AgainstName : "against-session" , Scopes : [ ] string { "session" } } ,
{ Binding : flags . SessionFile , AgainstName : "against-session-file" , Scopes : [ ] string { "session" } } ,
{ Binding : flags . PartyFile , AgainstName : "against-party-file" , Scopes : [ ] string { "party" } } ,
2026-11-20 05:02:46 +00:00
{ Binding : flags . SecretsDir , Scopes : [ ] string { "app" , "campaign" , "session" , "party" } } ,
} ,
2026-11-20 05:06:26 +00:00
Scopes : scopes ,
2026-11-20 05:02:46 +00:00
} )
}
2026-11-20 06:32:47 +00:00
func ( s * configurationInspectionState ) bootstrapDiagnostics ( ) [ ] commandkit . Diagnostic {
if s . pendingDiagnostics == nil || s . pendingDiagnostics . Len ( ) == 0 {
return nil
}
text := s . pendingDiagnostics . String ( )
s . pendingDiagnostics . Reset ( )
return [ ] commandkit . Diagnostic { { Severity : commandkit . SeverityInfo , Write : func ( w io . Writer ) error { _ , err := io . WriteString ( w , text ) ; return err } } }
}