From 2f511cf87cf0c62ef4666fb4ec46e32c8a776714 Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Thu, 5 Jan 2023 16:44:41 +0100 Subject: [PATCH] add install and remove packages to Backend trait --- src/backend/mod.rs | 6 ++++++ src/backend/pacman.rs | 22 ++++++++++++++++++++++ src/cmd.rs | 20 -------------------- src/core.rs | 7 +++---- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 9b884b0..3889e0d 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -7,8 +7,14 @@ use crate::Package; pub use pacman::Pacman; pub trait Backend { + /// The binary that should be called to run the associated package manager. + const BINARY: &'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); + /// Remove the specified packages. + fn remove_packages(packages: Vec); } diff --git a/src/backend/pacman.rs b/src/backend/pacman.rs index 756e6ff..7d8d15a 100644 --- a/src/backend/pacman.rs +++ b/src/backend/pacman.rs @@ -1,4 +1,6 @@ use std::collections::HashSet; +use std::os::unix::process::CommandExt; +use std::process::Command; use alpm::Alpm; use alpm::PackageReason::Explicit; @@ -9,6 +11,8 @@ use crate::Package; pub struct Pacman; impl Backend for Pacman { + const BINARY: &'static str = "paru"; + fn get_all_installed_packages() -> HashSet { convert_to_pacdef_packages(get_all_installed_packages_from_alpm()) } @@ -16,6 +20,24 @@ 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 { diff --git a/src/cmd.rs b/src/cmd.rs index 2b1f76e..2151c64 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -1,11 +1,9 @@ -use std::os::unix::process::CommandExt; use std::path::PathBuf; use std::process::{Command, ExitStatus}; use anyhow::{anyhow, Result}; use crate::env::get_editor; -use crate::Package; pub fn run_edit_command(files: &[PathBuf]) -> Result { let mut cmd = Command::new(get_editor()?); @@ -14,21 +12,3 @@ pub fn run_edit_command(files: &[PathBuf]) -> Result { } cmd.status().map_err(|e| anyhow!(e)) } - -pub fn run_install_command(diff: Vec) { - let mut cmd = Command::new("paru"); - cmd.arg("-S"); - for p in diff { - cmd.arg(format!("{p}")); - } - cmd.exec(); -} - -pub fn run_remove_command(unmanaged: Vec) { - let mut cmd = Command::new("paru"); - cmd.arg("-Rsn"); - for p in unmanaged { - cmd.arg(format!("{p}")); - } - cmd.exec(); -} diff --git a/src/core.rs b/src/core.rs index 4e4f518..74ba3fd 100644 --- a/src/core.rs +++ b/src/core.rs @@ -6,7 +6,7 @@ use clap::ArgMatches; use crate::action; use crate::backend::{Backend, Pacman}; -use crate::cmd::{run_edit_command, run_install_command, run_remove_command}; +use crate::cmd::run_edit_command; use crate::ui::get_user_confirmation; use crate::Group; use crate::Package; @@ -56,10 +56,9 @@ impl Pacdef { for p in &diff { println!(" {p}"); } - println!(); crate::ui::get_user_confirmation(); - run_install_command(diff); + Pacman::install_packages(diff); } #[allow(clippy::unit_arg)] @@ -138,6 +137,6 @@ impl Pacdef { println!(" {p}"); } get_user_confirmation(); - run_remove_command(unmanaged); + Pacman::remove_packages(unmanaged); } }