59 lines
1.6 KiB
Rust
59 lines
1.6 KiB
Rust
use crate::common::{SummaryItem, print_summary, yesno};
|
|
use anyhow::anyhow;
|
|
use clap::Parser;
|
|
use console::style;
|
|
use indicatif::ProgressBar;
|
|
use packie::PackieBuilder;
|
|
use rust_i18n::t;
|
|
|
|
#[derive(Debug, Parser)]
|
|
pub struct Cli {
|
|
#[arg(short, long)]
|
|
yes: bool,
|
|
}
|
|
|
|
pub fn main(cli: Cli) -> Result<(), crate::Error> {
|
|
let mut packie = PackieBuilder::new().build()?;
|
|
|
|
let packages = packie.calculate_autoremove()?;
|
|
if packages.is_empty() {
|
|
Err(anyhow!("{}", t!("autoremove.no_packages_to_remove")))?;
|
|
}
|
|
|
|
let summary = packages
|
|
.iter()
|
|
.map(|x| SummaryItem {
|
|
pkgspec: x.clone().into(),
|
|
pkgident: x.clone(),
|
|
operation: SummaryItem::REMOVE,
|
|
})
|
|
.collect();
|
|
println!("{}", t!("autoremove.prompt_to_remove"));
|
|
println!();
|
|
print_summary(summary);
|
|
println!();
|
|
if cli.yes {
|
|
println!("y");
|
|
} else if !yesno(&t!("autoremove.confirm_removal"), false) {
|
|
println!("{}", t!("autoremove.aborting"));
|
|
return Ok(());
|
|
}
|
|
|
|
let nr_packages = packages.len();
|
|
let progress_bar = ProgressBar::new(nr_packages as _);
|
|
|
|
for (c, i) in packages.into_iter().enumerate() {
|
|
progress_bar.println(format!(
|
|
"[{}/{}] {}",
|
|
c + 1,
|
|
nr_packages,
|
|
t!("autoremove.action_hint", package = style(&i).red()),
|
|
));
|
|
packie
|
|
.remove(&i.clone().into())
|
|
.map_err(|error| anyhow!("{}", t!("autoremove.action_failure", error = error)))?;
|
|
progress_bar.inc(1);
|
|
}
|
|
Ok(())
|
|
}
|