pub mod archives;
use anyhow::Result;
use std::path::PathBuf;
pub fn prepare_source(source: &str) -> Result<PreparedSource> {
if source.starts_with("http://") || source.starts_with("https://") {
return archives::download_and_extract(source);
}
let path = PathBuf::from(source);
if path.is_file() {
return archives::extract_archive(&path);
}
if path.is_dir() {
return Ok(PreparedSource {
path,
is_temporary: false,
});
}
anyhow::bail!("Source path does not exist: {}", source);
}
pub struct PreparedSource {
pub path: PathBuf,
pub is_temporary: bool,
}
impl PreparedSource {
pub fn path(&self) -> &PathBuf {
&self.path
}
}
impl Drop for PreparedSource {
fn drop(&mut self) {
if self.is_temporary {
if let Err(e) = std::fs::remove_dir_all(&self.path) {
tracing::warn!(
"Failed to clean up temporary directory {}: {}",
self.path.display(),
e
);
}
}
}
}