refactor todo_per_backend
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
use std::process::ExitStatus;
|
||||||
|
|
||||||
use anyhow::{bail, ensure, Context, Result};
|
use anyhow::{bail, ensure, Context, Result};
|
||||||
|
|
||||||
use super::Backend;
|
use super::Backend;
|
||||||
@@ -22,31 +24,31 @@ impl ToDoPerBackend {
|
|||||||
self.0.iter().all(|(_, diff)| diff.is_empty())
|
self.0.iter().all(|(_, diff)| diff.is_empty())
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO try to combine these methods into one
|
|
||||||
pub(crate) fn install_missing_packages(&self) -> Result<()> {
|
pub(crate) fn install_missing_packages(&self) -> Result<()> {
|
||||||
for (backend, packages) in &self.0 {
|
self.handle_backend_command(Backend::install_packages, "install", "installing")
|
||||||
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<()> {
|
pub(crate) fn remove_unmanaged_packages(&self) -> Result<()> {
|
||||||
|
self.handle_backend_command(Backend::remove_packages, "remove", "removing")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_backend_command<'a, F>(
|
||||||
|
&'a self,
|
||||||
|
func: F,
|
||||||
|
verb: &'static str,
|
||||||
|
verb_continuous: &'static str,
|
||||||
|
) -> Result<()>
|
||||||
|
where
|
||||||
|
F: Fn(&'a dyn Backend, &'a [Package]) -> Result<ExitStatus>,
|
||||||
|
{
|
||||||
for (backend, packages) in &self.0 {
|
for (backend, packages) in &self.0 {
|
||||||
let exit_status = backend
|
let exit_status = func(&**backend, packages).with_context(|| {
|
||||||
.remove_packages(packages)
|
format!("{verb_continuous} packages for {}", backend.get_binary())
|
||||||
.with_context(|| format!("removing packages for {}", backend.get_binary()))?;
|
})?;
|
||||||
|
|
||||||
match exit_status.code() {
|
match exit_status.code() {
|
||||||
Some(val) => ensure!(val == 0, "command returned with exit code {val}"),
|
Some(val) => ensure!(val == 0, "command returned with exit code {val}"),
|
||||||
None => bail!("could not remove packages for {}", backend.get_binary()),
|
None => bail!("could not {verb} packages for {}", backend.get_binary()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
+15
-9
@@ -61,14 +61,7 @@ impl Pacdef {
|
|||||||
let mut to_install = ToDoPerBackend::new();
|
let mut to_install = ToDoPerBackend::new();
|
||||||
|
|
||||||
for backend in Backends::iter() {
|
for backend in Backends::iter() {
|
||||||
let mut backend = if backend.get_section() == "pacman" {
|
let mut backend = self.overwrite_binary_from_config(backend);
|
||||||
Box::new(crate::backend::Pacman {
|
|
||||||
binary: self.config.aur_helper.clone(),
|
|
||||||
packages: HashSet::new(),
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
backend
|
|
||||||
};
|
|
||||||
|
|
||||||
backend.load(&self.groups);
|
backend.load(&self.groups);
|
||||||
|
|
||||||
@@ -81,6 +74,17 @@ impl Pacdef {
|
|||||||
to_install
|
to_install
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn overwrite_binary_from_config(&self, backend: Box<dyn Backend>) -> Box<dyn Backend> {
|
||||||
|
if backend.get_section() == "pacman" {
|
||||||
|
Box::new(crate::backend::Pacman {
|
||||||
|
binary: self.config.aur_helper.clone(),
|
||||||
|
packages: HashSet::new(),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
backend
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn install_packages(&self) -> Result<()> {
|
fn install_packages(&self) -> Result<()> {
|
||||||
let to_install = self.get_missing_packages();
|
let to_install = self.get_missing_packages();
|
||||||
|
|
||||||
@@ -140,7 +144,9 @@ impl Pacdef {
|
|||||||
fn get_unmanaged_packages(self) -> ToDoPerBackend {
|
fn get_unmanaged_packages(self) -> ToDoPerBackend {
|
||||||
let mut result = ToDoPerBackend::new();
|
let mut result = ToDoPerBackend::new();
|
||||||
|
|
||||||
for mut backend in Backends::iter() {
|
for backend in Backends::iter() {
|
||||||
|
let mut backend = self.overwrite_binary_from_config(backend);
|
||||||
|
|
||||||
backend.load(&self.groups);
|
backend.load(&self.groups);
|
||||||
|
|
||||||
match backend.get_unmanaged_packages_sorted() {
|
match backend.get_unmanaged_packages_sorted() {
|
||||||
|
|||||||
Reference in New Issue
Block a user