improve command running

This commit is contained in:
timeshifter
2023-01-14 17:40:49 +01:00
parent a5419d02ec
commit b0460b2c2d
4 changed files with 57 additions and 40 deletions
+30 -8
View File
@@ -1,3 +1,5 @@
use anyhow::{bail, ensure, Context, Result};
use super::Backend;
use crate::Package;
@@ -12,10 +14,6 @@ impl ToDoPerBackend {
self.0.push(item);
}
pub(crate) fn into_iter(self) -> impl Iterator<Item = (Box<dyn Backend>, Vec<Package>)> {
self.0.into_iter()
}
pub(crate) fn iter(&self) -> impl Iterator<Item = &(Box<dyn Backend>, Vec<Package>)> {
self.0.iter()
}
@@ -24,10 +22,34 @@ impl ToDoPerBackend {
self.0.iter().all(|(_, diff)| diff.is_empty())
}
pub(crate) fn install_missing_packages(&self) {
self.0
.iter()
.for_each(|(backend, diff)| backend.install_packages(diff));
// TODO try to combine these methods into one
pub(crate) fn install_missing_packages(&self) -> Result<()> {
for (backend, packages) in &self.0 {
let exit_status = backend
.install_packages(packages)
.with_context(|| format!("installing packages for {}", backend.get_binary()))?;
match exit_status.code() {
Some(val) => ensure!(val == 0, "command returned with exit code {val}"),
None => bail!("could not install packages for {}", backend.get_binary()),
}
}
Ok(())
}
// TODO this one
pub(crate) fn remove_unmanaged_packages(&self) -> Result<()> {
for (backend, packages) in &self.0 {
let exit_status = backend
.remove_packages(packages)
.with_context(|| format!("removing packages for {}", backend.get_binary()))?;
match exit_status.code() {
Some(val) => ensure!(val == 0, "command returned with exit code {val}"),
None => bail!("could not remove packages for {}", backend.get_binary()),
}
}
Ok(())
}
pub(crate) fn is_empty(&self) -> bool {