Files
packie/cli/src/upgrade.rs
T

155 lines
4.7 KiB
Rust

use crate::common::{SummaryItem, format_size, print_summary, yesno};
use clap::Parser;
use console::style;
use indicatif::ProgressBar;
use packie::{
PackieBuilder, graph::UpgradationAction, install::InstallOptions, local::InstallFlags,
package::PkgSpec,
};
use rust_i18n::t;
#[derive(Debug, Parser)]
pub struct Cli {
/// Don't require manual confirmation
#[arg(short, long)]
yes: bool,
items: Vec<PkgSpec>,
}
pub fn main(cli: Cli) -> Result<(), crate::Error> {
let mut packie = PackieBuilder::new().build()?;
let actions = packie.calculate_upgradation(cli.items)?;
let mut ui_summary = Vec::with_capacity(actions.len());
for action in &actions {
match action {
UpgradationAction::InstallFromRepo(req) => {
ui_summary.push(SummaryItem {
pkgspec: req.repo_pkg.pkg_manifest.pkg_ident().into(),
pkgident: req.repo_pkg.pkg_manifest.pkg_ident(),
operation: SummaryItem::INSTALL,
});
}
UpgradationAction::Remove(pkg) => {
ui_summary.push(SummaryItem {
pkgspec: pkg.clone().into(),
pkgident: pkg.clone(),
operation: SummaryItem::REMOVE,
});
}
_ => (),
}
}
if ui_summary.is_empty() {
eprintln!("{}", t!("upgrade.nothing_to_do"));
return Ok(());
}
println!("{}", t!("upgrade.prompt_to_upgrade"));
println!();
print_summary(ui_summary.clone());
println!();
let download_size: u64 = actions
.iter()
.map(|x| match x {
UpgradationAction::InstallFromRepo(req) => req.repo_pkg.download_size,
_ => 0,
})
.sum();
let installed_size = actions
.iter()
.map(|x| match x {
UpgradationAction::InstallFromRepo(req) => req.repo_pkg.installed_size as i64,
UpgradationAction::Remove(pkg_ident) => packie
.select_local_package(pkg_ident)
.map(|y| -(y.installed_size as i64))
.unwrap_or_default(),
_ => 0,
})
.sum();
println!(
"{}",
t!(
"upgrade.download_size",
size = format_size(download_size as i64)
)
);
println!(
"{}",
t!("upgrade.installed_size", size = format_size(installed_size))
);
if cli.yes {
eprintln!("y");
} else if !yesno(&t!("upgrade.confirm_upgradation"), true) {
eprintln!("{}", t!("upgrade.aborting"));
return Ok(());
}
let downloaded = crate::common::download_packages(
&mut packie,
actions.iter().filter_map(|x| match x {
UpgradationAction::InstallFromRepo(req) => Some(&req.repo_pkg),
_ => None,
}),
)?;
let nr_actions = ui_summary.len();
let progress_bar = ProgressBar::new(nr_actions as _);
let mut count = 1;
for action in actions {
match action {
UpgradationAction::InstallFromRepo(req) => {
progress_bar.println(format!(
"[{}/{}] {}",
count,
nr_actions,
t!(
"upgrade.installing_hint",
pkg = style(&req.repo_pkg.pkg_manifest.pkg_ident()).green(),
)
));
progress_bar.inc(1);
count += 1;
let mut options = InstallOptions::default();
if req.override_links {
options.override_links = true;
}
packie.install_package_file(
downloaded
.get(&req.repo_pkg.pkg_manifest.pkg_ident())
.unwrap(),
&options,
)?;
}
UpgradationAction::EnsureInstallation(req) => {
let mut install_flags = InstallFlags::default();
if req.auto_installed {
install_flags.set(InstallFlags::AUTO_INSTALL);
}
packie.ensure_installation(
req.install_pkgspec,
req.pkg_ident,
install_flags,
req.force,
)?;
}
UpgradationAction::Remove(pkg_ident) => {
progress_bar.println(format!(
"[{}/{}] {}",
count,
nr_actions,
t!("upgrade.removing_hint", pkg = style(&pkg_ident).green(),)
));
packie.remove(&pkg_ident.into())?;
}
}
}
Ok(())
}