150 lines
4.4 KiB
Rust
150 lines
4.4 KiB
Rust
|
|
use std::fs;
|
||
|
|
use std::path::Path;
|
||
|
|
use std::process::{Command, Stdio};
|
||
|
|
|
||
|
|
use crate::utils::parser::{self, env::get_install_env};
|
||
|
|
use crate::utils::shell::run_install_script_hook;
|
||
|
|
|
||
|
|
pub fn download(repo: &String, pkgname: &String) -> Result<(), bool> {
|
||
|
|
let pkg_md_path = &crate::commands::get_var_path().join(format!("{}/{}.md", &repo, &pkgname));
|
||
|
|
if !pkg_md_path.exists() { upload_from_repo(&repo, &pkgname, &pkg_md_path)? }
|
||
|
|
|
||
|
|
let full_env = get_install_env(repo, pkgname, pkg_md_path, "download");
|
||
|
|
|
||
|
|
let url = match parser::pkginfo::get_url(pkg_md_path) {
|
||
|
|
Ok(url) => url,
|
||
|
|
Err(e) => {
|
||
|
|
eprintln!("Failed to parse URL: {}", e);
|
||
|
|
return Err(false);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
let src = crate::commands::get_aeropkg_base().join("src").join(pkgname);
|
||
|
|
if src.exists() {
|
||
|
|
let src_url = fs::read_to_string(src.join("aeropkg.download-url")).unwrap_or("".to_string());
|
||
|
|
if url == src_url {
|
||
|
|
return Ok(())
|
||
|
|
} else {
|
||
|
|
fs::remove_dir_all(&src).unwrap();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
if !url.ends_with(".git") {
|
||
|
|
if let Err(e) = fs::create_dir_all(&src) {
|
||
|
|
eprintln!("Failed to create directory {}: {}", &src.display(), e);
|
||
|
|
return Err(false);
|
||
|
|
}
|
||
|
|
let compress_flag = if url.ends_with(".bz2") {
|
||
|
|
"--bzip2"
|
||
|
|
} else if url.ends_with(".xz") {
|
||
|
|
"--xz"
|
||
|
|
} else if url.ends_with(".lz") {
|
||
|
|
"--lzip"
|
||
|
|
} else if url.ends_with(".lzma") {
|
||
|
|
"--lzma"
|
||
|
|
} else if url.ends_with(".lzo") {
|
||
|
|
"--lzop"
|
||
|
|
} else if url.ends_with(".zst") {
|
||
|
|
"--zstd"
|
||
|
|
} else if url.ends_with(".gz") {
|
||
|
|
"--gzip"
|
||
|
|
} else {
|
||
|
|
eprintln!("Unsupported compression format for URL: {}", url);
|
||
|
|
return Err(false);
|
||
|
|
};
|
||
|
|
|
||
|
|
let wget_output = Command::new("wget")
|
||
|
|
.arg("-O-")
|
||
|
|
.arg("-q")
|
||
|
|
.arg("--show-progress")
|
||
|
|
.arg(&url)
|
||
|
|
.envs(&full_env)
|
||
|
|
.stdout(Stdio::piped())
|
||
|
|
.spawn();
|
||
|
|
|
||
|
|
let tar_input = match wget_output {
|
||
|
|
Ok(child) => child.stdout.unwrap(),
|
||
|
|
Err(e) => {
|
||
|
|
eprintln!("Failed to execute wget: {}", e);
|
||
|
|
return Err(false);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
let tar_status = Command::new("tar")
|
||
|
|
.arg("-x")
|
||
|
|
.arg(compress_flag)
|
||
|
|
.arg("-C")
|
||
|
|
.arg(&src)
|
||
|
|
.envs(&full_env)
|
||
|
|
.stdin(tar_input)
|
||
|
|
.status();
|
||
|
|
|
||
|
|
if tar_status.is_err() || !tar_status.unwrap().success() {
|
||
|
|
eprintln!("Failed to extract archive from URL: {}", url);
|
||
|
|
return Err(false);
|
||
|
|
}
|
||
|
|
|
||
|
|
let entries = fs::read_dir(&src).unwrap();
|
||
|
|
let dirs: Vec<_> = entries
|
||
|
|
.filter_map(|entry| entry.ok())
|
||
|
|
.filter(|entry| entry.file_type().map_or(false, |ft| ft.is_dir()))
|
||
|
|
.collect();
|
||
|
|
|
||
|
|
if dirs.len() == 1 {
|
||
|
|
let single_dir = dirs[0].path();
|
||
|
|
|
||
|
|
for entry in fs::read_dir(&single_dir).unwrap() {
|
||
|
|
let entry = entry.unwrap();
|
||
|
|
let dest = src.join(entry.file_name());
|
||
|
|
fs::rename(entry.path(), dest).unwrap();
|
||
|
|
}
|
||
|
|
|
||
|
|
fs::remove_dir(single_dir).unwrap();
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
let git_status = Command::new("git")
|
||
|
|
.arg("clone")
|
||
|
|
.arg(&url)
|
||
|
|
.arg(&src)
|
||
|
|
.status();
|
||
|
|
|
||
|
|
if git_status.is_err() || !git_status.unwrap().success() {
|
||
|
|
eprintln!("Failed to clone git repository from URL: {}", url);
|
||
|
|
return Err(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fs::write(src.join("aeropkg.download-url"), &url).unwrap();
|
||
|
|
|
||
|
|
run_install_script_hook(repo, "download", &full_env)?;
|
||
|
|
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
|
||
|
|
fn upload_from_repo(repo: &String, pkgname: &String, pkg_md_path: &Path) -> Result<(), bool> {
|
||
|
|
let repo_addr = parser::repoinfo::get_repo_addr(repo);
|
||
|
|
let rsync_command = format!(
|
||
|
|
"rsync --include='{}.md' --exclude='*' {} {}",
|
||
|
|
pkgname,
|
||
|
|
repo_addr,
|
||
|
|
pkg_md_path.to_str().unwrap()
|
||
|
|
);
|
||
|
|
let rsync_output = Command::new("sh")
|
||
|
|
.arg("-c")
|
||
|
|
.arg(rsync_command)
|
||
|
|
.output()
|
||
|
|
.expect("Failed to execute rsync");
|
||
|
|
|
||
|
|
if !rsync_output.status.success() {
|
||
|
|
eprintln!("broken repo: {}", repo);
|
||
|
|
return Err(false);
|
||
|
|
}
|
||
|
|
if !pkg_md_path.exists() {
|
||
|
|
eprintln!("not found {} in {} repo", pkgname, repo);
|
||
|
|
return Err(true);
|
||
|
|
}
|
||
|
|
|
||
|
|
Ok(())
|
||
|
|
}
|