From d002a267850b76e03701d5cdf88fc5edaceff7e3 Mon Sep 17 00:00:00 2001 From: Tarek Date: Sat, 10 Feb 2024 14:23:47 +0200 Subject: [PATCH] feat(coffee): add coffee enable and disable commands This commit introduces coffee disable and coffee enable commands, enabling users to toggle plugin status without complete uninstallation. Additionally, it includes the implementation of corresponding methods in CoffeeManager and updates to CLI commands and documentation. Signed-off-by: Tarek --- coffee_cmd/src/cmd.rs | 8 ++++ coffee_cmd/src/main.rs | 8 ++++ coffee_core/src/coffee.rs | 62 ++++++++++++++++++++++++++++++ coffee_core/src/lib.rs | 4 ++ coffee_lib/src/plugin_manager.rs | 6 +++ docs/docs-book/src/using-coffee.md | 50 ++++++++++++++++++------ 6 files changed, 127 insertions(+), 11 deletions(-) diff --git a/coffee_cmd/src/cmd.rs b/coffee_cmd/src/cmd.rs index 6f60502..cf1a474 100644 --- a/coffee_cmd/src/cmd.rs +++ b/coffee_cmd/src/cmd.rs @@ -73,6 +73,12 @@ pub enum CoffeeCommand { /// tipping a plugins developer. #[clap(arg_required_else_help = false)] Tip { plugin: String, amount_msat: u64 }, + /// Disable a plugin + #[clap(arg_required_else_help = true)] + Disable { plugin: String }, + /// Enable a plugin + #[clap(arg_required_else_help = true)] + Enable { plugin: String }, } #[derive(Debug, Subcommand)] @@ -114,6 +120,8 @@ impl From<&CoffeeCommand> for coffee_core::CoffeeOperation { plugin, amount_msat, } => Self::Tip(plugin.to_owned(), amount_msat.clone()), + CoffeeCommand::Disable { plugin } => Self::Disable(plugin.to_owned()), + CoffeeCommand::Enable { plugin } => Self::Enable(plugin.to_owned()), } } } diff --git a/coffee_cmd/src/main.rs b/coffee_cmd/src/main.rs index 623a6f2..d5708fa 100644 --- a/coffee_cmd/src/main.rs +++ b/coffee_cmd/src/main.rs @@ -176,6 +176,14 @@ async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeEr let tip_result = coffee.tip(&plugin, amount_msat).await?; coffee_term::show_tips(&tip_result)?; } + CoffeeCommand::Disable { plugin } => { + coffee.disable(&plugin).await?; + term::success!("Plugin {plugin} disabled"); + } + CoffeeCommand::Enable { plugin } => { + coffee.enable(&plugin).await?; + term::success!("Plugin {plugin} enabled"); + } }; Ok(()) } diff --git a/coffee_core/src/coffee.rs b/coffee_core/src/coffee.rs index 47fbb98..511eb2c 100644 --- a/coffee_core/src/coffee.rs +++ b/coffee_core/src/coffee.rs @@ -597,6 +597,68 @@ impl PluginManager for CoffeeManager { .await?; Ok(tip) } + + async fn disable(&mut self, plugin: &str) -> Result<(), CoffeeError> { + log::debug!("disabling plugin: {plugin}"); + + let plugin = self + .config + .plugins + .iter_mut() + .find(|repo_plugin| plugin == repo_plugin.name()) + .ok_or(error!( + "No plugin with name `{plugin}` found in the plugins installed" + ))?; + log::debug!("plugin: {:?}", plugin); + if plugin.enabled == Some(false) { + return Err(error!("Plugin `{plugin}` is already disabled")); + } + self.coffee_cln_config + .rm_conf("plugin", Some(&plugin.exec_path)) + .map_err(|err| error!("{}", err.cause))?; + log::debug!( + "Plugin {} was removed from CLN configuration successfully", + plugin.name() + ); + plugin.enabled = Some(false); + + self.flush().await?; + self.update_conf().await?; + + Ok(()) + } + + async fn enable(&mut self, plugin: &str) -> Result<(), CoffeeError> { + log::debug!("enabling plugin: {plugin}"); + + let plugin = self + .config + .plugins + .iter_mut() + .find(|repo_plugin| plugin == repo_plugin.name()) + .ok_or(error!( + "No plugin with name `{plugin}` found in the plugins installed" + ))?; + log::debug!("plugin: {:?}", plugin); + if plugin.enabled.is_none() || plugin.enabled == Some(true) { + return Err(error!( + "Plugin `{plugin}` is already enabled or enabled by default" + )); + } + self.coffee_cln_config + .add_conf("plugin", &plugin.exec_path) + .map_err(|err| error!("{}", err.cause))?; + log::debug!( + "Plugin {} was added to CLN configuration successfully", + plugin.name() + ); + plugin.enabled = Some(true); + + self.flush().await?; + self.update_conf().await?; + + Ok(()) + } } // FIXME: we need to move on but this is not safe and with the coffee diff --git a/coffee_core/src/lib.rs b/coffee_core/src/lib.rs index fe695e1..d237d49 100644 --- a/coffee_core/src/lib.rs +++ b/coffee_core/src/lib.rs @@ -26,6 +26,10 @@ pub enum CoffeeOperation { /// /// (plugin_name, amount_msat) Tip(String, u64), + /// Disable a plugin(plugin name) + Disable(String), + /// Enable a plugin(plugin name) + Enable(String), } #[derive(Clone, Debug)] diff --git a/coffee_lib/src/plugin_manager.rs b/coffee_lib/src/plugin_manager.rs index 65bca03..103b77e 100644 --- a/coffee_lib/src/plugin_manager.rs +++ b/coffee_lib/src/plugin_manager.rs @@ -69,4 +69,10 @@ pub trait PluginManager { /// /// P.S: only Bitcoin ofc async fn tip(&mut self, plugin: &str, amount_msat: u64) -> Result; + + /// disable a plugin by name + async fn disable(&mut self, plugin: &str) -> Result<(), CoffeeError>; + + /// enable a plugin by name + async fn enable(&mut self, plugin: &str) -> Result<(), CoffeeError>; } diff --git a/docs/docs-book/src/using-coffee.md b/docs/docs-book/src/using-coffee.md index a23d367..4a7e0d4 100644 --- a/docs/docs-book/src/using-coffee.md +++ b/docs/docs-book/src/using-coffee.md @@ -32,11 +32,11 @@ include /home/alice/.coffee/testnet/coffee.conf In addition there are the following additional option that you can specify: - `--network`: by default set to `bitcoin`, but if you want to specify the network -that Core Lightning is using, you must ensure that the flag is set to -the correct network. + that Core Lightning is using, you must ensure that the flag is set to + the correct network. - `--data-dir`: by default set to `/home/alice/.coffee`, you may want to set -this option if you are looking to specify a different directory for the -Coffee home. + this option if you are looking to specify a different directory for the + Coffee home. - `--skip-verify`: Use this option to bypass `coffee`'s validation process, which checks for conflicts between its configuration and the local storage. ### Add a Plugin Repository @@ -66,7 +66,7 @@ To list plugin repositories, simply run the following command. > ✅ Implemented ```bash -coffee remote list +coffee remote list ``` To list available plugins in a specific remote repository @@ -114,10 +114,34 @@ To remove an installed plugin, you simply have to run the following command. coffee remove ``` +### Disabling a Plugin + +> ✅ Implemented + +Disabling a plugin means that the plugin will not be loaded with CLN but it will still be installed and can be enabled at any time. + +To disable a plugin, run: + +```bash +coffee disable +``` + +### Enabling a Plugin + +> ✅ Implemented + +To enable a plugin, run: + +```bash +coffee enable +``` + ### Upgrade a Plugin Coffee tightly integrates with git, allowing you to easily upgrade your plugins through the command line interface (CLI). This eliminates the need for tedious tasks such as downloading the latest updates and creating new versions of plugins. To upgrade a plugin, all you need to do is run. + > ✅ Implemented + ```bash coffee upgrade ``` @@ -138,7 +162,7 @@ coffee list coffee show ``` -### Searching for a plugin in remote repositories +### Searching for a plugin in remote repositories > ✅ Implemented @@ -153,12 +177,15 @@ coffee search ```bash coffee nurse ``` + Additionally, if you wish to perform a verification of coffee without making any changes, you can use the `--verify` flag: ```bash coffee nurse --verify ``` -_________ + +--- + ### Tipping a plugin in Bitcoin > ✅ Implemented @@ -167,7 +194,8 @@ _________ coffee tip ``` ------- +--- + ## Running coffee as a server To run Coffee as a server, you can use the `coffee_httpd` binary. @@ -178,8 +206,8 @@ Please note that the server runs on `localhost` with port `8080` where you can f To start the Coffee server, run the following command: - ```shell - coffee_httpd --cln-path --network - ``` +```shell +coffee_httpd --cln-path --network +``` Make sure the `coffee_httpd` binary is in your system PATH or in the current working directory.