dotfiles/src/update.rs

44 lines
1.5 KiB
Rust
Raw Normal View History

2023-01-25 12:25:12 +00:00
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 PackageEntry {
2023-02-12 22:15:09 +00:00
version: String
2023-01-25 12:25:12 +00:00
}
2023-02-12 22:15:09 +00:00
const GITEA_KEY: &str = "1e9527c38c98b297dbdaaf39c060e075b2729464";
2023-01-25 12:25:12 +00:00
pub fn check_for_updates(version: &str) {
2023-02-12 22:15:09 +00:00
let version: u64 = version.parse().unwrap();
2023-01-25 12:25:12 +00:00
print!("Checking for updates... ");
2023-02-12 22:15:09 +00:00
let resp: Vec<PackageEntry> = attohttpc::get("https://gitea.mattv.de/api/v1/packages/root?type=generic&q=installer")
.header("accept", "application/json")
.header("Authorization", format!("token {GITEA_KEY}"))
.send().unwrap().json().unwrap();
let newest = resp.into_iter()
.map(|entry| entry.version.parse::<u64>().unwrap())
.max().unwrap();
if newest > version {
println!("New version {newest}");
2023-01-25 12:25:12 +00:00
let exe = current_exe().unwrap();
let temp = Path::new("temp");
print!("Downloading... ");
2023-02-12 22:15:09 +00:00
attohttpc::get(format!("https://gitea.mattv.de/api/packages/root/generic/installer/{newest}/installer-amd64"))
.header("Authorization", format!("token {GITEA_KEY}"))
.send().unwrap().write_to(File::create(temp).unwrap()).unwrap();
2023-01-25 12:25:12 +00:00
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");
}