diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 3889e0d..cd31145 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -1,6 +1,8 @@ mod pacman; use std::collections::HashSet; +use std::os::unix::process::CommandExt; +use std::process::Command; use crate::Package; @@ -9,12 +11,36 @@ pub use pacman::Pacman; pub trait Backend { /// The binary that should be called to run the associated package manager. const BINARY: &'static str; + + /// The switches that signals the `BINARY` that the packages should be installed. + const SWITCH_INSTALL: &'static str; + + /// The switches that signals the `BINARY` that the packages should be removed. + const SWITCH_REMOVE: &'static str; + /// Get all packages that are installed in the system. fn get_all_installed_packages() -> HashSet; + /// Get all packages that were installed in the system explicitly. fn get_explicitly_installed_packages() -> HashSet; + /// Install the specified packages. - fn install_packages(packages: Vec); + fn install_packages(packages: Vec) { + let mut cmd = Command::new(Self::BINARY); + cmd.arg(Self::SWITCH_INSTALL); + for p in packages { + cmd.arg(format!("{p}")); + } + cmd.exec(); + } + /// Remove the specified packages. - fn remove_packages(packages: Vec); + fn remove_packages(packages: Vec) { + let mut cmd = Command::new(Self::BINARY); + cmd.arg(Self::SWITCH_REMOVE); + for p in packages { + cmd.arg(format!("{p}")); + } + cmd.exec(); + } } diff --git a/src/backend/pacman.rs b/src/backend/pacman.rs index 7d8d15a..0958fd4 100644 --- a/src/backend/pacman.rs +++ b/src/backend/pacman.rs @@ -1,6 +1,4 @@ use std::collections::HashSet; -use std::os::unix::process::CommandExt; -use std::process::Command; use alpm::Alpm; use alpm::PackageReason::Explicit; @@ -12,6 +10,8 @@ pub struct Pacman; impl Backend for Pacman { const BINARY: &'static str = "paru"; + const SWITCH_INSTALL: &'static str = "-S"; + const SWITCH_REMOVE: &'static str = "-Rsn"; fn get_all_installed_packages() -> HashSet { convert_to_pacdef_packages(get_all_installed_packages_from_alpm()) @@ -20,24 +20,6 @@ impl Backend for Pacman { fn get_explicitly_installed_packages() -> HashSet { convert_to_pacdef_packages(get_explicitly_installed_packages_from_alpm()) } - - fn install_packages(packages: Vec) { - let mut cmd = Command::new("paru"); - cmd.arg("-S"); - for p in packages { - cmd.arg(format!("{p}")); - } - cmd.exec(); - } - - fn remove_packages(packages: Vec) { - let mut cmd = Command::new("paru"); - cmd.arg("-Rsn"); - for p in packages { - cmd.arg(format!("{p}")); - } - cmd.exec(); - } } fn get_all_installed_packages_from_alpm() -> HashSet {