From 357854384b161f69495ad22c651bc54a0cbed552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Misty=20De=20M=C3=A9o?= <misty@axo.dev> Date: Thu, 7 Mar 2024 13:24:59 +1100 Subject: [PATCH] feat: return version info on success Returns old and new version info, plus the tag the new verson was built from. Fixes #33. --- axoupdater-cli/src/bin/axoupdater/main.rs | 4 ++-- axoupdater/src/lib.rs | 27 +++++++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/axoupdater-cli/src/bin/axoupdater/main.rs b/axoupdater-cli/src/bin/axoupdater/main.rs index 94546f9..b0f0c42 100644 --- a/axoupdater-cli/src/bin/axoupdater/main.rs +++ b/axoupdater-cli/src/bin/axoupdater/main.rs @@ -6,11 +6,11 @@ struct CliArgs {} fn real_main(_cli: &CliApp<CliArgs>) -> Result<(), miette::Report> { eprintln!("Checking for updates..."); - if AxoUpdater::new_for_updater_executable()? + if let Some(result) = AxoUpdater::new_for_updater_executable()? .load_receipt()? .run_sync()? { - eprintln!("New release installed!") + eprintln!("New release {} installed!", result.new_version) } else { eprintln!("Already up to date; not upgrading"); } diff --git a/axoupdater/src/lib.rs b/axoupdater/src/lib.rs index d41ceb9..6068f1c 100644 --- a/axoupdater/src/lib.rs +++ b/axoupdater/src/lib.rs @@ -25,6 +25,16 @@ use serde::Deserialize; use temp_dir::TempDir; use thiserror::Error; +/// Provides information about the result of the upgrade operation +pub struct UpdateResult { + /// The old version (pre-upgrade) + pub old_version: String, + /// The new version (post-upgrade) + pub new_version: String, + /// The tag the new version was created from + pub new_version_tag: String, +} + /// Struct representing an updater process pub struct AxoUpdater { /// The name of the program to update, if specified @@ -152,9 +162,9 @@ impl AxoUpdater { /// update was actually performed or not; false indicates "no update was /// needed", while an error indicates that an update couldn't be performed /// due to an error. - pub async fn run(&mut self) -> AxoupdateResult<bool> { + pub async fn run(&mut self) -> AxoupdateResult<Option<UpdateResult>> { if !self.is_update_needed().await? { - return Ok(false); + return Ok(None); } let release = match &self.latest_release { @@ -218,12 +228,21 @@ impl AxoUpdater { } command.run()?; - Ok(true) + let result = UpdateResult { + old_version: self + .current_version + .to_owned() + .unwrap_or("unable to determine".to_owned()), + new_version: release.version(), + new_version_tag: release.tag_name.to_owned(), + }; + + Ok(Some(result)) } #[cfg(feature = "blocking")] /// Identical to Axoupdater::run(), but performed synchronously. - pub fn run_sync(&mut self) -> AxoupdateResult<bool> { + pub fn run_sync(&mut self) -> AxoupdateResult<Option<UpdateResult>> { tokio::runtime::Builder::new_current_thread() .worker_threads(1) .max_blocking_threads(128)