From 951d31bd77927f4f91a277e2ab373ca0419a0c49 Mon Sep 17 00:00:00 2001 From: Michal Date: Mon, 4 Jul 2022 18:11:43 +0000 Subject: [PATCH] Don't error out if there are no packages to clean --- src/operations/clean.rs | 98 ++++++++++++++++++++++++++--------------- 1 file changed, 63 insertions(+), 35 deletions(-) diff --git a/src/operations/clean.rs b/src/operations/clean.rs index 150c153..4254f50 100644 --- a/src/operations/clean.rs +++ b/src/operations/clean.rs @@ -1,3 +1,5 @@ +use std::process::Command; + use crate::crash; use crate::info; use crate::internal::commands::ShellCommand; @@ -16,56 +18,82 @@ pub fn clean(options: Options) { .wait_with_output() .silent_unwrap(AppExitCode::PacmanError); - info(format!( - "Removing orphans would uninstall the following packages: \n{}", - &orphaned_packages.stdout - )); - let cont = prompt("Continue?".to_string(), false); - if !cont { - info("Exiting".to_string()); - std::process::exit(AppExitCode::PacmanError as i32); - } + if orphaned_packages.stdout.as_str() == "" { + info("No orphaned packages found".to_string()); + } else { + info(format!( + "Removing orphans would uninstall the following packages: \n{}", + &orphaned_packages.stdout + )); + let cont = prompt("Continue?".to_string(), false); + if !cont { + info("Exiting".to_string()); + std::process::exit(AppExitCode::PacmanError as i32); + } - let mut pacman_args = vec!["-Rns"]; - if noconfirm { - pacman_args.push("--noconfirm"); - } + let mut pacman_args = vec!["-Rns"]; + if noconfirm { + pacman_args.push("--noconfirm"); + } - let orphaned_packages_vec = orphaned_packages.stdout.split('\n').collect::>(); - for package in &orphaned_packages_vec { - if package.len() > 0 { - pacman_args.push(package); + let orphaned_packages_vec = orphaned_packages.stdout.split('\n').collect::>(); + for package in &orphaned_packages_vec { + if !package.is_empty() { + pacman_args.push(package); + } } - } - if verbosity >= 1 { - log(format!("Removing orphans: {:?}", orphaned_packages_vec)); - } + if verbosity >= 1 { + log(format!("Removing orphans: {:?}", orphaned_packages_vec)); + } - let pacman_result = ShellCommand::pacman() - .elevated() - .args(pacman_args) - .wait() - .silent_unwrap(AppExitCode::PacmanError); + let pacman_result = ShellCommand::pacman() + .elevated() + .args(pacman_args) + .wait() + .silent_unwrap(AppExitCode::PacmanError); - if pacman_result.success() { - info("Successfully removed orphans".to_string()); - } else { - crash( - "Failed to remove orphans".to_string(), - AppExitCode::PacmanError, - ); + if pacman_result.success() { + info("Successfully removed orphans".to_string()); + } else { + crash( + "Failed to remove orphans".to_string(), + AppExitCode::PacmanError, + ); + } } let clear_cache = prompt("Also clear pacman's package cache?".to_string(), false); if clear_cache { - let mut pacman_args = vec!["-Scc"]; + let mut pacman_args = vec!["-Sc"]; if noconfirm { pacman_args.push("--noconfirm"); } + let mut paccache_args = vec!["-r"]; + if noconfirm { + paccache_args.push("--noconfirm"); + } + + if verbosity >= 1 { + log("Clearing using `paccache -r`".to_string()); + } + + Command::new("sudo") + .arg("paccache") + .args(paccache_args) + .spawn() + .unwrap_or_else(|e| { + crash( + format!("Couldn't clear cache using `paccache -r`, {}", e), + AppExitCode::PacmanError, + ) + }) + .wait() + .unwrap(); + if verbosity >= 1 { - log("Clearing package cache".to_string()); + log("Clearing using `pacman -Sc`".to_string()); } let pacman_result = ShellCommand::pacman()