use crate::{pos, utils_mod::OptionLogNone, ResultLogError, GREEN, RED, RESET, YELLOW};
pub fn new_cli(remote_repo: Option<String>) -> anyhow::Result<()> {
let Some(remote_repo) = remote_repo else {
println!("{RED}Error: First argument 'remote_repo' is missing: `cargo auto new_cli remote_repo`{RESET}");
println!("{RED}Example: cargo auto new_cli codeberg.org/bestia-dev-work-in-progress/hello_world{RESET}");
return Ok(());
};
let mut splitted = remote_repo.split("/");
let repo_domain = splitted.next().log_msg(pos!(), "repo_domain missing")?;
let owner_or_organization = splitted.next().log_msg(pos!(), "owner_or_organization missing")?;
let rust_project_name = splitted.next().log_msg(pos!(), "rust_project_name missing")?;
copy_to_files(repo_domain, owner_or_organization, rust_project_name).log(pos!())?;
println!();
println!(" {YELLOW}The command `cargo auto new_cli` generated the directory `{rust_project_name}`.{RESET}");
println!(" {YELLOW}You can open this new Rust project in VSCode:{RESET}",);
println!("{GREEN}code {rust_project_name}{RESET}");
println!(" {YELLOW}Then build inside the VSCode terminal with:{RESET}");
println!("{GREEN}cargo auto build{RESET}");
println!(" {YELLOW}and follow the detailed instructions.{RESET}");
Ok(())
}
fn copy_to_files(repo_domain: &str, owner_or_organization: &str, rust_project_name: &str) -> anyhow::Result<()> {
let folder_path = std::path::Path::new(rust_project_name);
if folder_path.exists() {
anyhow::bail!("{RED}Error: Folder {rust_project_name} already exists! {RESET}");
}
std::fs::create_dir_all(folder_path).log(pos!())?;
println!(" {YELLOW}Downloading template.tar.gz...{RESET}");
let file_to_download = "template.tar.gz";
let path = "./template.tar.gz";
let url = "https://codeberg.org/automation-tasks-rs/cargo_auto_template_new_cli/releases/latest";
let custom_policy = reqwest::redirect::Policy::custom(|attempt| attempt.stop());
let reqwest_client = reqwest::blocking::ClientBuilder::new()
.redirect(custom_policy)
.build()
.log(pos!())?;
let http_response = reqwest_client.get(url).send().log(pos!())?;
let http_location = http_response
.headers()
.get(reqwest::header::LOCATION)
.log_msg(pos!(), "header Location None")?;
let http_location = http_location.to_str().log(pos!())?;
let http_location = http_location.replace("/tag/", "/download/");
let url = format!("https://codeberg.org{http_location}/{file_to_download}");
let reqwest_client = reqwest::blocking::Client::new();
let http_response = reqwest_client.get(url).send();
if let Ok(body) = http_response {
let body = body.bytes().log(pos!())?;
std::fs::write(path, &body)
.or_else(|err| anyhow::bail!("Download failed for {file_to_download} {err}"))
.log(pos!())?;
} else {
anyhow::bail!("Error while retrieving data: {:#?}", http_response.err());
}
let tar_gz = std::fs::File::open(path).log(pos!())?;
let tar = flate2::read::GzDecoder::new(tar_gz);
let mut archive = tar::Archive::new(tar);
archive.unpack(folder_path).log(pos!())?;
std::fs::remove_file(path).log(pos!())?;
for entry in walkdir::WalkDir::new(folder_path).into_iter().filter_map(Result::ok) {
if entry.file_type().is_file() {
println!("replace: {}", entry.path().to_string_lossy());
let mut content = std::fs::read_to_string(entry.path()).log(pos!())?;
content = content.replace(
"codeberg.org/automation-tasks-rs/cargo_auto_template_new_cli",
&format!("{repo_domain}/{owner_or_organization}/{rust_project_name}"),
);
content = content.replace(
"automation-tasks-rs/cargo_auto_template_new_cli",
&format!("{owner_or_organization}/{rust_project_name}"),
);
content = content.replace("cargo_auto_template_new_cli", rust_project_name);
content = content.replace("CARGO_AUTO_TEMPLATE_NEW_CLI", &rust_project_name.to_uppercase());
std::fs::write(entry.path(), content).log(pos!())?;
}
}
let mut traverse_reverse: Vec<walkdir::DirEntry> = walkdir::WalkDir::new(folder_path).into_iter().filter_map(Result::ok).collect();
traverse_reverse.reverse();
for entry in traverse_reverse.iter() {
if entry.file_name() == "cargo_auto_template_new_cli" {
println!("rename: {}", entry.path().to_string_lossy());
use anyhow::Context;
std::fs::rename(
entry.path(),
entry.path().parent().context("parent is None")?.join(rust_project_name),
)
.log(pos!())?;
}
}
Ok(())
}