Create config file on startup

This commit is contained in:
Antoine Gersant 2024-10-11 20:09:39 -07:00
parent 5f585a61d8
commit 142d400b8b

View file

@ -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::<storage::Config>(
&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::<storage::Config>(&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(())
}