dotfiles/src/update.rs

36 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-13 19:05:20 +00:00
const JENKINS_KEY: &str = "1196373359a3f17bbb8f0f5685b8152276";
2023-02-12 22:15:09 +00:00
2023-02-13 19:05:20 +00:00
pub fn check_for_updates(version: u64, name: &str) {
2023-01-25 12:25:12 +00:00
print!("Checking for updates... ");
2023-02-13 19:05:20 +00:00
let newest: u64 = attohttpc::get(format!("https://jenkins.mattv.de/job/Gitea%20Organization/job/dotfiles/job/{name}/lastSuccessfulBuild/buildNumber"))
.basic_auth("root", Some(JENKINS_KEY))
.send().unwrap()
.text().unwrap()
.parse().unwrap();
if newest > version {
println!("New version {name}-{newest}");
2023-01-25 12:25:12 +00:00
let exe = current_exe().unwrap();
let temp = Path::new("temp");
print!("Downloading... ");
2023-02-13 19:05:20 +00:00
attohttpc::get(format!("https://jenkins.mattv.de/job/Gitea%20Organization/job/dotfiles/job/{name}/{newest}/artifact/installer-amd64"))
.basic_auth("root", Some(JENKINS_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");
}