Reduce duplication.

This commit is contained in:
R-VdP 2023-03-16 12:05:13 +01:00
parent f569c59eee
commit bacaae7878
No known key found for this signature in database

View file

@ -85,16 +85,7 @@ pub fn activate(store_path: &StorePath, ephemeral: bool) -> Result<()> {
status?; status?;
let new_state = create_etc_links(config.entries.values(), &etc_dir, state, &old_state) let new_state = create_etc_links(config.entries.values(), &etc_dir, state, &old_state)
.update_state(old_state, &|path, status| { .update_state(old_state, &try_delete_path);
log::debug!("Deactivating: {}", path.display());
try_delete_path(path, status)
.map_err(|e| {
log::error!("Error deleting path: {}", path.display());
log::error!("{e}");
e
})
.is_ok()
});
serialise_state(new_state)?; serialise_state(new_state)?;
@ -106,22 +97,14 @@ pub fn deactivate() -> Result<()> {
let state = read_created_files()?; let state = read_created_files()?;
log::debug!("{:?}", state); log::debug!("{:?}", state);
serialise_state(state.deactivate(&|path, status| { serialise_state(state.deactivate(&try_delete_path))?;
log::debug!("Deactivating: {}", path.display());
try_delete_path(path, status)
.map_err(|e| {
log::error!("Error deleting path: {}", path.display());
log::error!("{e}");
e
})
.is_ok()
}))?;
log::info!("Done"); log::info!("Done");
Ok(()) Ok(())
} }
fn try_delete_path(path: &Path, status: &EtcFileStatus) -> Result<()> { fn try_delete_path(path: &Path, status: &EtcFileStatus) -> bool {
fn do_try_delete(path: &Path, status: &EtcFileStatus) -> Result<()> {
// exists() returns false for broken symlinks // exists() returns false for broken symlinks
if path.exists() || path.is_symlink() { if path.exists() || path.is_symlink() {
if path.is_symlink() { if path.is_symlink() {
@ -138,13 +121,23 @@ fn try_delete_path(path: &Path, status: &EtcFileStatus) -> Result<()> {
Ok(()) Ok(())
} }
} else { } else {
Err(anyhow!("Unsupported file type! {}", path.display())) anyhow::bail!("Unsupported file type! {}", path.display())
} }
} else { } else {
Ok(()) Ok(())
} }
} }
log::debug!("Deactivating: {}", path.display());
do_try_delete(path, status)
.map_err(|e| {
log::error!("Error deleting path: {}", path.display());
log::error!("{e}");
e
})
.is_ok()
}
fn create_etc_links<'a, E>( fn create_etc_links<'a, E>(
entries: E, entries: E,
etc_dir: &Path, etc_dir: &Path,