Skip to content

Commit

Permalink
feat(core): consistent dynamic plugins install/remove
Browse files Browse the repository at this point in the history
dynamic plugins now get saved persistently
on remove dynamic plugins get stopped
do not return on every Err, if it's expected

part of coffee-tools#245
  • Loading branch information
daywalker90 committed Feb 27, 2024
1 parent e14d7d5 commit 7eb089b
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions coffee_core/src/coffee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ impl CoffeeManager {
Ok(())
}

pub async fn stop_plugin(&self, path: &str) -> Result<(), CoffeeError> {
let mut payload = json_utils::init_payload();
json_utils::add_str(&mut payload, "subcommand", "stop");
json_utils::add_str(&mut payload, "plugin", path);
let response = self
.cln::<serde_json::Value, serde_json::Value>("plugin", payload)
.await?;
log::debug!("plugin stopped: {response}");
Ok(())
}

pub fn storage_info(&self) -> CoffeeStorageInfo {
CoffeeStorageInfo::from(self)
}
Expand Down Expand Up @@ -300,6 +311,8 @@ impl PluginManager for CoffeeManager {
self.flush().await?;
self.update_conf().await?;
} else {
self.config.plugins.push(plugin);
self.flush().await?;
self.start_plugin(&path).await?;
}
return Ok(());
Expand Down Expand Up @@ -328,9 +341,19 @@ impl PluginManager for CoffeeManager {
log::debug!("runnable plugin path: {exec_path}");
plugins.remove(index);
log::debug!("coffee cln config: {}", self.coffee_cln_config);
self.coffee_cln_config
.rm_conf("plugin", Some(&exec_path.to_owned()))
.map_err(|err| error!("{}", &err.cause))?;
let remove_config = self
.coffee_cln_config
.rm_conf("plugin", Some(&exec_path.to_owned()));
if let Err(err) = remove_config {
// if this is true, we are probably a dynamic plugin:
if err.cause.contains("field with `plugin` not present") {
if let Err(e) = self.stop_plugin(&exec_path).await {
log::warn!("{}", e);
};
} else {
return Err(error!("{}", &err.cause));
}
}
self.flush().await?;
self.update_conf().await?;
Ok(CoffeeRemove { plugin })
Expand Down

0 comments on commit 7eb089b

Please sign in to comment.