use std::env; use std::env::temp_dir; use std::fs::{create_dir_all, remove_dir_all}; use std::io::Cursor; use std::process::exit; use uuid::Uuid; use pkgfile::PKGFile; use crate::args::{Args, Command}; use crate::prelude::run_bootstrap; mod args; mod prelude; fn main() { let args = Args::from(env::args().collect::>()[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 { println!("** reading data..."); if let Ok(d) = std::fs::read(uri) { d } else { println!("Could not read file!"); exit(1); } }; println!("** parsing pkgfile..."); let pkg = match PKGFile::try_from(data) { Ok(p) => p, Err(_) => { println!("!! Could not interpret PKGFile..."); exit(1); } }; let mani = prelude::mani_from_str(&pkg.manifest); println!("-- Package: {}", &mani.package.name); println!("-- Description: {}", &mani.package.description); println!("-- Version: {}", &mani.package.version); println!("-- Tags: {}", &mani.package.tags.join(", ")); 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."); println!("** extracting pkgtar..."); { 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"); 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 }) } _ => { println!("Unsupported command, allowed commands: strap."); exit(1); } } }