Skip to content

Commit

Permalink
refactor: create toml config if if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Nov 17, 2024
1 parent 22ab2fc commit 46b17e7
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,15 @@ impl ConfigStorage for FileStorage {
) -> Result<()> {
let filepath = self.path.clone();
conf.validate()?;
if Path::new(&filepath).is_file() {
let path = Path::new(&filepath);
if !path.exists() && path.extension().unwrap_or_default() == "toml" {
fs::File::create(&path).await.map_err(|e| Error::Io {
source: e,
file: filepath.clone(),
})?;
}

if path.is_file() {
let ping_conf = toml::to_string_pretty(&conf)
.map_err(|e| Error::Ser { source: e })?;
let mut values: toml::Table = toml::from_str(&ping_conf)
Expand All @@ -115,11 +123,9 @@ impl ConfigStorage for FileStorage {
}
let ping_conf = toml::to_string_pretty(&values)
.map_err(|e| Error::Ser { source: e })?;
return fs::write(&filepath, ping_conf).await.map_err(|e| {
Error::Io {
source: e,
file: filepath,
}
return fs::write(path, ping_conf).await.map_err(|e| Error::Io {
source: e,
file: filepath,
});
}

Expand Down

0 comments on commit 46b17e7

Please sign in to comment.