@@ -16,6 +16,7 @@ use spin_factor_sqlite::runtime_config::spin as sqlite;
16
16
use spin_factor_sqlite:: SqliteFactor ;
17
17
use spin_factor_variables:: { spin_cli as variables, VariablesFactor } ;
18
18
use spin_factor_wasi:: WasiFactor ;
19
+ use spin_factors:: runtime_config:: toml:: GetTomlValue as _;
19
20
use spin_factors:: {
20
21
runtime_config:: toml:: TomlKeyTracker , FactorRuntimeConfigSource , RuntimeConfigSourceFinalizer ,
21
22
} ;
@@ -33,6 +34,10 @@ pub struct ResolvedRuntimeConfig<T> {
33
34
pub key_value_resolver : key_value:: RuntimeConfigResolver ,
34
35
/// The resolver used to resolve sqlite databases from runtime configuration.
35
36
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 > ,
36
41
}
37
42
38
43
impl < T > ResolvedRuntimeConfig < T >
41
46
for < ' a > <T as TryFrom < TomlRuntimeConfigSource < ' a > > >:: Error : Into < anyhow:: Error > ,
42
47
{
43
48
/// 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`.
44
52
pub fn from_file (
45
53
runtime_config_path : & Path ,
46
54
state_dir : Option < & str > ,
@@ -64,21 +72,22 @@ where
64
72
runtime_config_path. display( )
65
73
)
66
74
} ) ?;
67
- let runtime_config : T = TomlRuntimeConfigSource :: new (
75
+ let source = TomlRuntimeConfigSource :: new (
68
76
& toml,
69
- state_dir. unwrap_or ( DEFAULT_STATE_DIR ) . into ( ) ,
77
+ state_dir,
70
78
& key_value_config_resolver,
71
79
& tls_resolver,
72
80
& sqlite_config_resolver,
73
81
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) ?;
77
85
78
86
Ok ( Self {
79
87
runtime_config,
80
88
key_value_resolver : key_value_config_resolver,
81
89
sqlite_resolver : sqlite_config_resolver,
90
+ state_dir,
82
91
} )
83
92
}
84
93
@@ -102,24 +111,32 @@ where
102
111
}
103
112
Ok ( ( ) )
104
113
}
114
+
115
+ /// The fully resolved state directory.
116
+ pub fn state_dir ( & self ) -> Option < PathBuf > {
117
+ self . state_dir . clone ( )
118
+ }
105
119
}
106
120
107
121
impl < T : Default > ResolvedRuntimeConfig < T > {
122
+ /// Creates a new resolved runtime configuration with default values.
108
123
pub fn default ( state_dir : Option < & str > ) -> Self {
109
124
let state_dir = state_dir. map ( PathBuf :: from) ;
110
125
Self {
111
126
sqlite_resolver : sqlite_config_resolver ( state_dir. clone ( ) )
112
127
. 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 ( ) ) ,
114
129
runtime_config : Default :: default ( ) ,
130
+ state_dir,
115
131
}
116
132
}
117
133
}
118
134
119
135
/// The TOML based runtime configuration source Spin CLI.
120
136
pub struct TomlRuntimeConfigSource < ' a > {
121
137
table : TomlKeyTracker < ' a > ,
122
- state_dir : PathBuf ,
138
+ /// Explicitly provided state directory.
139
+ state_dir : Option < & ' a str > ,
123
140
key_value : & ' a key_value:: RuntimeConfigResolver ,
124
141
tls : & ' a SpinTlsRuntimeConfig ,
125
142
sqlite : & ' a sqlite:: RuntimeConfigResolver ,
@@ -129,7 +146,7 @@ pub struct TomlRuntimeConfigSource<'a> {
129
146
impl < ' a > TomlRuntimeConfigSource < ' a > {
130
147
pub fn new (
131
148
table : & ' a toml:: Table ,
132
- state_dir : PathBuf ,
149
+ state_dir : Option < & ' a str > ,
133
150
key_value : & ' a key_value:: RuntimeConfigResolver ,
134
151
tls : & ' a SpinTlsRuntimeConfig ,
135
152
sqlite : & ' a sqlite:: RuntimeConfigResolver ,
@@ -144,6 +161,17 @@ impl<'a> TomlRuntimeConfigSource<'a> {
144
161
use_gpu,
145
162
}
146
163
}
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
+ }
147
175
}
148
176
149
177
impl FactorRuntimeConfigSource < KeyValueFactor > for TomlRuntimeConfigSource < ' _ > {
@@ -187,7 +215,7 @@ impl FactorRuntimeConfigSource<OutboundMysqlFactor> for TomlRuntimeConfigSource<
187
215
188
216
impl FactorRuntimeConfigSource < LlmFactor > for TomlRuntimeConfigSource < ' _ > {
189
217
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 )
191
219
}
192
220
}
193
221
0 commit comments