From 2a289a3f3112c71f7814e522f19bc7c6448ecb2c Mon Sep 17 00:00:00 2001 From: Shar-jeel-Sajid Date: Sun, 23 Mar 2025 06:14:32 +0500 Subject: [PATCH] implementing self update command --- binaries/cli/Cargo.toml | 1 + binaries/cli/src/lib.rs | 51 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/binaries/cli/Cargo.toml b/binaries/cli/Cargo.toml index bc87c7e8..a40d77ad 100644 --- a/binaries/cli/Cargo.toml +++ b/binaries/cli/Cargo.toml @@ -50,6 +50,7 @@ tabwriter = "1.4.0" log = { version = "0.4.21", features = ["serde"] } colored = "2.1.0" env_logger = "0.11.3" +self_update = "0.42.0" pyo3 = { workspace = true, features = [ "extension-module", "abi3", diff --git a/binaries/cli/src/lib.rs b/binaries/cli/src/lib.rs index e169f667..c0c7f614 100644 --- a/binaries/cli/src/lib.rs +++ b/binaries/cli/src/lib.rs @@ -240,6 +240,12 @@ enum Command { #[clap(long)] quiet: bool, }, + + SelfUpdate { + /// Only check for updates without installing + #[clap(long)] + check_only: bool, + }, } #[derive(Debug, clap::Args)] @@ -537,6 +543,51 @@ fn run(args: Args) -> eyre::Result<()> { .context("failed to run dora-daemon")? } Command::Runtime => dora_runtime::main().context("Failed to run dora-runtime")?, + Command::SelfUpdate { check_only } => { + println!("Checking for updates..."); + + let status = self_update::backends::github::Update::configure() + .repo_owner("dora-rs") + .repo_name("dora") + .bin_name("dora") + .show_download_progress(true) + .current_version(env!("CARGO_PKG_VERSION")) + .build()?; + + if check_only { + // Only check if an update is available + match status.get_latest_release() { + Ok(release) => { + let current_version = self_update::cargo_crate_version!(); + if current_version != release.version { + println!( + "An update is available: {}. Run 'dora self-update' to update", + release.version + ); + } else { + println!( + "Dora CLI is already at the latest version: {}", + current_version + ); + } + } + Err(e) => println!("Failed to check for updates: {}", e), + } + } else { + // Perform the actual update + match status.update() { + Ok(update_status) => match update_status { + self_update::Status::UpToDate(version) => { + println!("Dora CLI is already at the latest version: {}", version); + } + self_update::Status::Updated(version) => { + println!("Successfully updated Dora CLI to version: {}", version); + } + }, + Err(e) => println!("Failed to update: {}", e), + } + } + } }; Ok(())