Skip to content

Commit

Permalink
Create config file on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
agersant committed Oct 12, 2024
1 parent 5f585a6 commit 142d400
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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(())
}

Expand Down

0 comments on commit 142d400

Please sign in to comment.