pack/src/main.rs

105 lines
3.5 KiB
Rust
Raw Normal View History

2019-09-03 10:37:49 +00:00
use std::fs::File;
2019-09-03 15:44:47 +00:00
use std::path::Path;
2019-09-03 23:17:02 +00:00
use std::result::Result;
2019-09-04 16:46:44 +00:00
use std::process::Command;
2019-09-03 10:37:49 +00:00
use reqwest::Client;
use reqwest::header::ETAG;
2019-09-04 23:04:18 +00:00
fn err_and_exit(msg: String, code: i32) {
println!("ERROR!: {}", msg);
std::process::exit(code);
}
2019-09-03 10:37:49 +00:00
fn main() -> Result<(), std::io::Error> {
let client = Client::new();
2019-09-03 15:23:13 +00:00
let args: Vec<_> = std::env::args().collect();
if args.len() < 2 {
2019-09-03 22:38:25 +00:00
println!("[PACK]: Please enter a package name...");
std::process::exit(1);
2019-09-03 15:23:13 +00:00
}
let split = args[1].as_str().split("::");
let vec: Vec<&str> = split.collect();
2019-09-04 23:04:18 +00:00
if vec.len() < 2 {
err_and_exit("Please enter a repo to download from!".to_string(), 0);
}
2019-09-03 15:44:47 +00:00
2019-09-03 22:38:25 +00:00
println!("[PACK]: Downloading {}", vec[0]);
2019-09-03 21:41:20 +00:00
if !Path::new(format!("/usr/pack/repo/{}", vec[1]).as_str()).exists() {
2019-09-04 23:04:18 +00:00
err_and_exit(format!("Path /usr/pack/repo/{} does not exist. Please setup your environment.", vec[1]), 1);
2019-09-03 15:44:47 +00:00
}
2019-09-03 22:38:25 +00:00
let full_path: Vec<&str> = vec[0].split("/").collect();
let pkgname = full_path[full_path.len() - 1];
2019-09-03 23:17:02 +00:00
let repo = match vec[1] {
2019-09-04 16:46:44 +00:00
"arch_core" => "http://mirrors.advancedhosters.com/archlinux/core/os/x86_64/",
2019-09-03 23:17:02 +00:00
_ => {
2019-09-04 23:04:18 +00:00
println!("ERROR!: Repo {} is not supported...", vec[1]);
2019-09-03 23:17:02 +00:00
std::process::exit(0);
},
};
2019-09-03 10:37:49 +00:00
2019-09-04 16:46:44 +00:00
print!("[PACK]: Making request...");
2019-09-03 23:17:02 +00:00
let url = reqwest::Url::parse(&(repo.to_string() + pkgname)).expect("Failed to parse URL");
2019-09-04 23:04:18 +00:00
let mut resp = match client.get(url).send() {
Ok(a) => a,
Err(e) => {
println!("ERROR!: Couldn't get package from url. Are you connected to the internet?\n\n{}", e);
std::process::exit(0);
},
};
2019-09-03 23:17:02 +00:00
2019-09-03 10:37:49 +00:00
if resp.status().is_success() {
2019-09-03 21:41:20 +00:00
println!("OK");
2019-09-03 10:37:49 +00:00
if let Some(etag) = resp.headers().get(ETAG) {
std::fs::write("etag", etag.as_bytes()).expect("Failed to write ETAG...");
}
2019-09-03 22:38:25 +00:00
let mut file = match File::create(format!("/usr/pack/repo/{}/tarballs/{}", vec[1], pkgname)) {
2019-09-03 15:23:13 +00:00
Ok(res) => res,
2019-09-03 22:38:25 +00:00
Err(e) => {
2019-09-04 23:04:18 +00:00
println!("Failed to create file...did you setup you environment? Do you have permission to create files?\n\n{}", e);
std::process::exit(0);
},
2019-09-03 15:23:13 +00:00
};
2019-09-03 10:37:49 +00:00
resp.copy_to(&mut file).expect("failed to write file");
2019-09-03 21:41:20 +00:00
} else {
println!("FAIL!");
std::process::exit(1);
2019-09-03 10:37:49 +00:00
}
2019-09-04 16:46:44 +00:00
let spltdirname: Vec<&str> = pkgname.split(".").collect();
let dirname = spltdirname[0];
print!("[PACK]: Creating package dir...");
std::fs::create_dir(format!("/usr/pack/repo/{}/{}", vec[1], dirname)).expect("Failed to create directory");
println!("OK");
print!("[PACK]: Executing tar...");
2019-09-04 23:04:18 +00:00
Command::new("tar")
2019-09-04 16:46:44 +00:00
.arg(format!("-C/usr/pack/repo/{}/{}", vec[1], dirname))
.arg("-xf")
.arg(format!("/usr/pack/repo/{}/tarballs/{}", vec[1], pkgname))
.output()
.expect("Failed to execute tar");
println!("OK");
let paths = std::fs::read_dir(format!("/usr/pack/repo/{}/{}/usr/bin/", vec[1], dirname)).expect("Failed to read dir...");
println!("[PACK]: Creating symlinks");
for file in paths {
let pth = &file.expect("Failed to read file").path();
let strpth = pth.to_str().expect("Failed to convert to str...");
println!("\tAdding file {} to /usr/pack/bin/", strpth);
let a: Vec<&str> = strpth.split("/").collect();
let name = a[a.len() - 1];
2019-09-04 23:04:18 +00:00
std::os::unix::fs::symlink(pth, format!("/usr/pack/bin/{}", name)).expect("Failed to symlink...");
2019-09-04 16:46:44 +00:00
}
2019-09-03 21:41:20 +00:00
2019-09-03 10:37:49 +00:00
Ok(())
}