feat: implemented the whole process

This commit is contained in:
Strix 2023-10-14 22:39:40 +02:00
parent 59b77fd07f
commit 61e3c450b7
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774
9 changed files with 64 additions and 25 deletions

View file

@ -13,3 +13,4 @@ serde = { version = "1.0.171", features = ["derive"] }
regex = "1.9.1"
reqwest = { version = "0.11.18", features = ["blocking"] }
uuid = { version = "1.4.0", features = ["serde", "v4"] }
tar = "0.4.39"

View file

@ -1,6 +1,5 @@
pub enum Command {
Strap,
Unpack,
None,
}
@ -8,7 +7,6 @@ impl From<String> for Command {
fn from(value: String) -> Self {
match value.to_lowercase().as_str() {
"strap" => Command::Strap,
"unpack" => Command::Unpack,
_ => Command::None,
}
}

View file

@ -1,7 +1,7 @@
use std::env;
use std::env::temp_dir;
use std::fs::{create_dir_all, File, remove_dir_all};
use std::io::Write;
use std::fs::{create_dir_all, remove_dir_all};
use std::io::Cursor;
use std::process::exit;
use uuid::Uuid;
@ -9,6 +9,7 @@ use uuid::Uuid;
use pkgfile::PKGFile;
use crate::args::{Args, Command};
use crate::prelude::run_bootstrap;
mod args;
mod prelude;
@ -67,27 +68,25 @@ fn main() {
create_dir_all(&tmp_dir)
.expect("Could not create tmp dir.");
println!("** extracting pkgtar...");
{
println!("** writing tar to tmp file...");
let mut file = File::create(format!("{}/pkgtar", tmp_dir.to_str().unwrap_or("/tmp")))
.expect("Could not create tmp pkgtar file");
file.write(&pkg.data)
.expect("Could not write pkgtar to tmp file");
let mut archive = tar::Archive::new(Cursor::new(pkg.data));
archive.unpack(format!("{}/contents", &tmp_dir.to_str().unwrap()))
.expect("Could not extract archive");
}
env::set_current_dir(format!("{}/contents", &tmp_dir.to_str().unwrap()))
.expect("could not enter tmp dir");
// TODO: untar
// TODO: cd into pkg
// TODO: run bootstrap commands
println!("** running bootstrap commands...");
let res_bootstrap = run_bootstrap(mani);
println!("** removing temporary directory...");
remove_dir_all(&tmp_dir)
.expect(&*format!("Could not remove tmp dir: {}", &tmp_dir.to_str().unwrap()));
exit(if res_bootstrap { println!("!! bootstrap success"); 0 } else { 1 })
}
Command::Unpack => {}
_ => {
println!("Unsupported command, allowed commands: strap/unpack.");
println!("Unsupported command, allowed commands: strap.");
exit(1);
}
}

View file

@ -12,8 +12,8 @@ pub struct PKGR {
#[derive(Serialize, Deserialize)]
pub struct Bootstrap {
check_installed_commands: Vec<String>,
commands: Vec<String>
pub check_installed_commands: Vec<String>,
pub commands: Vec<String>
}
#[derive(Serialize, Deserialize)]
@ -33,3 +33,34 @@ pub fn mani_from_str(s: &str) -> Manifest<Option<PKGR>> {
pkgr: bmani.pkgr
}
}
pub fn run_bootstrap(mani: Manifest<Option<PKGR>>) -> bool {
if let Some(pkgr) = mani.pkgr {
if let Some(bootstrap) = pkgr.bootstrap {
fn run_command<S: Into<String>>(s: S) -> i32 {
std::process::Command::new("sh")
.arg("-c")
.arg(s.into())
.spawn().expect("Could not spawn process.")
.wait().expect("Could not wait for process.")
.code().expect("Could not fetch exit code.")
}
for command in &bootstrap.check_installed_commands {
if run_command(command) != 0 {
println!("!! Command failed: {}", command);
println!("!! Already installed.");
return false
}
}
for command in &bootstrap.commands {
if run_command(command) != 0 {
println!("!! Command failed: {}", command);
println!("!! Bootstrap failed!!!");
return false
}
}
}
}
return true
}