@@ -52,20 +52,23 @@ where
52
52
/// Creates a new resolved runtime configuration from an optional runtime config source TOML file.
53
53
pub fn from_optional_file (
54
54
runtime_config_path : Option < & Path > ,
55
- provided_state_dir : Option < & Path > ,
56
- provided_log_dir : LogDir ,
55
+ local_app_dir : Option < PathBuf > ,
56
+ provided_state_dir : UserProvidedPath ,
57
+ provided_log_dir : UserProvidedPath ,
57
58
use_gpu : bool ,
58
59
) -> anyhow:: Result < Self > {
59
60
match runtime_config_path {
60
61
Some ( runtime_config_path) => Self :: from_file (
61
62
runtime_config_path,
63
+ local_app_dir,
62
64
provided_state_dir,
63
65
provided_log_dir,
64
66
use_gpu,
65
67
) ,
66
68
None => Self :: new (
67
69
Default :: default ( ) ,
68
70
None ,
71
+ local_app_dir,
69
72
provided_state_dir,
70
73
provided_log_dir,
71
74
use_gpu,
78
81
/// `provided_state_dir` is the explicitly provided state directory, if any.
79
82
pub fn from_file (
80
83
runtime_config_path : & Path ,
81
- provided_state_dir : Option < & Path > ,
82
- provided_log_dir : LogDir ,
84
+ local_app_dir : Option < PathBuf > ,
85
+ provided_state_dir : UserProvidedPath ,
86
+ provided_log_dir : UserProvidedPath ,
83
87
use_gpu : bool ,
84
88
) -> anyhow:: Result < Self > {
85
89
let file = std:: fs:: read_to_string ( runtime_config_path) . with_context ( || {
98
102
Self :: new (
99
103
toml,
100
104
Some ( runtime_config_path) ,
105
+ local_app_dir,
101
106
provided_state_dir,
102
107
provided_log_dir,
103
108
use_gpu,
@@ -108,11 +113,13 @@ where
108
113
pub fn new (
109
114
toml : toml:: Table ,
110
115
runtime_config_path : Option < & Path > ,
111
- provided_state_dir : Option < & Path > ,
112
- provided_log_dir : LogDir ,
116
+ local_app_dir : Option < PathBuf > ,
117
+ provided_state_dir : UserProvidedPath ,
118
+ provided_log_dir : UserProvidedPath ,
113
119
use_gpu : bool ,
114
120
) -> anyhow:: Result < Self > {
115
- let toml_resolver = TomlResolver :: new ( & toml, provided_state_dir, provided_log_dir) ;
121
+ let toml_resolver =
122
+ TomlResolver :: new ( & toml, local_app_dir, provided_state_dir, provided_log_dir) ;
116
123
let tls_resolver = runtime_config_path. map ( SpinTlsRuntimeConfig :: new) ;
117
124
let key_value_config_resolver = key_value_config_resolver ( toml_resolver. state_dir ( ) ?) ;
118
125
let sqlite_config_resolver = sqlite_config_resolver ( toml_resolver. state_dir ( ) ?)
@@ -178,19 +185,25 @@ where
178
185
/// Resolves runtime configuration from a TOML file.
179
186
pub struct TomlResolver < ' a > {
180
187
table : TomlKeyTracker < ' a > ,
188
+ /// The local app directory.
189
+ local_app_dir : Option < PathBuf > ,
181
190
/// Explicitly provided state directory.
182
- state_dir : Option < & ' a Path > ,
191
+ state_dir : UserProvidedPath ,
183
192
/// Explicitly provided log directory.
184
- log_dir : LogDir ,
193
+ log_dir : UserProvidedPath ,
185
194
}
186
195
187
196
impl < ' a > TomlResolver < ' a > {
188
197
/// Create a new TOML resolver.
189
- ///
190
- /// The `state_dir` is the explicitly provided state directory, if any.
191
- pub fn new ( table : & ' a toml:: Table , state_dir : Option < & ' a Path > , log_dir : LogDir ) -> Self {
198
+ pub fn new (
199
+ table : & ' a toml:: Table ,
200
+ local_app_dir : Option < PathBuf > ,
201
+ state_dir : UserProvidedPath ,
202
+ log_dir : UserProvidedPath ,
203
+ ) -> Self {
192
204
Self {
193
205
table : TomlKeyTracker :: new ( table) ,
206
+ local_app_dir,
194
207
state_dir,
195
208
log_dir,
196
209
}
@@ -200,28 +213,63 @@ impl<'a> TomlResolver<'a> {
200
213
///
201
214
/// Errors if the path cannot be converted to an absolute path.
202
215
pub fn state_dir ( & self ) -> std:: io:: Result < Option < PathBuf > > {
203
- let from_toml = || {
204
- self . table
205
- . get ( "state_dir" )
206
- . and_then ( |v| v. as_str ( ) )
207
- . filter ( |v| !v. is_empty ( ) )
208
- . map ( Path :: new)
209
- } ;
210
- // Prefer explicitly provided state directory, then take from toml.
211
- self . state_dir
212
- . or_else ( from_toml)
213
- . map ( std:: path:: absolute)
214
- . transpose ( )
216
+ let mut state_dir = self . state_dir . clone ( ) ;
217
+ // If the state_dir is not explicitly provided, check the toml.
218
+ if matches ! ( state_dir, UserProvidedPath :: Default ) {
219
+ let from_toml =
220
+ self . table
221
+ . get ( "state_dir" )
222
+ . and_then ( |v| v. as_str ( ) )
223
+ . map ( |toml_value| {
224
+ if toml_value. is_empty ( ) {
225
+ // If the toml value is empty, treat it as unset.
226
+ UserProvidedPath :: Unset
227
+ } else {
228
+ // Otherwise, treat the toml value as a provided path.
229
+ UserProvidedPath :: Provided ( PathBuf :: from ( toml_value) )
230
+ }
231
+ } ) ;
232
+ // If toml value is not provided, use the original value after all.
233
+ state_dir = from_toml. unwrap_or ( state_dir) ;
234
+ }
235
+
236
+ match ( state_dir, & self . local_app_dir ) {
237
+ ( UserProvidedPath :: Provided ( p) , _) => Ok ( Some ( std:: path:: absolute ( p) ?) ) ,
238
+ ( UserProvidedPath :: Default , Some ( local_app_dir) ) => {
239
+ Ok ( Some ( local_app_dir. join ( ".spin" ) ) )
240
+ }
241
+ ( UserProvidedPath :: Default | UserProvidedPath :: Unset , _) => Ok ( None ) ,
242
+ }
215
243
}
216
244
217
245
/// Get the configured log directory.
218
246
///
219
247
/// Errors if the path cannot be converted to an absolute path.
220
248
pub fn log_dir ( & self ) -> std:: io:: Result < Option < PathBuf > > {
221
- match & self . log_dir {
222
- LogDir :: Provided ( p) => Ok ( Some ( std:: path:: absolute ( p) ?) ) ,
223
- LogDir :: Default => Ok ( self . state_dir ( ) ?. map ( |p| p. join ( "logs" ) ) ) ,
224
- LogDir :: None => Ok ( None ) ,
249
+ let mut log_dir = self . log_dir . clone ( ) ;
250
+ // If the log_dir is not explicitly provided, check the toml.
251
+ if matches ! ( log_dir, UserProvidedPath :: Default ) {
252
+ let from_toml = self
253
+ . table
254
+ . get ( "log_dir" )
255
+ . and_then ( |v| v. as_str ( ) )
256
+ . map ( |toml_value| {
257
+ if toml_value. is_empty ( ) {
258
+ // If the toml value is empty, treat it as unset.
259
+ UserProvidedPath :: Unset
260
+ } else {
261
+ // Otherwise, treat the toml value as a provided path.
262
+ UserProvidedPath :: Provided ( PathBuf :: from ( toml_value) )
263
+ }
264
+ } ) ;
265
+ // If toml value is not provided, use the original value after all.
266
+ log_dir = from_toml. unwrap_or ( log_dir) ;
267
+ }
268
+
269
+ match log_dir {
270
+ UserProvidedPath :: Provided ( p) => Ok ( Some ( std:: path:: absolute ( p) ?) ) ,
271
+ UserProvidedPath :: Default => Ok ( self . state_dir ( ) ?. map ( |p| p. join ( "logs" ) ) ) ,
272
+ UserProvidedPath :: Unset => Ok ( None ) ,
225
273
}
226
274
}
227
275
@@ -397,13 +445,13 @@ fn sqlite_config_resolver(
397
445
) )
398
446
}
399
447
400
- /// The log directory for the trigger .
448
+ /// A user provided option which be either be provided, default, or explicitly none .
401
449
#[ derive( Clone , Debug ) ]
402
- pub enum LogDir {
403
- /// Use the explicitly provided log directory.
450
+ pub enum UserProvidedPath {
451
+ /// Use the explicitly provided directory.
404
452
Provided ( PathBuf ) ,
405
- /// Use the default log directory .
453
+ /// Use the default.
406
454
Default ,
407
- /// Do not log .
408
- None ,
455
+ /// Explicitly unset .
456
+ Unset ,
409
457
}
0 commit comments