builder: add paccache builder

i18n
Fries 2 years ago committed by fries1234
parent cb3cc43749
commit 1bc9d0a1db

@ -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,

@ -1,5 +1,6 @@
pub mod git;
pub mod makepkg;
pub mod paccache;
pub mod pacdiff;
pub mod pacman;
pub mod pager;

@ -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
}
}

@ -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")
}

@ -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
}
}

@ -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,
}

@ -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,
};

@ -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");
}
}

Loading…
Cancel
Save