Skip to content

Commit

Permalink
Fix windows updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Heliozoa committed Jul 12, 2023
1 parent e9c526a commit f0eb2c4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
27 changes: 25 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u128> {
self.config
.get(Self::UPDATE_LAST_CHECKED_KEY)
.and_then(|v| v.as_str())
.and_then(|s| s.parse::<u128>().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()));
}
}
11 changes: 8 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
46 changes: 11 additions & 35 deletions src/updater.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::commands::util::get_path;
use crate::TmcCliConfig;
use anyhow::Context;
use indicatif::{ProgressBar, ProgressStyle};
use reqwest::{
Expand All @@ -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.
Expand All @@ -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...");
Expand Down Expand Up @@ -104,20 +102,10 @@ fn elevate(command: String) -> anyhow::Result<()> {
Ok(())
}

fn is_it_time_yet() -> anyhow::Result<bool> {
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::<u128>() {
Ok(time) => time,
_ => return Ok(true),
fn is_it_time_yet(config: &TmcCliConfig) -> anyhow::Result<bool> {
let last_check = match config.get_update_last_checked() {
Some(time) => time,
None => return Ok(true),
};
let now = SystemTime::now();
let now = now
Expand All @@ -128,21 +116,9 @@ fn is_it_time_yet() -> anyhow::Result<bool> {
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(())
Expand Down

0 comments on commit f0eb2c4

Please sign in to comment.