dotfiles/src/update.rs

35 lines
1.3 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;
2023-02-12 22:30:26 +00:00
const GITEA_KEY: &str = "786666bd8bce93c562c4fc4c83933faa6cbdc802";
2023-02-12 22:15:09 +00:00
2023-01-25 12:25:12 +00:00
pub fn check_for_updates(version: &str) {
print!("Checking for updates... ");
2023-02-12 22:30:26 +00:00
let resp: serde_json::Value = attohttpc::get("https://gitea.mattv.de/api/v1/repos/root/dotfiles/branches/installer")
2023-02-12 22:15:09 +00:00
.header("accept", "application/json")
.header("Authorization", format!("token {GITEA_KEY}"))
.send().unwrap().json().unwrap();
2023-02-12 22:30:26 +00:00
let newest = resp["commit"]["id"].as_str().unwrap();
if newest != version {
2023-02-12 22:15:09 +00:00
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");
}