2023-10-14 22:39:39 +02:00
|
|
|
use std::env;
|
2023-10-14 22:39:39 +02:00
|
|
|
use std::env::temp_dir;
|
2023-10-14 22:39:39 +02:00
|
|
|
use std::fs::{create_dir_all, File, remove_dir_all};
|
2023-10-14 22:39:39 +02:00
|
|
|
use std::io::Write;
|
2023-10-14 22:39:39 +02:00
|
|
|
use std::process::exit;
|
2023-10-14 22:39:39 +02:00
|
|
|
|
2023-10-14 22:39:39 +02:00
|
|
|
use uuid::Uuid;
|
2023-10-14 22:39:39 +02:00
|
|
|
|
2023-10-14 22:39:39 +02:00
|
|
|
use pkgfile::PKGFile;
|
|
|
|
|
|
|
|
use crate::args::{Args, Command};
|
|
|
|
|
2023-10-14 22:39:39 +02:00
|
|
|
mod args;
|
2023-10-14 22:39:39 +02:00
|
|
|
mod prelude;
|
2023-10-14 22:39:39 +02:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args = Args::from(env::args().collect::<Vec<String>>()[1..].to_owned());
|
|
|
|
match args.command {
|
|
|
|
Command::Strap => {
|
|
|
|
if args.args.len() == 0 {
|
|
|
|
println!("no path/uri");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
let uri = &args.args[0];
|
|
|
|
println!("!! Got package: \"{}\"", uri);
|
|
|
|
let re = r"[A-z]+://.+";
|
|
|
|
let data = if regex::Regex::new(re).unwrap().is_match(uri) {
|
|
|
|
println!("** detected uri.");
|
|
|
|
reqwest::blocking::get(uri)
|
|
|
|
.expect("Could not request file.")
|
|
|
|
.bytes()
|
|
|
|
.expect("Could not turn file into bytes.")
|
|
|
|
.to_vec()
|
|
|
|
} else {
|
2023-10-14 22:39:39 +02:00
|
|
|
println!("** reading data...");
|
2023-10-14 22:39:39 +02:00
|
|
|
if let Ok(d) = std::fs::read(uri) {
|
|
|
|
d
|
|
|
|
} else {
|
|
|
|
println!("Could not read file!");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-14 22:39:39 +02:00
|
|
|
println!("** parsing pkgfile...");
|
2023-10-14 22:39:39 +02:00
|
|
|
let pkg = match PKGFile::try_from(data) {
|
2023-10-14 22:39:39 +02:00
|
|
|
Ok(p) => p,
|
|
|
|
Err(_) => {
|
|
|
|
println!("!! Could not interpret PKGFile...");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-14 22:39:39 +02:00
|
|
|
let mani = prelude::mani_from_str(&pkg.manifest);
|
|
|
|
|
2023-10-14 22:39:39 +02:00
|
|
|
println!("-- Package: {}", &mani.package.name);
|
|
|
|
println!("-- Description: {}", &mani.package.description);
|
|
|
|
println!("-- Version: {}", &mani.package.version);
|
|
|
|
println!("-- Tags: {}", &mani.package.tags.join(", "));
|
2023-10-14 22:39:39 +02:00
|
|
|
|
|
|
|
if !mani.valid() {
|
|
|
|
println!("!!! Manifest is not valid.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut tmp_dir = temp_dir();
|
|
|
|
tmp_dir.push(format!("{}", Uuid::new_v4()));
|
|
|
|
create_dir_all(&tmp_dir)
|
|
|
|
.expect("Could not create tmp dir.");
|
|
|
|
|
|
|
|
{
|
2023-10-14 22:39:39 +02:00
|
|
|
println!("** writing tar to tmp file...");
|
|
|
|
let mut file = File::create(format!("{}/pkgtar", tmp_dir.to_str().unwrap_or("/tmp")))
|
2023-10-14 22:39:39 +02:00
|
|
|
.expect("Could not create tmp pkgtar file");
|
|
|
|
|
|
|
|
file.write(&pkg.data)
|
|
|
|
.expect("Could not write pkgtar to tmp file");
|
|
|
|
}
|
|
|
|
|
2023-10-14 22:39:39 +02:00
|
|
|
|
2023-10-14 22:39:39 +02:00
|
|
|
// TODO: untar
|
|
|
|
// TODO: cd into pkg
|
|
|
|
// TODO: run bootstrap commands
|
2023-10-14 22:39:39 +02:00
|
|
|
|
|
|
|
println!("** removing temporary directory...");
|
|
|
|
remove_dir_all(&tmp_dir)
|
|
|
|
.expect(&*format!("Could not remove tmp dir: {}", &tmp_dir.to_str().unwrap()));
|
2023-10-14 22:39:39 +02:00
|
|
|
}
|
|
|
|
Command::Unpack => {}
|
2023-10-14 22:39:39 +02:00
|
|
|
_ => {
|
|
|
|
println!("Unsupported command, allowed commands: strap/unpack.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|