@@ -16,6 +16,7 @@ use spin_factor_sqlite::runtime_config::spin as sqlite;
1616use spin_factor_sqlite:: SqliteFactor ;
1717use spin_factor_variables:: { spin_cli as variables, VariablesFactor } ;
1818use spin_factor_wasi:: WasiFactor ;
19+ use spin_factors:: runtime_config:: toml:: GetTomlValue as _;
1920use spin_factors:: {
2021 runtime_config:: toml:: TomlKeyTracker , FactorRuntimeConfigSource , RuntimeConfigSourceFinalizer ,
2122} ;
@@ -33,6 +34,10 @@ pub struct ResolvedRuntimeConfig<T> {
3334 pub key_value_resolver : key_value:: RuntimeConfigResolver ,
3435 /// The resolver used to resolve sqlite databases from runtime configuration.
3536 pub sqlite_resolver : sqlite:: RuntimeConfigResolver ,
37+ /// The fully resolved state directory.
38+ ///
39+ /// `None` is used for an "unset" state directory which each factor will treat differently.
40+ pub state_dir : Option < PathBuf > ,
3641}
3742
3843impl < T > ResolvedRuntimeConfig < T >
4146 for < ' a > <T as TryFrom < TomlRuntimeConfigSource < ' a > > >:: Error : Into < anyhow:: Error > ,
4247{
4348 /// Creates a new resolved runtime configuration from a runtime config source TOML file.
49+ ///
50+ /// `state_dir` is the explicitly provided state directory, if any. Some("") will be treated as
51+ /// as `None`.
4452 pub fn from_file (
4553 runtime_config_path : & Path ,
4654 state_dir : Option < & str > ,
@@ -64,21 +72,22 @@ where
6472 runtime_config_path. display( )
6573 )
6674 } ) ?;
67- let runtime_config : T = TomlRuntimeConfigSource :: new (
75+ let source = TomlRuntimeConfigSource :: new (
6876 & toml,
69- state_dir. unwrap_or ( DEFAULT_STATE_DIR ) . into ( ) ,
77+ state_dir,
7078 & key_value_config_resolver,
7179 & tls_resolver,
7280 & sqlite_config_resolver,
7381 use_gpu,
74- )
75- . try_into ( )
76- . map_err ( Into :: into) ?;
82+ ) ;
83+ let state_dir = source . state_dir ( ) ;
84+ let runtime_config : T = source . try_into ( ) . map_err ( Into :: into) ?;
7785
7886 Ok ( Self {
7987 runtime_config,
8088 key_value_resolver : key_value_config_resolver,
8189 sqlite_resolver : sqlite_config_resolver,
90+ state_dir,
8291 } )
8392 }
8493
@@ -102,24 +111,32 @@ where
102111 }
103112 Ok ( ( ) )
104113 }
114+
115+ /// The fully resolved state directory.
116+ pub fn state_dir ( & self ) -> Option < PathBuf > {
117+ self . state_dir . clone ( )
118+ }
105119}
106120
107121impl < T : Default > ResolvedRuntimeConfig < T > {
122+ /// Creates a new resolved runtime configuration with default values.
108123 pub fn default ( state_dir : Option < & str > ) -> Self {
109124 let state_dir = state_dir. map ( PathBuf :: from) ;
110125 Self {
111126 sqlite_resolver : sqlite_config_resolver ( state_dir. clone ( ) )
112127 . expect ( "failed to resolve sqlite runtime config" ) ,
113- key_value_resolver : key_value_config_resolver ( state_dir) ,
128+ key_value_resolver : key_value_config_resolver ( state_dir. clone ( ) ) ,
114129 runtime_config : Default :: default ( ) ,
130+ state_dir,
115131 }
116132 }
117133}
118134
119135/// The TOML based runtime configuration source Spin CLI.
120136pub struct TomlRuntimeConfigSource < ' a > {
121137 table : TomlKeyTracker < ' a > ,
122- state_dir : PathBuf ,
138+ /// Explicitly provided state directory.
139+ state_dir : Option < & ' a str > ,
123140 key_value : & ' a key_value:: RuntimeConfigResolver ,
124141 tls : & ' a SpinTlsRuntimeConfig ,
125142 sqlite : & ' a sqlite:: RuntimeConfigResolver ,
@@ -129,7 +146,7 @@ pub struct TomlRuntimeConfigSource<'a> {
129146impl < ' a > TomlRuntimeConfigSource < ' a > {
130147 pub fn new (
131148 table : & ' a toml:: Table ,
132- state_dir : PathBuf ,
149+ state_dir : Option < & ' a str > ,
133150 key_value : & ' a key_value:: RuntimeConfigResolver ,
134151 tls : & ' a SpinTlsRuntimeConfig ,
135152 sqlite : & ' a sqlite:: RuntimeConfigResolver ,
@@ -144,6 +161,17 @@ impl<'a> TomlRuntimeConfigSource<'a> {
144161 use_gpu,
145162 }
146163 }
164+
165+ /// Get the configured state_directory.
166+ pub fn state_dir ( & self ) -> Option < PathBuf > {
167+ let from_toml = || self . table . get ( "state_dir" ) . and_then ( |v| v. as_str ( ) ) ;
168+ // Prefer explicitly provided state directory, then take from toml.
169+ self . state_dir
170+ . or_else ( from_toml)
171+ // Treat "" as None.
172+ . filter ( |s| !s. is_empty ( ) )
173+ . map ( PathBuf :: from)
174+ }
147175}
148176
149177impl FactorRuntimeConfigSource < KeyValueFactor > for TomlRuntimeConfigSource < ' _ > {
@@ -187,7 +215,7 @@ impl FactorRuntimeConfigSource<OutboundMysqlFactor> for TomlRuntimeConfigSource<
187215
188216impl FactorRuntimeConfigSource < LlmFactor > for TomlRuntimeConfigSource < ' _ > {
189217 fn get_runtime_config ( & mut self ) -> anyhow:: Result < Option < spin_factor_llm:: RuntimeConfig > > {
190- llm:: runtime_config_from_toml ( self . table . as_ref ( ) , self . state_dir . clone ( ) , self . use_gpu )
218+ llm:: runtime_config_from_toml ( self . table . as_ref ( ) , self . state_dir ( ) , self . use_gpu )
191219 }
192220}
193221
0 commit comments