@@ -65,6 +65,10 @@ struct ConfigPreflightArgs {
6565 #[ arg( long, conflicts_with = "gateway_args" ) ]
6666 path : Option < PathBuf > ,
6767
68+ /// Explicit drop-in directory. Defaults to `gateway.d` next to the base file.
69+ #[ arg( long, conflicts_with = "gateway_args" ) ]
70+ config_dir : Option < PathBuf > ,
71+
6872 /// Gateway daemon arguments to replay after `--`.
6973 #[ arg( last = true , allow_hyphen_values = true , value_name = "GATEWAY_ARGS" ) ]
7074 gateway_args : Vec < OsString > ,
@@ -81,6 +85,14 @@ struct RunArgs {
8185 #[ arg( long, env = "OPENSHELL_GATEWAY_CONFIG" ) ]
8286 config : Option < PathBuf > ,
8387
88+ /// Directory of `*.toml` drop-in fragments merged over the base file.
89+ ///
90+ /// Fragments apply in lexical order and a later fragment wins. Tables
91+ /// merge key by key; scalars and arrays replace. Defaults to `gateway.d`
92+ /// next to the base file when that directory exists.
93+ #[ arg( long, env = "OPENSHELL_GATEWAY_CONFIG_DIR" ) ]
94+ config_dir : Option < PathBuf > ,
95+
8496 /// Operator-assigned name for this gateway installation.
8597 #[ arg(
8698 long = "name" ,
@@ -423,11 +435,15 @@ fn prepare_server_config_with_drivers(
423435 // Load TOML when explicitly requested, or from the default XDG location
424436 // when that file exists. Missing default config is not an error: runtime
425437 // defaults and OPENSHELL_* env vars are enough for package-managed starts.
426- let config_path = resolve_config_path ( args) ?;
427- let mut file: Option < ConfigFile > = if let Some ( path) = config_path {
428- Some ( config_file:: load ( & path) . map_err ( |e| miette:: miette!( "{e}" ) ) ?)
429- } else {
438+ let sources = resolve_config_sources ( args, None , None ) ?;
439+ let mut file: Option < ConfigFile > = if sources. is_empty ( ) {
430440 None
441+ } else {
442+ Some (
443+ config_file:: load_layered ( sources. base . as_deref ( ) , sources. dir . as_deref ( ) )
444+ . map_err ( |e| miette:: miette!( "{e}" ) ) ?
445+ . file ,
446+ )
431447 } ;
432448 if let Some ( file) = file. as_ref ( ) {
433449 merge_file_into_args ( args, & file. openshell . gateway , matches) ;
@@ -1041,7 +1057,13 @@ fn run_config_preflight_with_drivers(
10411057 compute_drivers : & ComputeDriverRegistry ,
10421058) -> Result < Option < crate :: ConfiguredComputeDriver > > {
10431059 if args. gateway_args . is_empty ( ) {
1044- return run_effective_config_preflight ( args. path , run, matches, compute_drivers) ;
1060+ return run_effective_config_preflight (
1061+ args. path ,
1062+ args. config_dir ,
1063+ run,
1064+ matches,
1065+ compute_drivers,
1066+ ) ;
10451067 }
10461068
10471069 let replay_matches = match command ( ) . try_get_matches_from (
@@ -1065,24 +1087,30 @@ fn run_config_preflight_with_drivers(
10651087 // configuration. Let the immediately following invocation perform it.
10661088 return Ok ( None ) ;
10671089 }
1068- run_effective_config_preflight ( None , replay. run , & replay_matches, compute_drivers)
1090+ run_effective_config_preflight ( None , None , replay. run , & replay_matches, compute_drivers)
10691091}
10701092
10711093fn run_effective_config_preflight (
10721094 path_override : Option < PathBuf > ,
1095+ dir_override : Option < PathBuf > ,
10731096 mut run : RunArgs ,
10741097 matches : & ArgMatches ,
10751098 compute_drivers : & ComputeDriverRegistry ,
10761099) -> Result < Option < crate :: ConfiguredComputeDriver > > {
1077- let path = if path_override. is_some ( ) {
1078- path_override
1100+ let sources = resolve_config_sources ( & run, path_override, dir_override) ?;
1101+ // Name the base file (or the directory) in the stable failure message.
1102+ let path = sources. base . clone ( ) . or_else ( || sources. dir . clone ( ) ) ;
1103+ let mut file = if sources. is_empty ( ) {
1104+ None
10791105 } else {
1080- resolve_config_path ( & run) ?
1106+ let layered =
1107+ config_file:: preflight_layered ( sources. base . as_deref ( ) , sources. dir . as_deref ( ) )
1108+ . map_err ( |error| miette:: miette!( "{error}" ) ) ?;
1109+ for source in & layered. sources {
1110+ println ! ( "config: merged {}" , source. display( ) ) ;
1111+ }
1112+ Some ( layered. file )
10811113 } ;
1082- let mut file = path
1083- . as_ref ( )
1084- . map ( |path| config_file:: preflight ( path) . map_err ( |error| miette:: miette!( "{error}" ) ) )
1085- . transpose ( ) ?;
10861114 if let Some ( file) = file. as_ref ( ) {
10871115 merge_file_into_args ( & mut run, & file. openshell . gateway , matches) ;
10881116 }
@@ -1386,21 +1414,54 @@ fn validate_preflight_semantics(
13861414 Ok ( ( ) )
13871415}
13881416
1389- fn resolve_config_path ( args : & RunArgs ) -> Result < Option < PathBuf > > {
1390- if let Some ( path) = args. config . clone ( ) {
1391- return Ok ( Some ( path) ) ;
1392- }
1417+ /// Where gateway configuration is read from: an optional base file and an
1418+ /// optional directory of `*.toml` drop-in fragments merged over it.
1419+ #[ derive( Debug , Default ) ]
1420+ struct ConfigSources {
1421+ base : Option < PathBuf > ,
1422+ dir : Option < PathBuf > ,
1423+ }
13931424
1394- let default_path = defaults:: default_gateway_config_path ( ) ?;
1395- match std:: fs:: symlink_metadata ( & default_path) {
1396- Err ( error) if error. kind ( ) == std:: io:: ErrorKind :: NotFound => Ok ( None ) ,
1397- // Fail closed when the path exists or cannot be inspected. Returning it
1398- // lets the shared loader produce the same safe, path-specific error used
1399- // for an explicit configuration instead of treating it as absent.
1400- Ok ( _) | Err ( _) => Ok ( Some ( default_path) ) ,
1425+ impl ConfigSources {
1426+ fn is_empty ( & self ) -> bool {
1427+ self . base . is_none ( ) && self . dir . is_none ( )
14011428 }
14021429}
14031430
1431+ fn resolve_config_sources (
1432+ args : & RunArgs ,
1433+ base_override : Option < PathBuf > ,
1434+ dir_override : Option < PathBuf > ,
1435+ ) -> Result < ConfigSources > {
1436+ let base = if let Some ( path) = base_override. or_else ( || args. config . clone ( ) ) {
1437+ Some ( path)
1438+ } else {
1439+ let default_path = defaults:: default_gateway_config_path ( ) ?;
1440+ match std:: fs:: symlink_metadata ( & default_path) {
1441+ Err ( error) if error. kind ( ) == std:: io:: ErrorKind :: NotFound => None ,
1442+ // Fail closed when the path exists or cannot be inspected.
1443+ // Returning it lets the shared loader produce the same safe,
1444+ // path-specific error used for an explicit configuration.
1445+ Ok ( _) | Err ( _) => Some ( default_path) ,
1446+ }
1447+ } ;
1448+ // An explicit directory must exist. Otherwise use `gateway.d` beside the
1449+ // base file (or beside the default location) when it is there.
1450+ let dir = if let Some ( dir) = dir_override. or_else ( || args. config_dir . clone ( ) ) {
1451+ Some ( dir)
1452+ } else {
1453+ let anchor = match base. clone ( ) {
1454+ Some ( path) => path,
1455+ None => defaults:: default_gateway_config_path ( ) ?,
1456+ } ;
1457+ anchor
1458+ . parent ( )
1459+ . map ( |parent| parent. join ( "gateway.d" ) )
1460+ . filter ( |dir| dir. is_dir ( ) )
1461+ } ;
1462+ Ok ( ConfigSources { base, dir } )
1463+ }
1464+
14041465fn apply_runtime_defaults ( args : & mut RunArgs ) -> Result < Option < LocalTlsPaths > > {
14051466 let local_tls = if args. disable_tls {
14061467 None
@@ -3633,13 +3694,23 @@ require_client_auth = true
36333694 let tmp = std:: path:: PathBuf :: from ( std:: env:: var_os ( "XDG_CONFIG_HOME" ) . unwrap ( ) ) ;
36343695
36353696 let ( args, _) = parse_with_args ( & [ "openshell-gateway" ] ) ;
3636- assert_eq ! ( super :: resolve_config_path( & args) . unwrap( ) , None ) ;
3697+ assert_eq ! (
3698+ super :: resolve_config_sources( & args, None , None )
3699+ . unwrap( )
3700+ . base,
3701+ None
3702+ ) ;
36373703
36383704 let config = tmp. join ( "openshell" ) . join ( "gateway.toml" ) ;
36393705 std:: fs:: create_dir_all ( config. parent ( ) . unwrap ( ) ) . unwrap ( ) ;
36403706 std:: fs:: write ( & config, "[openshell]\n version = 2\n " ) . unwrap ( ) ;
36413707
3642- assert_eq ! ( super :: resolve_config_path( & args) . unwrap( ) , Some ( config) ) ;
3708+ assert_eq ! (
3709+ super :: resolve_config_sources( & args, None , None )
3710+ . unwrap( )
3711+ . base,
3712+ Some ( config)
3713+ ) ;
36433714 } ) ;
36443715 }
36453716
@@ -3652,12 +3723,106 @@ require_client_auth = true
36523723 parse_with_args ( & [ "openshell-gateway" , "--config" , "/tmp/missing.toml" ] ) ;
36533724
36543725 assert_eq ! (
3655- super :: resolve_config_path( & args) . unwrap( ) ,
3726+ super :: resolve_config_sources( & args, None , None )
3727+ . unwrap( )
3728+ . base,
36563729 Some ( std:: path:: PathBuf :: from( "/tmp/missing.toml" ) )
36573730 ) ;
36583731 } ) ;
36593732 }
36603733
3734+ #[ test]
3735+ fn config_dir_defaults_to_gateway_d_beside_the_base_file ( ) {
3736+ let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
3737+ let base = tmp. path ( ) . join ( "gateway.toml" ) ;
3738+ std:: fs:: write ( & base, "[openshell]\n version = 2\n " ) . unwrap ( ) ;
3739+ Environment :: new ( )
3740+ . remove ( "OPENSHELL_GATEWAY_CONFIG" )
3741+ . remove ( "OPENSHELL_GATEWAY_CONFIG_DIR" )
3742+ . run ( || {
3743+ let ( args, _) =
3744+ parse_with_args ( & [ "openshell-gateway" , "--config" , base. to_str ( ) . unwrap ( ) ] ) ;
3745+ assert_eq ! (
3746+ super :: resolve_config_sources( & args, None , None )
3747+ . unwrap( )
3748+ . dir,
3749+ None ,
3750+ "a missing gateway.d is not an error"
3751+ ) ;
3752+ std:: fs:: create_dir ( tmp. path ( ) . join ( "gateway.d" ) ) . unwrap ( ) ;
3753+ assert_eq ! (
3754+ super :: resolve_config_sources( & args, None , None )
3755+ . unwrap( )
3756+ . dir,
3757+ Some ( tmp. path( ) . join( "gateway.d" ) )
3758+ ) ;
3759+ } ) ;
3760+ }
3761+
3762+ #[ test]
3763+ fn explicit_config_dir_overrides_the_implicit_directory ( ) {
3764+ let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
3765+ let other = tmp. path ( ) . join ( "other" ) ;
3766+ Environment :: new ( )
3767+ . remove ( "OPENSHELL_GATEWAY_CONFIG" )
3768+ . remove ( "OPENSHELL_GATEWAY_CONFIG_DIR" )
3769+ . run ( || {
3770+ let ( args, _) = parse_with_args ( & [
3771+ "openshell-gateway" ,
3772+ "--config-dir" ,
3773+ other. to_str ( ) . unwrap ( ) ,
3774+ ] ) ;
3775+ assert_eq ! (
3776+ super :: resolve_config_sources( & args, None , None )
3777+ . unwrap( )
3778+ . dir,
3779+ Some ( other. clone( ) )
3780+ ) ;
3781+ } ) ;
3782+ }
3783+
3784+ #[ test]
3785+ fn preflight_merges_fragments_and_names_a_bad_fragment ( ) {
3786+ let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
3787+ let base = tmp. path ( ) . join ( "gateway.toml" ) ;
3788+ std:: fs:: write ( & base, "[openshell]\n version = 2\n " ) . unwrap ( ) ;
3789+ let dir = tmp. path ( ) . join ( "gateway.d" ) ;
3790+ std:: fs:: create_dir ( & dir) . unwrap ( ) ;
3791+ std:: fs:: write (
3792+ dir. join ( "10-ok.toml" ) ,
3793+ "[openshell.gateway]\n log_level = 'debug'\n " ,
3794+ )
3795+ . unwrap ( ) ;
3796+ let xdg = tempfile:: tempdir ( ) . unwrap ( ) ;
3797+ Environment :: new ( )
3798+ . set ( "XDG_CONFIG_HOME" , xdg. path ( ) . to_str ( ) . unwrap ( ) )
3799+ . set ( "XDG_STATE_HOME" , xdg. path ( ) . to_str ( ) . unwrap ( ) )
3800+ . remove ( "OPENSHELL_GATEWAY_CONFIG" )
3801+ . remove ( "OPENSHELL_GATEWAY_CONFIG_DIR" )
3802+ . remove ( "OPENSHELL_COMPUTE_DRIVER" )
3803+ . remove ( "OPENSHELL_DRIVERS" )
3804+ . run ( || {
3805+ let args = || super :: ConfigPreflightArgs {
3806+ path : Some ( base. clone ( ) ) ,
3807+ ..Default :: default ( )
3808+ } ;
3809+ let ( run, matches) = parse_with_args ( & [ "openshell-gateway" , "--disable-tls" ] ) ;
3810+ super :: run_config_preflight ( args ( ) , run, & matches) . unwrap ( ) ;
3811+
3812+ std:: fs:: write (
3813+ dir. join ( "20-bad.toml" ) ,
3814+ "[openshell.gateway]\n not_a_field = 1\n " ,
3815+ )
3816+ . unwrap ( ) ;
3817+ let ( run, matches) = parse_with_args ( & [ "openshell-gateway" , "--disable-tls" ] ) ;
3818+ let error = super :: run_config_preflight ( args ( ) , run, & matches)
3819+ . unwrap_err ( )
3820+ . to_string ( ) ;
3821+ assert ! ( error. contains( "category=malformed" ) , "{error}" ) ;
3822+ assert ! ( error. contains( "20-bad.toml" ) , "{error}" ) ;
3823+ } ) ;
3824+ }
3825+
36613826 #[ test]
36623827 fn runtime_defaults_populate_database_url_from_xdg_state ( ) {
36633828 let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
@@ -3864,8 +4029,14 @@ require_client_auth = true
38644029 )
38654030 . unwrap ( ) ;
38664031 let ( preflight_args, preflight_matches) = parse_with_args ( & argv) ;
3867- super :: run_effective_config_preflight ( None , preflight_args, & preflight_matches, & registry)
3868- . expect ( "selected or candidate Podman preflight accepts the legacy alias" ) ;
4032+ super :: run_effective_config_preflight (
4033+ None ,
4034+ None ,
4035+ preflight_args,
4036+ & preflight_matches,
4037+ & registry,
4038+ )
4039+ . expect ( "selected or candidate Podman preflight accepts the legacy alias" ) ;
38694040 assert ! ( !state. path( ) . join( "network-supervisor" ) . exists( ) ) ;
38704041 let prepared = super :: prepare_server_config_with_drivers ( & mut run, & matches, & registry)
38714042 . expect ( "global network trust should normalize during startup preparation" ) ;
@@ -3946,7 +4117,7 @@ require_client_auth = true
39464117 argv. extend ( [ "--compute-driver" , "docker" ] ) ;
39474118 }
39484119 let ( run, matches) = parse_with_args ( & argv) ;
3949- super :: run_effective_config_preflight ( None , run, & matches, & registry)
4120+ super :: run_effective_config_preflight ( None , None , run, & matches, & registry)
39504121 . expect ( "inactive Podman settings must not affect preflight" ) ;
39514122 let ( mut run, matches) = parse_with_args ( & argv) ;
39524123 let prepared =
0 commit comments