Fix issue with empty silo dirs being created on apply

main
trivernis 9 months ago
parent 12ee1b86bc
commit 7e24a411df
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG Key ID: 7E6D18B61C8D2F4B

@ -42,9 +42,7 @@ impl SiloRepo {
}
pub fn apply(&self) -> Result<()> {
let cwd = env::current_dir()
.into_diagnostic()
.context("get current dir")?;
let cwd = dirs::home_dir().unwrap_or(env::current_dir().into_diagnostic()?);
let ctx = ApplyContext {
config: self.config.clone(),
};
@ -141,16 +139,14 @@ impl DirEntry {
fn apply(&self, ctx: &ApplyContext, cwd: &Path) -> Result<()> {
match self {
DirEntry::File(file) => file.apply(ctx, cwd),
DirEntry::File(file) => {
ensure_cwd(cwd)?;
file.apply(ctx, cwd)
}
DirEntry::Dir(p, children) => {
let cwd = if p != cwd {
let cwd = cwd.join(p.file_name().unwrap());
if !cwd.exists() {
log::info!("Creating {cwd:?}");
fs::create_dir_all(&cwd)
.into_diagnostic()
.with_context(|| format!("Creating directory {cwd:?}"))?;
}
ensure_cwd(&cwd)?;
cwd
} else {
p.to_owned()
@ -162,15 +158,8 @@ impl DirEntry {
}
DirEntry::Root(_, data, children) => {
let rendered_path = templating::render(&data.path, &ctx.config.template_context)?;
let cwd = PathBuf::from(rendered_path);
if !cwd.exists() {
log::info!("Creating {cwd:?}");
fs::create_dir_all(&cwd)
.into_diagnostic()
.with_context(|| format!("Creating directory {cwd:?}"))?;
}
for child in children {
child.apply(ctx, &cwd)?;
}
@ -303,3 +292,13 @@ fn confirm_write(diff_tool: &str, a: &Path, b: &Path) -> Result<bool> {
.interact()
.into_diagnostic()
}
fn ensure_cwd(cwd: &Path) -> Result<(), miette::ErrReport> {
if cwd.exists() {
return Ok(());
}
log::info!("Creating {cwd:?}");
fs::create_dir_all(&cwd)
.into_diagnostic()
.with_context(|| format!("Creating directory {cwd:?}"))
}

Loading…
Cancel
Save