From 20be671c111f86b6240e4a4714bf016a006b5ea6 Mon Sep 17 00:00:00 2001 From: Tarek Date: Wed, 29 May 2024 14:25:36 +0300 Subject: [PATCH] feat: add coffee unlink command - Add methods in `coffee_core` to enable unlinking coffee configuration from CLN configuration - Update `coffee_cmd` crate to support the new `unlink` command - Add documentation for the new `unlink` command Note: The `clightningrpc-conf` from git is required to use the `rm_subconf()` method. Signed-off-by: Tarek --- Cargo.lock | 13 +++++++++++-- coffee_cmd/src/cmd.rs | 4 ++++ coffee_cmd/src/main.rs | 3 +++ coffee_core/Cargo.toml | 6 +++--- coffee_core/src/coffee.rs | 21 +++++++++++++++++++++ coffee_core/src/lib.rs | 2 ++ coffee_lib/src/plugin_manager.rs | 3 +++ docs/docs-book/src/using-coffee.md | 7 +++++++ 8 files changed, 54 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1bed823..8076c96 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -575,6 +575,15 @@ dependencies = [ "indexmap 1.9.3", ] +[[package]] +name = "clightningrpc-conf" +version = "0.0.3" +source = "git+https://github.com/laanwj/cln4rust#a278c14e334cef169ed5e378f71ec5eceae75d42" +dependencies = [ + "albert_stream", + "indexmap 1.9.3", +] + [[package]] name = "clightningrpc-plugin" version = "0.3.0-beta.8" @@ -606,7 +615,7 @@ dependencies = [ "async-trait", "clap", "clightningrpc-common", - "clightningrpc-conf", + "clightningrpc-conf 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "coffee_core", "coffee_lib", "env_logger", @@ -625,7 +634,7 @@ dependencies = [ "async-trait", "chrono", "clightningrpc-common", - "clightningrpc-conf", + "clightningrpc-conf 0.0.3 (git+https://github.com/laanwj/cln4rust)", "coffee_github", "coffee_lib", "coffee_storage", diff --git a/coffee_cmd/src/cmd.rs b/coffee_cmd/src/cmd.rs index 2c9766a..04c4607 100644 --- a/coffee_cmd/src/cmd.rs +++ b/coffee_cmd/src/cmd.rs @@ -30,6 +30,9 @@ pub enum CoffeeCommand { /// configuration #[clap(arg_required_else_help = true)] Link { cln_conf: String }, + /// Unlink coffee from the core lightning configuration + #[clap(arg_required_else_help = true)] + Unlink { cln_conf: String }, /// Install a single by name. #[clap(arg_required_else_help = true)] Install { @@ -100,6 +103,7 @@ impl From<&CoffeeCommand> for coffee_core::CoffeeOperation { fn from(value: &CoffeeCommand) -> Self { match value { CoffeeCommand::Link { cln_conf } => Self::Link(cln_conf.to_owned()), + CoffeeCommand::Unlink { cln_conf } => Self::Unlink(cln_conf.to_owned()), CoffeeCommand::Install { plugin, verbose, diff --git a/coffee_cmd/src/main.rs b/coffee_cmd/src/main.rs index de8aaa9..ae1620f 100644 --- a/coffee_cmd/src/main.rs +++ b/coffee_cmd/src/main.rs @@ -21,6 +21,9 @@ async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeEr // and the coffee script coffee.link(&cln_conf).await?; } + CoffeeCommand::Unlink { cln_conf } => { + coffee.unlink(&cln_conf).await?; + } CoffeeCommand::Install { plugin, verbose, diff --git a/coffee_core/Cargo.toml b/coffee_core/Cargo.toml index bf1179f..5f4d65f 100644 --- a/coffee_core/Cargo.toml +++ b/coffee_core/Cargo.toml @@ -8,13 +8,13 @@ edition = "2021" tokio = { version = "1.22.0", features = ["full"] } async-trait = "0.1.57" coffee_lib = { path = "../coffee_lib" } -coffee_github = { path = "../coffee_github" } +coffee_github = { path = "../coffee_github" } log = "0.4.17" env_logger = "0.9.3" -coffee_storage = { path = "../coffee_storage" } +coffee_storage = { path = "../coffee_storage" } serde = { version = "1.0", features = ["derive"] } serde_json = "1" -clightningrpc-conf = "0.0.3" +clightningrpc-conf = { git = "https://github.com/laanwj/cln4rust" } clightningrpc-common = "0.3.0-beta.4" git2 = "^0.18.1" chrono = { version = "0.4", features = ["std"], default-features = false } diff --git a/coffee_core/src/coffee.rs b/coffee_core/src/coffee.rs index 7db51b1..bdedda5 100644 --- a/coffee_core/src/coffee.rs +++ b/coffee_core/src/coffee.rs @@ -243,6 +243,20 @@ impl CoffeeManager { conf.flush()?; Ok(()) } + + /// Unlink coffee from the core lightning configuration file + pub async fn unlink_from_cln(&mut self, cln_dir: &str) -> Result<(), CoffeeError> { + if self.cln_config.is_none() { + return Err(error!("no cln configuration found")); + } + let path_with_network = format!("{cln_dir}/{}/config", self.config.network); + log::info!("teardown coffee in the following cln config {path_with_network}"); + let mut conf = self.cln_config.clone().unwrap(); + conf.rm_subconf(&self.coffee_cln_config.clone().path) + .map_err(|err| error!("{}", &err.cause))?; + conf.flush()?; + Ok(()) + } } #[async_trait] @@ -424,6 +438,13 @@ impl PluginManager for CoffeeManager { Ok(()) } + async fn unlink(&mut self, cln_dir: &str) -> Result<(), CoffeeError> { + self.unlink_from_cln(cln_dir).await?; + log::info!("cln configuration removed"); + self.flush().await?; + Ok(()) + } + async fn add_remote(&mut self, name: &str, url: &str) -> Result<(), CoffeeError> { // FIXME: we should allow some error here like // for the add remote command the no found error for the `repository` diff --git a/coffee_core/src/lib.rs b/coffee_core/src/lib.rs index 0e091ea..40bd1d1 100644 --- a/coffee_core/src/lib.rs +++ b/coffee_core/src/lib.rs @@ -9,6 +9,8 @@ pub use coffee_lib as lib; pub enum CoffeeOperation { /// Link coffee to the lightning configuration file Link(String), + /// Unlink coffee from the lightning configuration file + Unlink(String), /// Install(plugin name, verbose run, dynamic installation) Install(String, bool, bool), /// List diff --git a/coffee_lib/src/plugin_manager.rs b/coffee_lib/src/plugin_manager.rs index 52f0725..a2fa0e3 100644 --- a/coffee_lib/src/plugin_manager.rs +++ b/coffee_lib/src/plugin_manager.rs @@ -42,6 +42,9 @@ pub trait PluginManager { /// Link coffee to CLN configuration file async fn link(&mut self, cln_conf_path: &str) -> Result<(), CoffeeError>; + /// Unlink coffee from CLN configuration file + async fn unlink(&mut self, cln_conf_path: &str) -> Result<(), CoffeeError>; + /// show the README file of the plugin async fn show(&mut self, plugin: &str) -> Result; diff --git a/docs/docs-book/src/using-coffee.md b/docs/docs-book/src/using-coffee.md index f12e2a3..be7a3e0 100644 --- a/docs/docs-book/src/using-coffee.md +++ b/docs/docs-book/src/using-coffee.md @@ -21,6 +21,13 @@ and you can do it with the following command coffee link /home/alice/.lightning ``` +If you want to unlink coffee from Core Lightning configuration at any time, you +can do it with the following command + +```bash +coffee unlink /home/alice/.lightning +``` + Then you will find an include at the end of the config file at `/home/alice/.lightning/bitcoin/config`, in case this config file do not exist Coffee will create it.