From 07cff812008931bc1324fa0c013610810bdf4bad Mon Sep 17 00:00:00 2001 From: Marshall Porter Date: Wed, 23 Oct 2024 01:56:03 -0700 Subject: [PATCH] fix self update --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/me.rs | 33 ++++++++++++++++++++++++--------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 99d2121..be78da4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -218,7 +218,7 @@ checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" [[package]] name = "ctrl" -version = "0.3.20" +version = "0.3.21" dependencies = [ "clap", "console", diff --git a/Cargo.toml b/Cargo.toml index 55aa428..85fc850 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ctrl" -version = "0.3.20" +version = "0.3.21" edition = "2021" description = "CLI tool for cross platform OS management" categories = ["command-line-interface"] diff --git a/src/me.rs b/src/me.rs index b98619a..3f5ba46 100644 --- a/src/me.rs +++ b/src/me.rs @@ -6,6 +6,7 @@ use std::path::Path; use clap::Subcommand; use dialoguer::Confirm; use reqwest; +use self_update::update::Release; use version_compare::Cmp; use crate::file::get_cur_path_str; @@ -82,17 +83,25 @@ fn upgrade() -> Result<(), Box> { .fetch()?; if releases.len() == 0 { - logcln("releases empty", Category::Info); + logcln("none found", Category::Info); return Ok(()); } - let current_version = self_update::cargo_crate_version!(); - let mut latest_version = releases[0].version.as_str(); + let current_version = self_update::cargo_crate_version!().to_string(); + let mut latest_version = current_version.clone(); let binary = format!("{}-ctrl", self_update::get_target()); - let asset = releases[0].asset_for(&binary, None); - if asset.is_none() { - latest_version = current_version; + let mut latest_viable_release: Option = None; + + for r in releases { + if r.asset_for(&binary, None).is_some() { + latest_viable_release = Some(r); + break; + } + } + + if let Some(ref r) = latest_viable_release { + latest_version = r.version.clone(); } logcln(&format!("current: {current_version}"), Category::Info); @@ -122,9 +131,15 @@ fn upgrade() -> Result<(), Box> { // create will then truncate the existing file let tmp_path_file = File::create(&tmp_path_str)?; - self_update::Download::from_url(&asset.unwrap().download_url) - .set_header(reqwest::header::ACCEPT, "application/octet-stream".parse()?) - .download_to(&tmp_path_file)?; + self_update::Download::from_url( + latest_viable_release + .unwrap() + .asset_for(&binary, None) + .unwrap() + .download_url.as_str(), + ) + .set_header(reqwest::header::ACCEPT, "application/octet-stream".parse()?) + .download_to(&tmp_path_file)?; self_update::Move::from_source(Path::new(&tmp_path_str)) // windows requires this; unix is optional