use std::env::current_exe;
use std::fs;
use std::fs::File;
use std::os::unix::process::CommandExt;
use std::path::Path;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Commit {
id: String
}
#[derive(Debug, Deserialize)]
struct Link {
direct_asset_url: String
}
#[derive(Debug, Deserialize)]
struct Assets {
links: Vec
}
#[derive(Debug, Deserialize)]
struct ReleaseEntry {
commit: Commit,
assets: Assets
}
pub fn check_for_updates(version: &str) {
print!("Checking for updates... ");
let resp: Vec = attohttpc::get("https://gitlab.mattv.de/api/v4/projects/43/releases").send().unwrap().json().unwrap();
let newest = resp.first().unwrap();
if newest.commit.id != version {
println!("New version exists");
let exe = current_exe().unwrap();
let temp = Path::new("temp");
print!("Downloading... ");
attohttpc::get(newest.assets.links.first().unwrap().direct_asset_url.clone()).send().unwrap().write_to(File::create(temp).unwrap()).unwrap();
println!("Done");
fs::set_permissions(&temp, exe.metadata().unwrap().permissions()).unwrap();
fs::rename(&temp, &exe).unwrap();
std::process::Command::new(exe).exec();
std::process::exit(1);
}
println!("No new version");
}