diff --git a/src/args.rs b/src/args.rs index a2815cd..c8c2a73 100644 --- a/src/args.rs +++ b/src/args.rs @@ -36,6 +36,10 @@ pub enum Operation { /// Upgrades locally installed packages to their latest versions #[clap(name="upgrade", aliases=&["upg", "up", "u", "-Syu"])] Upgrade, + + /// Removes all orphaned packages + #[clap(name="clean", aliases=&["cln", "cl", "-Sc"])] + Clean, } impl Default for Operation { diff --git a/src/main.rs b/src/main.rs index 466c150..b3eabae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use internal::error::SilentUnwrap; use crate::internal::exit_code::AppExitCode; #[allow(unused_imports)] -use crate::internal::{crash, warn, prompt, info, init, log, sort, structs::Options}; +use crate::internal::{crash, info, init, log, prompt, sort, structs::Options, warn}; #[global_allocator] static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; @@ -44,6 +44,10 @@ fn main() { info("Performing system upgrade".to_string()); operations::upgrade(options); } + Operation::Clean => { + info("Removing orphaned packages".to_string()); + operations::clean(options); + } } } diff --git a/src/operations/clean.rs b/src/operations/clean.rs new file mode 100644 index 0000000..f71663d --- /dev/null +++ b/src/operations/clean.rs @@ -0,0 +1,79 @@ +use crate::crash; +use crate::info; +use crate::internal::commands::ShellCommand; +use crate::internal::error::SilentUnwrap; +use crate::internal::exit_code::AppExitCode; +use crate::log; +use crate::prompt; +use crate::Options; + +pub fn clean(options: Options) { + let verbosity = options.verbosity; + let noconfirm = options.noconfirm; + + let orphaned_packages = ShellCommand::pacman() + .arg("-Qdt") + .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); + } + + let mut pacman_args = vec!["-Rns"]; + if noconfirm { + pacman_args.push("--noconfirm"); + } + + if verbosity >= 1 { + log("Removing orphans".to_string()); + } + + 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, + ); + } + + let clear_cache = prompt("Also clear pacman's package cache?".to_string(), false); + if clear_cache { + let mut pacman_args = vec!["-Scc"]; + if noconfirm { + pacman_args.push("--noconfirm"); + } + + if verbosity >= 1 { + log("Clearing package cache".to_string()); + } + + let pacman_result = ShellCommand::pacman() + .elevated() + .args(pacman_args) + .wait() + .silent_unwrap(AppExitCode::PacmanError); + + if pacman_result.success() { + info("Successfully cleared package cache".to_string()); + } else { + crash( + "Failed to clear package cache".to_string(), + AppExitCode::PacmanError, + ); + } + } +} diff --git a/src/operations/mod.rs b/src/operations/mod.rs index 7782641..6966d43 100644 --- a/src/operations/mod.rs +++ b/src/operations/mod.rs @@ -1,10 +1,12 @@ mod aur_install; +mod clean; mod install; mod search; mod uninstall; mod upgrade; pub use aur_install::*; +pub use clean::*; pub use install::*; pub use search::{aur_search, repo_search as search}; pub use uninstall::*; diff --git a/src/operations/upgrade.rs b/src/operations/upgrade.rs index 582a121..c3542ef 100644 --- a/src/operations/upgrade.rs +++ b/src/operations/upgrade.rs @@ -27,14 +27,16 @@ pub fn upgrade(options: Options) { if pacman_result.success() { info("Successfully upgraded repo packages".to_string()); } else { - let cont = prompt("Failed to upgrade repo packages, continue to upgrading AUR packages?".to_string(), false); + let cont = prompt( + "Failed to upgrade repo packages, continue to upgrading AUR packages?".to_string(), + false, + ); if !cont { info("Exiting".to_string()); std::process::exit(AppExitCode::PacmanError as i32); } } - if verbosity >= 1 { log("Upgrading AUR packages".to_string()); }