From 9c512073b8662c32a0397a867279c25af30a0676 Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Wed, 15 Feb 2023 15:52:56 +0100 Subject: [PATCH] handle non-existing pacdef-related dirs --- crates/pacdef_core/src/config.rs | 7 +++++++ crates/pacdef_core/src/grouping/group.rs | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/crates/pacdef_core/src/config.rs b/crates/pacdef_core/src/config.rs index 1f292f7..c9b09c8 100644 --- a/crates/pacdef_core/src/config.rs +++ b/crates/pacdef_core/src/config.rs @@ -46,6 +46,13 @@ impl Config { let result = Self::default(); let content = serde_yaml::to_string(&result).context("converting Config to yaml")?; + + let parent = file.parent().context("getting parent of config dir")?; + if !parent.is_dir() { + std::fs::create_dir_all(parent) + .with_context(|| format!("creating dir {}", parent.to_string_lossy()))?; + } + let mut output = File::create(file).context("creating default config file")?; write!(output, "{content}").context("writing default config")?; diff --git a/crates/pacdef_core/src/grouping/group.rs b/crates/pacdef_core/src/grouping/group.rs index ade08b7..abee621 100644 --- a/crates/pacdef_core/src/grouping/group.rs +++ b/crates/pacdef_core/src/grouping/group.rs @@ -29,6 +29,13 @@ impl Group { pub fn load(group_dir: &Path, warn_not_symlinks: bool) -> Result> { let mut result = HashSet::new(); + if !group_dir.is_dir() { + // we only need to create the innermost dir. The rest was already created from when + // we loaded the config + std::fs::create_dir(group_dir) + .with_context(|| format!("creating group dir {}", group_dir.to_string_lossy()))?; + } + for entry in group_dir.read_dir().context("reading group dir")? { let file = entry.context("getting group file")?; let path = file.path(); @@ -46,6 +53,10 @@ impl Group { result.insert(group); } + if result.is_empty() { + eprintln!("WARNING: no group files found"); + } + Ok(result) } }