2026-11-20 04:26:17 +00:00
package cli
import (
"context"
2026-11-20 04:32:01 +00:00
"errors"
2026-11-20 04:26:17 +00:00
"strconv"
appconfig "gitea.maximumdirect.net/eric/scriptorium/internal/config"
"go.maximumdirect.net/ckconfig"
"go.maximumdirect.net/commandkit"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
)
// applicationInput holds only application-owned selectors. Supplied retains the
// distinction between omission and a caller's empty or zero value.
type applicationInput struct {
Addr commandkit . Supplied [ string ]
ArtifactRoot commandkit . Supplied [ string ]
MaxRequestBytes commandkit . Supplied [ int64 ]
MaxArtifactBytes commandkit . Supplied [ int64 ]
MaxResponseBytes commandkit . Supplied [ int64 ]
}
func configurationSelectors [ I any ] ( target func ( * I ) * ckconfig . Selection ) [ ] commandkit . Flag [ I ] {
return [ ] commandkit . Flag [ I ] {
commandkit . ValueFlag ( commandkit . FlagMeta { Name : "config" , Usage : "Select the application configuration file." , Completion : commandkit . CompleteFiles ( ) } , ( * string ) ( nil ) , parseString ,
func ( input * I , value commandkit . Supplied [ string ] ) { target ( input ) . Config = value } ) ,
commandkit . ValueFlag ( commandkit . FlagMeta { Name : "overlay" , Usage : "Select an application overlay by name." } , ( * string ) ( nil ) , parseString ,
func ( input * I , value commandkit . Supplied [ string ] ) { target ( input ) . Overlay = value } ) ,
}
}
func serverSelectors [ I any ] ( target func ( * I ) * applicationInput ) [ ] commandkit . Flag [ I ] {
stringFlag := func ( name , usage string , completion commandkit . CompletionHint , field func ( * applicationInput ) * commandkit . Supplied [ string ] ) commandkit . Flag [ I ] {
return commandkit . ValueFlag ( commandkit . FlagMeta { Name : name , Usage : usage , Completion : completion } , ( * string ) ( nil ) , parseString ,
func ( input * I , value commandkit . Supplied [ string ] ) { * field ( target ( input ) ) = value } )
}
limitFlag := func ( name , usage string , field func ( * applicationInput ) * commandkit . Supplied [ int64 ] ) commandkit . Flag [ I ] {
return commandkit . ValueFlag ( commandkit . FlagMeta { Name : name , Usage : usage } , ( * int64 ) ( nil ) , func ( raw string ) ( int64 , error ) { return strconv . ParseInt ( raw , 10 , 64 ) } ,
func ( input * I , value commandkit . Supplied [ int64 ] ) { * field ( target ( input ) ) = value } )
}
return [ ] commandkit . Flag [ I ] {
stringFlag ( "addr" , "HTTP listen address." , commandkit . NoCompletion ( ) , func ( input * applicationInput ) * commandkit . Supplied [ string ] { return & input . Addr } ) ,
stringFlag ( "artifact-root" , "HTTP artifact root." , commandkit . CompleteDirectories ( ) , func ( input * applicationInput ) * commandkit . Supplied [ string ] { return & input . ArtifactRoot } ) ,
limitFlag ( "max-request-bytes" , "HTTP request byte limit (zero is unlimited)." , func ( input * applicationInput ) * commandkit . Supplied [ int64 ] { return & input . MaxRequestBytes } ) ,
limitFlag ( "max-artifact-bytes" , "HTTP artifact byte limit (zero is unlimited)." , func ( input * applicationInput ) * commandkit . Supplied [ int64 ] { return & input . MaxArtifactBytes } ) ,
limitFlag ( "max-response-bytes" , "HTTP response byte limit (zero is unlimited)." , func ( input * applicationInput ) * commandkit . Supplied [ int64 ] { return & input . MaxResponseBytes } ) ,
}
}
func parseString ( raw string ) ( string , error ) { return raw , nil }
func namedApplicationOverrides ( input applicationInput ) [ ] configkit . Override {
var records [ ] configkit . Override
appendString := func ( path configkit . Path , flag string , value commandkit . Supplied [ string ] ) {
if value . Set {
records = append ( records , configkit . Override { Path : path , Value : value . Value , Flag : flag } )
}
}
appendLimit := func ( key , flag string , value commandkit . Supplied [ int64 ] ) {
if value . Set {
records = append ( records , configkit . Override { Path : configkit . Keys ( "server" , key ) , Value : value . Value , Flag : flag } )
}
}
appendString ( configkit . Keys ( "server" , "addr" ) , "--addr" , input . Addr )
appendString ( configkit . Keys ( "server" , "artifact_root" ) , "--artifact-root" , input . ArtifactRoot )
appendLimit ( "max_request_bytes" , "--max-request-bytes" , input . MaxRequestBytes )
appendLimit ( "max_artifact_bytes" , "--max-artifact-bytes" , input . MaxArtifactBytes )
appendLimit ( "max_response_bytes" , "--max-response-bytes" , input . MaxResponseBytes )
return records
}
func loadApplication ( ctx context . Context , selection ckconfig . Selection , input applicationInput , _ struct { } ) ( * configkit . Result [ appconfig . Document ] , error ) {
if err := ctx . Err ( ) ; err != nil {
return nil , err
}
result , err := appconfig . Load ( appconfig . LoadOptions {
Path : selection . Config . Value , Explicit : selection . Config . Set , Overlay : selection . Overlay . Value ,
Overrides : append ( namedApplicationOverrides ( input ) , selection . Overrides ... ) ,
} )
if err != nil {
return nil , err
}
if err := ctx . Err ( ) ; err != nil {
2026-11-20 04:55:27 +00:00
return result , err
2026-11-20 04:26:17 +00:00
}
return result , nil
}
2026-11-20 04:32:01 +00:00
func loadEngine ( ctx context . Context , selection ckconfig . Selection , input applicationInput , state * acquisitionState ) ( * promptkit . Engine , error ) {
result , err := loadApplication ( ctx , selection , input , struct { } { } )
if state != nil {
evidence := applicationAcquisition { err : err }
if result != nil {
evidence . sources , evidence . records = result . Sources ( ) , result . Diagnostics ( )
}
var native * configkit . LoadError
if errors . As ( err , & native ) && native != nil {
evidence . sources , evidence . records = native . Sources ( ) , native . Diagnostics ( )
}
state . applications = append ( state . applications , evidence )
if err != nil {
return nil , errApplicationAcquisition
}
}
2026-11-20 04:26:17 +00:00
if err != nil {
return nil , err
}
settings := appconfig . Settings ( result . Config )
return newEngine ( settings )
}