add install and remove packages to Backend trait

This commit is contained in:
steven-omaha
2023-01-05 16:44:41 +01:00
parent ddcb5dec3d
commit 2f511cf87c
4 changed files with 31 additions and 24 deletions
+6
View File
@@ -7,8 +7,14 @@ use crate::Package;
pub use pacman::Pacman; pub use pacman::Pacman;
pub trait Backend { 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. /// Get all packages that are installed in the system.
fn get_all_installed_packages() -> HashSet<Package>; fn get_all_installed_packages() -> HashSet<Package>;
/// Get all packages that were installed in the system explicitly. /// Get all packages that were installed in the system explicitly.
fn get_explicitly_installed_packages() -> HashSet<Package>; fn get_explicitly_installed_packages() -> HashSet<Package>;
/// Install the specified packages.
fn install_packages(packages: Vec<Package>);
/// Remove the specified packages.
fn remove_packages(packages: Vec<Package>);
} }
+22
View File
@@ -1,4 +1,6 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::os::unix::process::CommandExt;
use std::process::Command;
use alpm::Alpm; use alpm::Alpm;
use alpm::PackageReason::Explicit; use alpm::PackageReason::Explicit;
@@ -9,6 +11,8 @@ use crate::Package;
pub struct Pacman; pub struct Pacman;
impl Backend for Pacman { impl Backend for Pacman {
const BINARY: &'static str = "paru";
fn get_all_installed_packages() -> HashSet<Package> { fn get_all_installed_packages() -> HashSet<Package> {
convert_to_pacdef_packages(get_all_installed_packages_from_alpm()) convert_to_pacdef_packages(get_all_installed_packages_from_alpm())
} }
@@ -16,6 +20,24 @@ impl Backend for Pacman {
fn get_explicitly_installed_packages() -> HashSet<Package> { fn get_explicitly_installed_packages() -> HashSet<Package> {
convert_to_pacdef_packages(get_explicitly_installed_packages_from_alpm()) convert_to_pacdef_packages(get_explicitly_installed_packages_from_alpm())
} }
fn install_packages(packages: Vec<Package>) {
let mut cmd = Command::new("paru");
cmd.arg("-S");
for p in packages {
cmd.arg(format!("{p}"));
}
cmd.exec();
}
fn remove_packages(packages: Vec<Package>) {
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<String> { fn get_all_installed_packages_from_alpm() -> HashSet<String> {
-20
View File
@@ -1,11 +1,9 @@
use std::os::unix::process::CommandExt;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{Command, ExitStatus}; use std::process::{Command, ExitStatus};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use crate::env::get_editor; use crate::env::get_editor;
use crate::Package;
pub fn run_edit_command(files: &[PathBuf]) -> Result<ExitStatus> { pub fn run_edit_command(files: &[PathBuf]) -> Result<ExitStatus> {
let mut cmd = Command::new(get_editor()?); let mut cmd = Command::new(get_editor()?);
@@ -14,21 +12,3 @@ pub fn run_edit_command(files: &[PathBuf]) -> Result<ExitStatus> {
} }
cmd.status().map_err(|e| anyhow!(e)) cmd.status().map_err(|e| anyhow!(e))
} }
pub fn run_install_command(diff: Vec<Package>) {
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<Package>) {
let mut cmd = Command::new("paru");
cmd.arg("-Rsn");
for p in unmanaged {
cmd.arg(format!("{p}"));
}
cmd.exec();
}
+3 -4
View File
@@ -6,7 +6,7 @@ use clap::ArgMatches;
use crate::action; use crate::action;
use crate::backend::{Backend, Pacman}; 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::ui::get_user_confirmation;
use crate::Group; use crate::Group;
use crate::Package; use crate::Package;
@@ -56,10 +56,9 @@ impl Pacdef {
for p in &diff { for p in &diff {
println!(" {p}"); println!(" {p}");
} }
println!();
crate::ui::get_user_confirmation(); crate::ui::get_user_confirmation();
run_install_command(diff); Pacman::install_packages(diff);
} }
#[allow(clippy::unit_arg)] #[allow(clippy::unit_arg)]
@@ -138,6 +137,6 @@ impl Pacdef {
println!(" {p}"); println!(" {p}");
} }
get_user_confirmation(); get_user_confirmation();
run_remove_command(unmanaged); Pacman::remove_packages(unmanaged);
} }
} }