From 142d400b8bb977177b01d18b9a15fb5888760cbd Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Fri, 11 Oct 2024 20:09:39 -0700 Subject: [PATCH] Create config file on startup --- src/app/config.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/app/config.rs b/src/app/config.rs index 6d41370..d9c5d3b 100644 --- a/src/app/config.rs +++ b/src/app/config.rs @@ -96,9 +96,27 @@ impl Manager { auth_secret, }; + if !tokio::fs::try_exists(config_file_path) + .await + .map_err(|e| Error::Io(config_file_path.to_owned(), e))? + { + manager.save_config().await?; + } + Ok(manager) } + async fn save_config(&self) -> Result<(), Error> { + let serialized = toml::ser::to_string_pretty::( + &self.config.read().await.clone().into(), + ) + .map_err(Error::ConfigSerialization)?; + tokio::fs::write(&self.config_file_path, serialized.as_bytes()) + .await + .map_err(|e| Error::Io(self.config_file_path.clone(), e))?; + Ok(()) + } + #[cfg(test)] pub async fn apply(&self, config: storage::Config) -> Result<(), Error> { self.mutate_fallible(|c| { @@ -122,11 +140,7 @@ impl Manager { ) -> Result<(), Error> { let mut config = self.config.write().await; op(&mut config)?; - let serialized = toml::ser::to_string_pretty::(&config.clone().into()) - .map_err(Error::ConfigSerialization)?; - tokio::fs::write(&self.config_file_path, serialized.as_bytes()) - .await - .map_err(|e| Error::Io(self.config_file_path.clone(), e))?; + self.save_config().await?; Ok(()) }