fetchium_core/plugin/
loader.rs1use crate::error::FetchiumError;
4use crate::plugin::manifest::PluginManifest;
5
6pub fn install_plugin(
11 source_path: &std::path::Path,
12 plugin_dir: &std::path::Path,
13) -> Result<PluginManifest, FetchiumError> {
14 let manifest_path = source_path.join("plugin.toml");
15 if !manifest_path.exists() {
16 return Err(FetchiumError::Config(format!(
17 "No plugin.toml found at {:?}",
18 source_path
19 )));
20 }
21 let manifest = PluginManifest::load(&manifest_path)?;
22 let dest = plugin_dir.join(&manifest.name);
23 if dest.exists() {
24 return Err(FetchiumError::Config(format!(
25 "Plugin '{}' is already installed at {:?}. Remove it first.",
26 manifest.name, dest
27 )));
28 }
29 copy_dir(source_path, &dest)?;
30 tracing::info!(plugin = %manifest.name, "Plugin installed");
31 Ok(manifest)
32}
33
34pub fn remove_plugin(name: &str, plugin_dir: &std::path::Path) -> Result<(), FetchiumError> {
36 let dest = plugin_dir.join(name);
37 if !dest.exists() {
38 return Err(FetchiumError::Config(format!(
39 "Plugin '{name}' is not installed"
40 )));
41 }
42 std::fs::remove_dir_all(&dest)?;
43 tracing::info!(plugin = name, "Plugin removed");
44 Ok(())
45}
46
47pub fn create_plugin_scaffold(
49 name: &str,
50 plugin_type: &str,
51 dest_path: &std::path::Path,
52) -> Result<(), FetchiumError> {
53 std::fs::create_dir_all(dest_path)?;
54 PluginManifest::scaffold(name, plugin_type, &dest_path.join("plugin.toml"))?;
55 std::fs::write(
57 dest_path.join("README.md"),
58 format!("# {name}\n\nA Fetchium `{plugin_type}` plugin.\n"),
59 )?;
60 tracing::info!(name, plugin_type, "Plugin scaffold created");
61 Ok(())
62}
63
64fn copy_dir(src: &std::path::Path, dst: &std::path::Path) -> Result<(), FetchiumError> {
65 std::fs::create_dir_all(dst)?;
66 for entry in std::fs::read_dir(src)? {
67 let entry = entry?;
68 let src_path = entry.path();
69 let dst_path = dst.join(entry.file_name());
70 if src_path.is_dir() {
71 copy_dir(&src_path, &dst_path)?;
72 } else {
73 std::fs::copy(&src_path, &dst_path)?;
74 }
75 }
76 Ok(())
77}