refactor: simpilfy packages management

This commit is contained in:
2025-05-02 00:38:15 +02:00
parent ad6935b82c
commit bb92cb362c
6 changed files with 116 additions and 125 deletions

View File

@@ -235,32 +235,17 @@ fn manage_package(package_command: PackageCommand) -> Result<()> {
}
fn manage_list(list: String, remove: bool) -> Result<()> {
process_packages(&list, remove, '+')?;
process_packages(&list, remove, '-')?;
Ok(())
}
fn process_packages(list: &String, remove: bool, prefix: char) -> Result<()> {
let packages = list
.lines()
.map(|line| line.trim())
.filter(|line| !line.is_empty() && !line.starts_with('#') && !line.starts_with('-'))
.collect::<Vec<_>>();
let mut dnf_cmd = std::process::Command::new("dnf");
if remove {
log::info!("Removing wanted list's packages...");
dnf_cmd.arg("remove");
} else {
log::info!("Installing wanted list's packages...");
dnf_cmd.arg("install").arg("--allowerasing");
}
packages.iter().for_each(|package| {
dnf_cmd.arg(package);
});
dnf_cmd.status()?;
let packages = list
.lines()
.map(|line| line.trim())
.filter(|line| !line.is_empty() && !line.starts_with('#') && line.starts_with('-'))
.map(|line| line.trim_start_matches('-').trim())
.filter(|line| !line.is_empty() && line.starts_with(prefix))
.map(|line| line.trim_start_matches(prefix).trim())
.collect::<Vec<_>>();
if packages.is_empty() {
@@ -269,19 +254,27 @@ fn manage_list(list: String, remove: bool) -> Result<()> {
let mut dnf_cmd = std::process::Command::new("dnf");
if remove {
log::info!("Re-installing unwanted list's packages...");
dnf_cmd.arg("install").arg("--allowerasing");
if prefix == '+' {
log::info!("Removing wanted list's packages...");
dnf_cmd.arg("remove");
} else {
log::info!("Re-installing unwanted list's packages...");
dnf_cmd.arg("install").arg("--allowerasing");
}
} else {
log::info!("Removing unwanted list's packages...");
dnf_cmd.arg("remove");
if prefix == '+' {
log::info!("Installing wanted list's packages...");
dnf_cmd.arg("install").arg("--allowerasing");
} else {
log::info!("Removing unwanted list's packages...");
dnf_cmd.arg("remove");
}
}
packages.iter().for_each(|package| {
dnf_cmd.arg(package);
});
dnf_cmd.status()?;
Ok(())
}