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?;
let new_state = create_etc_links(config.entries.values(), &etc_dir, state, &old_state)
.update_state(old_state, &|path, status| {
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()
});
.update_state(old_state, &try_delete_path);
serialise_state(new_state)?;
@ -106,22 +97,14 @@ pub fn deactivate() -> Result<()> {
let state = read_created_files()?;
log::debug!("{:?}", state);
serialise_state(state.deactivate(&|path, status| {
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(state.deactivate(&try_delete_path))?;
log::info!("Done");
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
if path.exists() || path.is_symlink() {
if path.is_symlink() {
@ -138,13 +121,23 @@ fn try_delete_path(path: &Path, status: &EtcFileStatus) -> Result<()> {
Ok(())
}
} else {
Err(anyhow!("Unsupported file type! {}", path.display()))
anyhow::bail!("Unsupported file type! {}", path.display())
}
} else {
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>(
entries: E,
etc_dir: &Path,