use std::error::Error;
use std::fs;
use std::fs::File;
use std::io::copy;
pub fn download_templates() {
info!("Cloning templates...");
download_file(
"https://github.com/ebalo55/crabby/archive/refs/heads/main.zip",
"crabby.zip",
).unwrap();
unzip_templates("crabby.zip", ".").unwrap();
fs::remove_file("crabby.zip").unwrap();
info!("Templates cloned successfully!");
}
fn download_file(url: &str, file_path: &str) -> Result<(), Box<dyn Error>> {
let response = reqwest::blocking::get(url)?;
if response.status().is_success() {
let mut file = File::create(file_path)?;
copy(&mut response.bytes().unwrap().as_ref(), &mut file)?;
info!("Project archive downloaded successfully!");
Ok(())
} else {
let status = response.status();
let message = response.text()?;
Err(format!("Request failed with status {}: {}", status, message).into())
}
}
fn unzip_templates(archive_path: &str, extract_to: &str) -> Result<(), Box<dyn Error>> {
let file = File::open(archive_path)?;
let mut archive = zip::ZipArchive::new(file)?;
archive.extract(extract_to)?;
fs::rename("crabby-main/templates", "templates")?;
fs::remove_dir_all("crabby-main")?;
Ok(())
}