use std::fs::File; use std::path::Path; use std::io::Read; use std::result::Result; use reqwest::IntoUrl; use reqwest::Client; use reqwest::header::ETAG; use smush::decode; fn main() -> Result<(), std::io::Error> { let client = Client::new(); let args: Vec<_> = std::env::args().collect(); if args.len() < 2 { println!("[PACK]: Please enter a package name..."); std::process::exit(1); } let split = args[1].as_str().split("::"); let vec: Vec<&str> = split.collect(); println!("[PACK]: Downloading {}", vec[0]); if !Path::new(format!("/usr/pack/repo/{}", vec[1]).as_str()).exists() { println!("[PACK]: Path /usr/pack/repo/{} does not exist. Please setup your environment.", vec[1]); std::process::exit(1); } let full_path: Vec<&str> = vec[0].split("/").collect(); let pkgname = full_path[full_path.len() - 1]; let repo = match vec[1] { "arch" => "http://mirrors.advancedhosters.com/archlinux/core/os/x86_64/", _ => { println!("Repo {} is not supported...", vec[1]); std::process::exit(0); }, }; print!("Making request..."); let url = reqwest::Url::parse(&(repo.to_string() + pkgname)).expect("Failed to parse URL"); let mut resp = client.get(url) .send().expect("Failed to make request"); if resp.status().is_success() { println!("OK"); if let Some(etag) = resp.headers().get(ETAG) { std::fs::write("etag", etag.as_bytes()).expect("Failed to write ETAG..."); } let mut file = match File::create(format!("/usr/pack/repo/{}/tarballs/{}", vec[1], pkgname)) { Ok(res) => res, Err(e) => { println!("Failed to create file...did you setup you environment? Do you have permission to create files?"); println!("\nError: {}", e); std::process::exit(1); } }; resp.copy_to(&mut file).expect("failed to write file"); } else { println!("FAIL!"); std::process::exit(1); } let mut archive = File::open(format!("/usr/pack/repo/{}/tarballs/{}", vec[1], pkgname)).expect("Failed to open tar..."); let mut tar = Vec::new(); archive.read_to_end(&mut tar).expect("Failed to read tar..."); Ok(()) }