Skip to content

Commit

Permalink
fix self update
Browse files Browse the repository at this point in the history
  • Loading branch information
meop committed Oct 23, 2024
1 parent 27b666c commit 07cff81
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
33 changes: 24 additions & 9 deletions src/me.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,17 +83,25 @@ fn upgrade() -> Result<(), Box<dyn Error>> {
.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<Release> = 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);
Expand Down Expand Up @@ -122,9 +131,15 @@ fn upgrade() -> Result<(), Box<dyn Error>> {
// 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
Expand Down

0 comments on commit 07cff81

Please sign in to comment.