Skip to content

Commit

Permalink
refactor(pm): use display format to run commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Rignchen committed Oct 22, 2024
1 parent 4345e58 commit e2f5470
Showing 1 changed file with 37 additions and 94 deletions.
131 changes: 37 additions & 94 deletions src/package_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ impl std::fmt::Display for PackageManager {
/// Run a command with the package manager.
/// This macro is used to avoid code duplication between all the package manager and their methods.
macro_rules! run_command {
($name:expr, $arg1:expr, $packages:expr, $arg2: expr, $instruction:expr) => {{
let mut command = Command::new($name);
($self:expr, $arg1:expr, $packages:expr, $arg2: expr, $instruction:expr) => {{
let mut command = Command::new(format!("{}", $self));
command.arg($arg1);
if $instruction.yes {
command.arg($arg2);
}
command.args($packages);
command
}};
($name:expr, $arg1:expr, $packages:expr) => {{
let mut command = Command::new($name);
($self:expr, $arg1:expr, $packages:expr) => {{
let mut command = Command::new(format!("{}", $self));
command.arg($arg1);
command.arg($packages);
command
}};
($name:expr, $all:expr, $arg1:expr, $packages:expr) => {{
let mut command = Command::new($name);
($self:expr, $all:expr, $arg1:expr, $packages:expr) => {{
let mut command = Command::new(format!("{}", $self));
match $packages {
Some(package) => command.arg($arg1).args(package),
None => command.arg($all),
Expand All @@ -84,43 +84,13 @@ impl PackageManager {
/// Install a package using the package manager.
pub fn install(&self, instruction: &Install) -> DepotResult<()> {
let result = match self {
PackageManager::Pacman => run_command!(
"pacman",
"-S",
&instruction.package,
"--noconfirm",
instruction
),
PackageManager::Yay => run_command!(
"yay",
"-S",
&instruction.package,
"--noconfirm",
instruction
),
PackageManager::Apk => run_command!(
"apk",
"add",
&instruction.package,
"--no-cache",
instruction
),
PackageManager::AptGet => run_command!(
"apt-get",
"install",
&instruction.package,
"-y",
instruction
),
PackageManager::Apt => {
run_command!("apt", "install", &instruction.package, "-y", instruction)
}
PackageManager::Pkg => {
run_command!("pkg", "install", &instruction.package, "-y", instruction)
}
PackageManager::Dnf => {
run_command!("dnf", "install", &instruction.package, "-y", instruction)
}
PackageManager::Pacman => run_command!(self, "-S", &instruction.package, "--noconfirm", instruction),
PackageManager::Yay => run_command!(self, "-S", &instruction.package, "--noconfirm", instruction),
PackageManager::Apk => run_command!(self, "add", &instruction.package, "--no-cache", instruction),
PackageManager::AptGet => run_command!(self, "install", &instruction.package, "-y", instruction),
PackageManager::Apt => run_command!(self, "install", &instruction.package, "-y", instruction),
PackageManager::Pkg => run_command!(self, "install", &instruction.package, "-y", instruction),
PackageManager::Dnf => run_command!(self, "install", &instruction.package, "-y", instruction),
}
.status();
if result.is_ok() && result.unwrap().success() {
Expand All @@ -136,39 +106,13 @@ impl PackageManager {
/// Remove a package using the package manager.
pub fn remove(&self, instruction: &Remove) -> DepotResult<()> {
let result = match self {
PackageManager::Pacman => run_command!(
"pacman",
"-R",
&instruction.package,
"--noconfirm",
instruction
),
PackageManager::Yay => run_command!(
"yay",
"-R",
&instruction.package,
"--noconfirm",
instruction
),
PackageManager::Apk => run_command!(
"apk",
"del",
&instruction.package,
"--no-cache",
instruction
),
PackageManager::AptGet => {
run_command!("apt-get", "remove", &instruction.package, "-y", instruction)
}
PackageManager::Apt => {
run_command!("apt", "remove", &instruction.package, "-y", instruction)
}
PackageManager::Pkg => {
run_command!("pkg", "remove", &instruction.package, "-y", instruction)
}
PackageManager::Dnf => {
run_command!("dnf", "remove", &instruction.package, "-y", instruction)
}
PackageManager::Pacman => run_command!(self, "-R", &instruction.package, "--noconfirm", instruction),
PackageManager::Yay => run_command!(self, "-R", &instruction.package, "--noconfirm", instruction),
PackageManager::Apk => run_command!(self, "del", &instruction.package, "--no-cache", instruction),
PackageManager::AptGet => run_command!(self, "remove", &instruction.package, "-y", instruction),
PackageManager::Apt => run_command!(self, "remove", &instruction.package, "-y", instruction),
PackageManager::Pkg => run_command!(self, "remove", &instruction.package, "-y", instruction),
PackageManager::Dnf => run_command!(self, "remove", &instruction.package, "-y", instruction),
}
.status();
if result.is_ok() && result.unwrap().success() {
Expand All @@ -184,17 +128,18 @@ impl PackageManager {
/// Search for a package using the package manager.
pub fn search(&self, instruction: &Search) -> DepotResult<()> {
let result = match self {
PackageManager::Pacman => {
run_command!("pacman", "-Ss", &instruction.package)
}
PackageManager::Yay => run_command!("yay", "-Ss", &instruction.package),
PackageManager::Apk => run_command!("apk", "search", &instruction.package),
PackageManager::Pacman => run_command!(self, "-Ss", &instruction.package),
PackageManager::Yay => run_command!(self, "-Ss", &instruction.package),
PackageManager::Apk => run_command!(self, "search", &instruction.package),
PackageManager::AptGet => {
run_command!("apt-cache", "search", &instruction.package)
let mut command = Command::new("apt-cache");
command.arg("search");
command.arg(&instruction.package);
command
}
PackageManager::Apt => run_command!("apt", "search", &instruction.package),
PackageManager::Pkg => run_command!("pkg", "search", &instruction.package),
PackageManager::Dnf => run_command!("dnf", "search", &instruction.package),
PackageManager::Apt => run_command!(self, "search", &instruction.package),
PackageManager::Pkg => run_command!(self, "search", &instruction.package),
PackageManager::Dnf => run_command!(self, "search", &instruction.package),
}
.status();
if result.is_ok() && result.unwrap().success() {
Expand All @@ -210,15 +155,13 @@ impl PackageManager {
/// Update one or all package using the package manager.
pub fn update(&self, instruction: &Update) -> DepotResult<()> {
let result = match self {
PackageManager::Pacman => run_command!("pacman", "-Syu", "-S", &instruction.package),
PackageManager::Yay => run_command!("yay", "-Syu", "-S", &instruction.package),
PackageManager::Apk => run_command!("apk", "upgrade", "upgrade", &instruction.package),
PackageManager::AptGet => {
run_command!("apt-get", "upgrade", "upgrade", &instruction.package)
}
PackageManager::Apt => run_command!("apt", "upgrade", "upgrade", &instruction.package),
PackageManager::Pkg => run_command!("pkg", "upgrade", "upgrade", &instruction.package),
PackageManager::Dnf => run_command!("dnf", "upgrade", "upgrade", &instruction.package),
PackageManager::Pacman => run_command!(self, "-Syu", "-S", &instruction.package),
PackageManager::Yay => run_command!(self, "-Syu", "-S", &instruction.package),
PackageManager::Apk => run_command!(self, "upgrade", "upgrade", &instruction.package),
PackageManager::AptGet => run_command!(self, "upgrade", "upgrade", &instruction.package),
PackageManager::Apt => run_command!(self, "upgrade", "upgrade", &instruction.package),
PackageManager::Pkg => run_command!(self, "upgrade", "upgrade", &instruction.package),
PackageManager::Dnf => run_command!(self, "upgrade", "upgrade", &instruction.package),
}
.status();
if result.is_ok() && result.unwrap().success() {
Expand Down

0 comments on commit e2f5470

Please sign in to comment.