use crate::error::FetchiumError;
use crate::plugin::manifest::PluginManifest;
pub fn install_plugin(
source_path: &std::path::Path,
plugin_dir: &std::path::Path,
) -> Result<PluginManifest, FetchiumError> {
let manifest_path = source_path.join("plugin.toml");
if !manifest_path.exists() {
return Err(FetchiumError::Config(format!(
"No plugin.toml found at {:?}",
source_path
)));
}
let manifest = PluginManifest::load(&manifest_path)?;
let dest = plugin_dir.join(&manifest.name);
if dest.exists() {
return Err(FetchiumError::Config(format!(
"Plugin '{}' is already installed at {:?}. Remove it first.",
manifest.name, dest
)));
}
copy_dir(source_path, &dest)?;
tracing::info!(plugin = %manifest.name, "Plugin installed");
Ok(manifest)
}
pub fn remove_plugin(name: &str, plugin_dir: &std::path::Path) -> Result<(), FetchiumError> {
let dest = plugin_dir.join(name);
if !dest.exists() {
return Err(FetchiumError::Config(format!(
"Plugin '{name}' is not installed"
)));
}
std::fs::remove_dir_all(&dest)?;
tracing::info!(plugin = name, "Plugin removed");
Ok(())
}
pub fn create_plugin_scaffold(
name: &str,
plugin_type: &str,
dest_path: &std::path::Path,
) -> Result<(), FetchiumError> {
std::fs::create_dir_all(dest_path)?;
PluginManifest::scaffold(name, plugin_type, &dest_path.join("plugin.toml"))?;
std::fs::write(
dest_path.join("README.md"),
format!("# {name}\n\nA Fetchium `{plugin_type}` plugin.\n"),
)?;
tracing::info!(name, plugin_type, "Plugin scaffold created");
Ok(())
}
fn copy_dir(src: &std::path::Path, dst: &std::path::Path) -> Result<(), FetchiumError> {
std::fs::create_dir_all(dst)?;
for entry in std::fs::read_dir(src)? {
let entry = entry?;
let src_path = entry.path();
let dst_path = dst.join(entry.file_name());
if src_path.is_dir() {
copy_dir(&src_path, &dst_path)?;
} else {
std::fs::copy(&src_path, &dst_path)?;
}
}
Ok(())
}