dotfiles/src/update.rs

40 lines
1.2 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 Pipeline {
sha: String
2023-01-25 12:25:12 +00:00
}
#[derive(Debug, Deserialize)]
struct PackageEntry {
version: String,
pipeline: Pipeline
2023-01-25 12:25:12 +00:00
}
pub fn check_for_updates(version: &str) {
print!("Checking for updates... ");
let resp: Vec<PackageEntry> = attohttpc::get("https://gitlab.mattv.de/api/v4/projects/43/packages?name=installer&sort=desc").send().unwrap().json().unwrap();
2023-01-25 12:25:12 +00:00
let newest = resp.first().unwrap();
if newest.pipeline.sha != version {
2023-01-25 12:25:12 +00:00
println!("New version exists");
let exe = current_exe().unwrap();
let temp = Path::new("temp");
print!("Downloading... ");
attohttpc::get(format!("https://gitlab.mattv.de/api/v4/projects/43/packages/generic/installer/{}/installer-amd64", newest.version)).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");
}