From 1bc9d0a1db91aaffa9c86a7e5e29d90d83a66ba9 Mon Sep 17 00:00:00 2001 From: Fries Date: Mon, 12 Sep 2022 21:00:40 -0700 Subject: [PATCH] builder: add paccache builder --- src/args.rs | 4 +++ src/builder/mod.rs | 1 + src/builder/paccache.rs | 45 +++++++++++++++++++++++++++++ src/internal/commands.rs | 10 +++++++ src/internal/config.rs | 10 +++++-- src/internal/structs.rs | 1 + src/main.rs | 2 ++ src/operations/clean.rs | 62 ++++++++++++++++------------------------ 8 files changed, 96 insertions(+), 39 deletions(-) create mode 100644 src/builder/paccache.rs diff --git a/src/args.rs b/src/args.rs index a304e9e..8ee2bf1 100644 --- a/src/args.rs +++ b/src/args.rs @@ -17,6 +17,10 @@ pub struct Args { #[clap(long = "noconfirm", global = true)] pub no_confirm: bool, + /// Make some commands have less output + #[clap(long, short, global = true)] + pub quiet: bool, + /// Loops sudo in the background to ensure it doesn't time out during long builds #[clap(long = "sudoloop", global = true)] pub sudoloop: bool, diff --git a/src/builder/mod.rs b/src/builder/mod.rs index eeebf56..b6e2acf 100644 --- a/src/builder/mod.rs +++ b/src/builder/mod.rs @@ -1,5 +1,6 @@ pub mod git; pub mod makepkg; +pub mod paccache; pub mod pacdiff; pub mod pacman; pub mod pager; diff --git a/src/builder/paccache.rs b/src/builder/paccache.rs new file mode 100644 index 0000000..f343135 --- /dev/null +++ b/src/builder/paccache.rs @@ -0,0 +1,45 @@ +use crate::internal::{commands::ShellCommand, error::AppResult}; + +#[derive(Debug, Default)] +pub struct PaccacheBuilder { + keep: i32, + keep_ins: bool, + quiet: bool, +} + +impl PaccacheBuilder { + pub fn keep(mut self, keep: i32) -> Self { + self.keep = keep; + + self + } + + pub fn keep_ins(mut self, keep_ins: bool) -> Self { + self.keep_ins = keep_ins; + + self + } + + pub fn quiet(mut self, quiet: bool) -> Self { + self.quiet = quiet; + + self + } + + pub async fn remove(self) -> AppResult<()> { + let mut command = ShellCommand::paccache().elevated(); + + if self.quiet { + command = command.arg("-q"); + } + + if self.keep_ins { + command = command.arg("-u") + } + + command + .args(&["-r", &format!("-k{}", self.keep.to_string())]) + .wait_success() + .await + } +} diff --git a/src/internal/commands.rs b/src/internal/commands.rs index c5a5555..e2ef0c9 100644 --- a/src/internal/commands.rs +++ b/src/internal/commands.rs @@ -34,6 +34,16 @@ impl ShellCommand { } } + pub fn paccache() -> Self { + let paccache_cmd = Self::new("paccache"); + + if is_tty() { + paccache_cmd + } else { + paccache_cmd.arg("--nocolor") + } + } + pub fn pacdiff() -> Self { Self::new("pacdiff") } diff --git a/src/internal/config.rs b/src/internal/config.rs index 291b6c3..ddfbfcf 100644 --- a/src/internal/config.rs +++ b/src/internal/config.rs @@ -17,6 +17,8 @@ pub struct Config { #[derive(Debug, Deserialize, Serialize)] pub struct ConfigBase { pub pacdiff_warn: bool, + pub paccache_keep: i32, + pub paccache_keep_ins: bool, } #[derive(Debug, Deserialize, Serialize, Default)] @@ -32,7 +34,11 @@ pub struct ConfigBin { impl Default for ConfigBase { fn default() -> Self { - Self { pacdiff_warn: true } + Self { + pacdiff_warn: true, + paccache_keep: 0, + paccache_keep_ins: true, + } } } @@ -59,7 +65,7 @@ impl Config { } else { let default_conf = Config::default(); let toml_string = toml::ser::to_string_pretty(&default_conf).unwrap(); - fs::write(config_path, format!("{}\n\n{}", "# See https://github.com/crystal-linux/amethyst/tree/main/docs for more information on config keys", toml_string)).unwrap(); + fs::write(config_path, format!("{}\n\n{}", "# See https://github.com/crystal-linux/docs/blob/main/Amethyst/config.mdx for more information on config keys", toml_string)).unwrap(); default_conf } } diff --git a/src/internal/structs.rs b/src/internal/structs.rs index d91df00..11d0764 100644 --- a/src/internal/structs.rs +++ b/src/internal/structs.rs @@ -19,6 +19,7 @@ impl Sorted { /// Options to be passed down to internal functions pub struct Options { pub noconfirm: bool, + pub quiet: bool, pub asdeps: bool, pub upgrade: bool, } diff --git a/src/main.rs b/src/main.rs index 6f6d0bc..bba8ebf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,9 +37,11 @@ async fn main() { init_logger(args.verbose.into()); let noconfirm = args.no_confirm; + let quiet = args.quiet; let options = Options { noconfirm, + quiet, asdeps: false, upgrade: false, }; diff --git a/src/operations/clean.rs b/src/operations/clean.rs index 6d9eb9d..b8943b9 100644 --- a/src/operations/clean.rs +++ b/src/operations/clean.rs @@ -1,7 +1,10 @@ +use crate::builder::paccache::PaccacheBuilder; +use crate::builder::pacman::PacmanQueryBuilder; use crate::builder::rm::RmBuilder; use crate::crash; use crate::internal::commands::ShellCommand; +use crate::internal::config::Config; use crate::internal::error::SilentUnwrap; use crate::internal::exit_code::AppExitCode; @@ -13,6 +16,7 @@ use crate::Options; #[tracing::instrument(level = "trace")] pub async fn clean(options: Options) { let noconfirm = options.noconfirm; + let quiet = options.quiet; // Check for orphaned packages let orphaned_packages = ShellCommand::pacman() @@ -91,53 +95,37 @@ pub async fn clean(options: Options) { }; if clear_pacman_cache { - // Build pacman args - let mut pacman_args = vec!["-Sc"]; - if noconfirm { - pacman_args.push("--noconfirm"); + let conf = Config::read(); + let mut debug_str = "Clearing using paccache -r".to_string(); + + debug_str = format!("{} -k{}", debug_str, conf.base.paccache_keep); + if conf.base.paccache_keep_ins { + debug_str = format!("{} -u", debug_str); } - // Build paccache args - let mut paccache_args = vec!["-r"]; - if noconfirm { - paccache_args.push("--noconfirm"); + if quiet { + debug_str = format!("{} -q", debug_str); } - tracing::debug!("Clearing using `paccache -r`"); + tracing::debug!(debug_str); - // Clear pacman's cache (keeping latest 3 versions of installed packages) - ShellCommand::sudo() - .arg("paccache") - .args(paccache_args) - .elevated() - .spawn(true) + // Clear pacman's cache + // keeps 0 versions of the package in the cache by default + // keeps installed packages in the cache by default + PaccacheBuilder::default() + .keep(conf.base.paccache_keep) + .keep_ins(conf.base.paccache_keep_ins) + .quiet(quiet) + .remove() + .await .unwrap_or_else(|e| { crash!( AppExitCode::PacmanError, - "Couldn't clear cache using `paccache -r`, {}", + "Failed to clear package cache, {}", e ) - }) - .wait() - .await - .unwrap(); - - tracing::debug!("Clearing using `pacman -Sc`"); + }); - // Clear pacman's cache (keeping only installed packages) - let pacman_result = ShellCommand::pacman() - .elevated() - .args(pacman_args) - .wait() - .await - .silent_unwrap(AppExitCode::PacmanError); - - if pacman_result.success() { - // If pacman succeeded, notify user - tracing::info!("Successfully cleared package cache"); - } else { - // If pacman failed, crash - crash!(AppExitCode::PacmanError, "Failed to clear package cache",); - } + tracing::info!("Successfully cleared package cache"); } }