From f0eb2c418e47e60754c2c91c933dd25d5d8504d8 Mon Sep 17 00:00:00 2001 From: Heliozoa Date: Thu, 13 Jul 2023 02:13:26 +0300 Subject: [PATCH] Fix windows updater --- src/config.rs | 27 +++++++++++++++++++++++++-- src/lib.rs | 11 ++++++++--- src/updater.rs | 46 +++++++++++----------------------------------- 3 files changed, 44 insertions(+), 40 deletions(-) diff --git a/src/config.rs b/src/config.rs index 0648e55..1a16e47 100644 --- a/src/config.rs +++ b/src/config.rs @@ -55,7 +55,30 @@ impl TmcCliConfig { } pub fn remove_test_login(&mut self) { - let key = TEST_LOGIN_KEY; - self.config.remove(key); + self.config.remove(TEST_LOGIN_KEY); + } +} + +#[cfg(target_os = "windows")] +impl TmcCliConfig { + const UPDATE_LAST_CHECKED_KEY: &str = "update-last-checked"; + + pub fn get_update_last_checked(&self) -> Option { + self.config + .get(Self::UPDATE_LAST_CHECKED_KEY) + .and_then(|v| v.as_str()) + .and_then(|s| s.parse::().ok()) + } + + pub fn update_last_checked(&mut self) { + use std::time::{SystemTime, UNIX_EPOCH}; + + let key = Self::UPDATE_LAST_CHECKED_KEY.to_string(); + let timestamp = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Invalid system time") + .as_millis(); + self.config + .insert(key, toml::Value::String(timestamp.to_string())); } } diff --git a/src/lib.rs b/src/lib.rs index 5014929..39617ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,16 +33,21 @@ pub fn run(cli: Cli, io: &mut Io) { } fn run_inner(io: &mut Io, cli: Cli) -> anyhow::Result<()> { + let config_path = TmcCliConfig::location()?; + let config = TmcCliConfig::load(config_path)?; + + #[cfg(target_os = "windows")] + let mut config = config; + if cli.no_update { let os = std::env::consts::OS; if os == "windows" { #[cfg(target_os = "windows")] - updater::check_for_update(cli.force_update)?; + updater::check_for_update(&mut config, cli.force_update)?; } } else { println!("No Auto-Updates"); } - let config_path = TmcCliConfig::location()?; - let config = TmcCliConfig::load(config_path)?; + commands::handle(cli, io, config) } diff --git a/src/updater.rs b/src/updater.rs index 0f6591e..9e3cdfc 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -1,4 +1,4 @@ -use crate::commands::util::get_path; +use crate::TmcCliConfig; use anyhow::Context; use indicatif::{ProgressBar, ProgressStyle}; use reqwest::{ @@ -13,10 +13,8 @@ use std::{ process::Command, time::{SystemTime, UNIX_EPOCH}, }; -use tmc_langs::{ConfigValue, TmcConfig}; pub const GITHUB_URL: &str = "https://api.github.com/repos/rage/tmc-cli-rust/tags"; -pub const PLUGIN: &str = "tmc_cli_rust"; pub const DELAY_MILLIS_24H: u128 = 1440 * 60 * 1000; /// Autoupdater for Windows platform. @@ -25,9 +23,9 @@ pub const DELAY_MILLIS_24H: u128 = 1440 * 60 * 1000; /// stashes the old executable and downloads a new one. /// Will run in privileged stage if needed on Windows! -pub fn check_for_update(force: bool) -> anyhow::Result<()> { - if force || is_it_time_yet()? { - generate_time_stamp()?; +pub fn check_for_update(config: &mut TmcCliConfig, force: bool) -> anyhow::Result<()> { + if force || is_it_time_yet(config)? { + generate_time_stamp(config)?; checktemp()?; let new_ver = get_latest_version()?; println!("Checking for updates..."); @@ -104,20 +102,10 @@ fn elevate(command: String) -> anyhow::Result<()> { Ok(()) } -fn is_it_time_yet() -> anyhow::Result { - let config = TmcCliConfig::load()?; - - let value = config.get("update-last-checked"); - let last_check = match &value { - ConfigValue::Value(Some(s)) => s.as_str().context("invalid value")?, - _ => { - return Ok(true); - } - }; - - let last_check = match last_check.parse::() { - Ok(time) => time, - _ => return Ok(true), +fn is_it_time_yet(config: &TmcCliConfig) -> anyhow::Result { + let last_check = match config.get_update_last_checked() { + Some(time) => time, + None => return Ok(true), }; let now = SystemTime::now(); let now = now @@ -128,21 +116,9 @@ fn is_it_time_yet() -> anyhow::Result { Ok(update) } -fn generate_time_stamp() -> anyhow::Result<()> { - let mut config = TmcCliConfig::load()?; - let now = SystemTime::now(); - let since_the_epoch = now - .duration_since(UNIX_EPOCH) - .context("Time went backwards")? - .as_millis(); - - if let Err(_err) = config.insert( - "update-last-checked".to_string(), - toml::Value::String(since_the_epoch.to_string()), - ) { - println!("timestamp could not be changed"); - } - if let Err(_err) = config.save(get_path()?.as_path()) { +fn generate_time_stamp(config: &mut TmcCliConfig) -> anyhow::Result<()> { + config.update_last_checked(); + if let Err(_err) = config.save() { println!("Problem saving timestamp"); } Ok(())