From 1f5a2127e1f39199f404739d06a940f979e57f31 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 27 Aug 2022 21:15:20 +0200 Subject: [PATCH] Add builder for pacman query Signed-off-by: trivernis --- Containerfile | 4 +++- src/builder/pacman.rs | 44 +++++++++++++++++++++++++++++++++++++++ src/operations/upgrade.rs | 42 +++++++++++++++---------------------- 3 files changed, 64 insertions(+), 26 deletions(-) diff --git a/Containerfile b/Containerfile index d42e31f..77d3667 100644 --- a/Containerfile +++ b/Containerfile @@ -12,6 +12,8 @@ WORKDIR /usr/src/amethyst COPY Cargo.toml Cargo.lock ./ RUN cargo fetch COPY src ./src +RUN mkdir target +COPY target/debug/build ./target/debug/build RUN cargo build --frozen RUN mkdir /tmp/ame RUN cp target/debug/ame /tmp/ame/ @@ -28,5 +30,5 @@ RUN rm -f $(pacdiff -o -f) USER ame RUN mkdir -p /home/ame/.local/share RUN touch /home/ame/.zshrc -ENV AME_LOG=debug +ENV AME_LOG=debug,hyper=info ENTRYPOINT ["zsh"] \ No newline at end of file diff --git a/src/builder/pacman.rs b/src/builder/pacman.rs index 4d75cba..9dd2d7e 100644 --- a/src/builder/pacman.rs +++ b/src/builder/pacman.rs @@ -27,6 +27,7 @@ impl PacmanInstallBuilder { self } + #[allow(clippy::wrong_self_convention)] pub fn as_deps(mut self, as_deps: bool) -> Self { self.as_deps = as_deps; @@ -48,3 +49,46 @@ impl PacmanInstallBuilder { command.args(self.packages).wait_success().await } } + +#[derive(Debug, Default)] +pub struct PacmanQueryBuilder { + foreign: bool, +} + +impl PacmanQueryBuilder { + /// Query for foreign packages + pub fn foreign(mut self, foreign: bool) -> Self { + self.foreign = foreign; + + self + } + + #[tracing::instrument(level = "debug")] + pub async fn query(self) -> AppResult> { + let mut command = ShellCommand::pacman().arg("-Q").arg("--color").arg("never"); + + if self.foreign { + command = command.arg("-m"); + } + + let output = command.wait_with_output().await?; + let packages = output + .stdout + .split('\n') + .filter(|p| !p.is_empty()) + .filter_map(|p| p.split_once(' ')) + .map(|(name, version)| BasicPackageInfo { + name: name.to_string(), + version: version.to_string(), + }) + .collect(); + + Ok(packages) + } +} + +#[derive(Clone, Debug)] +pub struct BasicPackageInfo { + pub name: String, + pub version: String, +} diff --git a/src/operations/upgrade.rs b/src/operations/upgrade.rs index 176d46a..55e31a5 100644 --- a/src/operations/upgrade.rs +++ b/src/operations/upgrade.rs @@ -1,3 +1,4 @@ +use crate::builder::pacman::PacmanQueryBuilder; use crate::internal::commands::ShellCommand; use crate::internal::error::SilentUnwrap; use crate::internal::exit_code::AppExitCode; @@ -42,40 +43,31 @@ pub async fn upgrade(options: Options) { log!("Upgrading AUR packages"); } - let pacman_output = ShellCommand::pacman() - .arg("-Qm") - .args(&["--color", "never"]) - .wait_with_output() + let non_native_pkgs = PacmanQueryBuilder::default() + .foreign(true) + .query() .await .silent_unwrap(AppExitCode::PacmanError); - let non_native_pkgs = pacman_output - .stdout - .split('\n') - .filter(|p| !p.is_empty()) - .filter_map(|p| p.split_once(' ')) - .collect::>(); - if verbosity >= 1 { - log!("{:?}", non_native_pkgs); - } + tracing::debug!("aur packages: {non_native_pkgs:?}"); let mut aur_upgrades = vec![]; - for (pkg_name, pkg_version) in non_native_pkgs { - if verbosity >= 1 { - log!( - "remote package: name = {}, version = {}", - pkg_name, - pkg_version - ); - } - let remote_package = rpcinfo(pkg_name).await.silent_unwrap(AppExitCode::RpcError); + for pkg in non_native_pkgs { + let remote_package = rpcinfo(&pkg.name) + .await + .silent_unwrap(AppExitCode::RpcError); if let Some(remote_package) = remote_package { - if remote_package.metadata.version != pkg_version { - aur_upgrades.push(pkg_name.to_string()); + if remote_package.metadata.version != pkg.version { + tracing::debug!( + "local version: {}, remote version: {}", + pkg.version, + remote_package.metadata.version + ); + aur_upgrades.push(pkg.name); } } else { - warn!("Could not find the remote package for {}", pkg_name); + warn!("Could not find the remote package for {}", pkg.name); } }