Added self update function

This commit is contained in:
2023-01-25 13:25:12 +01:00
parent a340997e68
commit 1b496211a6
5 changed files with 455 additions and 64 deletions

View File

@@ -3,10 +3,9 @@ mod repository;
mod config;
mod operations;
mod utils;
mod update;
use strum::{EnumIter, IntoEnumIterator};
#[derive(Debug, Clone, EnumIter)]
#[derive(Debug, Clone)]
enum MainMenu {
Install,
Collect,
@@ -30,6 +29,13 @@ impl ToString for MainMenu {
}
fn main() {
if let Some(version) = option_env!("CI_COMMIT_SHA") {
println!("Starting installer version {}", version);
update::check_for_updates(version);
} else {
println!("Starting installer version unknown");
}
if !repository::open_repo() {
println!("Failed to open/clone repo!");
return;
@@ -50,7 +56,17 @@ fn main() {
};
'main_loop: loop {
let res = prompt::select(Some("What do you want to do?"), MainMenu::iter().collect());
let res = prompt::select(
Some("What do you want to do?"),
vec![
MainMenu::Install,
MainMenu::Collect,
MainMenu::Pull,
MainMenu::Diff,
MainMenu::Upload,
MainMenu::Quit
]
);
match res {
MainMenu::Install => operations::install(&mods),
MainMenu::Collect => operations::collect(&mods),

50
src/update.rs Normal file
View File

@@ -0,0 +1,50 @@
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<Link>
}
#[derive(Debug, Deserialize)]
struct ReleaseEntry {
commit: Commit,
assets: Assets
}
pub fn check_for_updates(version: &str) {
print!("Checking for updates... ");
let resp: Vec<ReleaseEntry> = 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");
}