From 98cc3e2388d8e0f8a4a9a656715fe6b441bfd1d5 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 3 Sep 2022 17:40:27 +0200 Subject: [PATCH] Add confirmation for reinstalling of already installed packages --- src/builder/pacman.rs | 11 ++++++++++- src/logging/output.rs | 36 +++++++++++++++++++++++++++++------ src/operations/aur_install.rs | 11 +++++++---- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/builder/pacman.rs b/src/builder/pacman.rs index b5ab95f..3740dca 100644 --- a/src/builder/pacman.rs +++ b/src/builder/pacman.rs @@ -8,6 +8,7 @@ pub struct PacmanInstallBuilder { files: Vec, as_deps: bool, no_confirm: bool, + needed: bool, } impl PacmanInstallBuilder { @@ -44,6 +45,12 @@ impl PacmanInstallBuilder { self } + pub fn needed(mut self, needed: bool) -> Self { + self.needed = needed; + + self + } + #[tracing::instrument(level = "debug")] pub async fn install(self) -> AppResult<()> { let mut command = ShellCommand::pacman().elevated(); @@ -61,9 +68,11 @@ impl PacmanInstallBuilder { if self.as_deps { command = command.arg("--asdeps") } + if self.needed { + command = command.arg("--needed") + } command - .arg("--needed") .args(self.packages) .args(self.files) .wait_success() diff --git a/src/logging/output.rs b/src/logging/output.rs index 56e4d02..7453285 100644 --- a/src/logging/output.rs +++ b/src/logging/output.rs @@ -1,12 +1,14 @@ +use std::collections::{HashMap, HashSet}; + use aur_rpc::PackageInfo; use console::Alignment; use crossterm::style::Stylize; -use crate::internal::dependencies::DependencyInformation; +use crate::{builder::pacman::PacmanQueryBuilder, internal::dependencies::DependencyInformation}; use super::get_logger; -pub fn print_dependency_list(dependencies: &[DependencyInformation]) -> bool { +pub async fn print_dependency_list(dependencies: &[DependencyInformation]) -> bool { let (deps_repo, makedeps_repo, deps_aur, makedeps_aur) = dependencies .iter() .map(|d| { @@ -39,7 +41,7 @@ pub fn print_dependency_list(dependencies: &[DependencyInformation]) -> bool { if !deps_aur.is_empty() { get_logger().print_newline(); tracing::info!("AUR dependencies"); - print_aur_package_list(&deps_aur); + print_aur_package_list(&deps_aur).await; empty = false; } @@ -53,23 +55,45 @@ pub fn print_dependency_list(dependencies: &[DependencyInformation]) -> bool { if !makedeps_aur.is_empty() { get_logger().print_newline(); tracing::info!("AUR make dependencies"); - print_aur_package_list(&makedeps_aur); + print_aur_package_list(&makedeps_aur).await; empty = false; } empty } -pub fn print_aur_package_list(packages: &[PackageInfo]) { +pub async fn print_aur_package_list(packages: &[PackageInfo]) -> bool { + let pkgs = packages + .iter() + .map(|p| p.metadata.name.clone()) + .collect::>(); + let installed = PacmanQueryBuilder::all() + .query_with_output() + .await + .unwrap() + .into_iter() + .filter(|p| pkgs.contains(&p.name)) + .map(|p| (p.name.clone(), p)) + .collect::>(); + get_logger().print_list( packages.iter().map(|pkg| { format!( - "{} version {} ({} votes)", + "{} version {} ({} votes) {}", console::pad_str(&pkg.metadata.name, 30, Alignment::Left, Some("...")).bold(), pkg.metadata.version.clone().dim(), pkg.metadata.num_votes, + if installed.contains_key(&pkg.metadata.name) { + "(Installed)" + } else { + "" + } + .bold() + .magenta() ) }), "\n ", ); + + !installed.is_empty() } diff --git a/src/operations/aur_install.rs b/src/operations/aur_install.rs index 4da6f36..840198f 100644 --- a/src/operations/aur_install.rs +++ b/src/operations/aur_install.rs @@ -104,8 +104,11 @@ pub async fn aur_install(packages: Vec, options: Options) { pb.finish_with_message("All packages found".green().to_string()); get_logger().reset_output_type(); - pb.finish_with_message("Found all packages in the aur"); - print_aur_package_list(&package_info); + if print_aur_package_list(&package_info).await && !options.noconfirm { + if !prompt!(default yes, "Some packages are already installed. Continue anyway?") { + cancelled!(); + } + } if !options.noconfirm { let to_review = multi_select!(&packages, "Select packages to review"); @@ -133,7 +136,7 @@ pub async fn aur_install(packages: Vec, options: Options) { .await .silent_unwrap(AppExitCode::RpcError); - print_dependency_list(&dependencies); + print_dependency_list(&dependencies).await; get_logger().new_multi_progress(); let contexts = future::try_join_all( @@ -377,7 +380,7 @@ async fn install_packages( ctx.step = BuildStep::Done; } - install_opts.files(packages).install().await?; + install_opts.files(packages).needed(false).install().await?; Ok(ctxs) }