Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add coffee unlink Command #279

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
755 changes: 397 additions & 358 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion coffee_cmd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async-trait = "0.1.57"
coffee_core = { path = "../coffee_core" }
coffee_lib = { path = "../coffee_lib" }
log = "0.4.17"
env_logger = "0.9.3"
env_logger = "0.11"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
clightningrpc-conf = "0.0.3"
Expand Down
14 changes: 9 additions & 5 deletions coffee_cmd/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ pub struct CoffeeArgs {
/// Coffee subcommand of the command line daemon.
#[derive(Debug, Subcommand)]
pub enum CoffeeCommand {
/// Configure coffee with the core lightning
/// 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 {
Expand Down Expand Up @@ -56,10 +63,6 @@ pub enum CoffeeCommand {
#[arg(name = "remote-name", help = "The name of the remote repository")]
name: Option<String>,
},
/// Configure coffee with the core lightning
/// configuration
#[clap(arg_required_else_help = true)]
Setup { cln_conf: String },
/// show the README file of the plugin
#[clap(arg_required_else_help = true)]
Show { plugin: String },
Expand Down Expand Up @@ -99,14 +102,15 @@ pub enum RemoteAction {
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,
dynamic,
} => Self::Install(plugin.to_owned(), *verbose, *dynamic),
CoffeeCommand::Upgrade { repo, verbose } => Self::Upgrade(repo.to_owned(), *verbose),
CoffeeCommand::List {} => Self::List,
CoffeeCommand::Setup { cln_conf } => Self::Setup(cln_conf.to_owned()),
CoffeeCommand::Remote { action, name } => {
if let Some(action) = action {
return Self::Remote(Some(action.into()), name.clone());
Expand Down
13 changes: 8 additions & 5 deletions coffee_cmd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ use crate::cmd::RemoteAction;

async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeError> {
match args.command {
CoffeeCommand::Link { cln_conf } => {
// FIXME: read the core lightning config
// and the coffee script
coffee.link(&cln_conf).await?;
}
CoffeeCommand::Unlink { cln_conf } => {
coffee.unlink(&cln_conf).await?;
}
CoffeeCommand::Install {
plugin,
verbose,
Expand Down Expand Up @@ -134,11 +142,6 @@ async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeEr
}
}
}
CoffeeCommand::Setup { cln_conf } => {
// FIXME: read the core lightning config
// and the coffee script
coffee.setup(&cln_conf).await?;
}
CoffeeCommand::Show { plugin } => {
let val = coffee.show(&plugin).await?;

Expand Down
8 changes: 4 additions & 4 deletions coffee_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
env_logger = "0.11"
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 }
27 changes: 24 additions & 3 deletions coffee_core/src/coffee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl CoffeeManager {
Ok(())
}

pub async fn setup_with_cln(&mut self, cln_dir: &str) -> Result<(), CoffeeError> {
pub async fn link_with_cln(&mut self, cln_dir: &str) -> Result<(), CoffeeError> {
if self.cln_config.is_some() {
log::warn!("you are overriding the previous set up");
}
Expand All @@ -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]
Expand Down Expand Up @@ -417,13 +431,20 @@ impl PluginManager for CoffeeManager {
Ok(status)
}

async fn setup(&mut self, cln_dir: &str) -> Result<(), CoffeeError> {
self.setup_with_cln(cln_dir).await?;
async fn link(&mut self, cln_dir: &str) -> Result<(), CoffeeError> {
self.link_with_cln(cln_dir).await?;
log::info!("cln configured");
self.flush().await?;
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`
Expand Down
6 changes: 4 additions & 2 deletions coffee_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ pub use coffee_lib as lib;

#[derive(Clone, Debug)]
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
Expand All @@ -16,8 +20,6 @@ pub enum CoffeeOperation {
Remove(String),
/// Remote(name repository, url of the repository)
Remote(Option<RemoteAction>, Option<String>),
/// Setup(core lightning root path)
Setup(String),
Show(String),
/// Search(plugin name)
Search(String),
Expand Down
2 changes: 1 addition & 1 deletion coffee_github/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async-trait = "0.1.57"
tokio = { version = "1.22.0", features = ["fs"] }
git2 = "^0.18.1"
log = "0.4.17"
env_logger = "0.9.3"
env_logger = "0.11"
serde_yaml = "^0.9.0"
walkdir = "2.3.2"
chrono = { version = "0.4", features = ["std"], default-features = false }
2 changes: 1 addition & 1 deletion coffee_httpd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ coffee_core = { path = "../coffee_core" }
coffee_lib = { path = "../coffee_lib", features = ["open-api"] }
paperclip = { version = "0.8.0", features = ["actix4"] }
log = "0.4.17"
env_logger = "0.9.3"
env_logger = "0.11"
serde = "1"
serde_json = "1"
2 changes: 1 addition & 1 deletion coffee_httpd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async fn main() -> Result<(), CoffeeError> {
env_logger::init();
let cmd = cmd::HttpdArgs::parse();
let mut coffee = CoffeeManager::new(&cmd).await?;
coffee.setup(&cmd.cln_path).await?;
coffee.link(&cmd.cln_path).await?;

let port = cmd.port.unwrap_or(8080) as u16;
log::info!("Running on port 127.0.0.1:{port}");
Expand Down
2 changes: 1 addition & 1 deletion coffee_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
git2 = "^0.18.1"
log = "0.4.17"
env_logger = "0.9.3"
env_logger = "0.11"
tokio = { version = "1.22.0", features = ["process", "fs"] }
paperclip = { version = "0.8.0", features = ["actix4"], optional = true }

Expand Down
8 changes: 5 additions & 3 deletions coffee_lib/src/plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ pub trait PluginManager {
/// List the plugins available in a remote repository.
async fn get_plugins_in_remote(&self, name: &str) -> Result<CoffeeList, CoffeeError>;

/// set up the core lightning configuration target for the
/// plugin manager.
async fn setup(&mut self, cln_conf_path: &str) -> Result<(), CoffeeError>;
/// 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<CoffeeShow, CoffeeError>;
Expand Down
2 changes: 1 addition & 1 deletion coffee_plugin/src/plugin/plugin_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn on_init(plugin: &mut Plugin<State>) -> Value {
}
let coffee = coffee.unwrap();
plugin.state.set_coffee(coffee);
plugin.state.setup().await
plugin.state.link().await
});

if let Err(err) = result {
Expand Down
4 changes: 2 additions & 2 deletions coffee_plugin/src/plugin/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ impl State {
self.args.clone().unwrap()
}

pub async fn setup(&self) -> Result<(), CoffeeError> {
pub async fn link(&self) -> Result<(), CoffeeError> {
self.coffee()
.lock()
.unwrap()
.setup(&self.args.clone().unwrap().conf)
.link(&self.args.clone().unwrap().conf)
.await?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion docs/docs-book/src/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ In fact, you can install a Java plugin that supports Coffee in a couple of
commands after you [install it](./install-coffee.md)

```bash
coffee --network testnet setup /home/alice/.lightning
coffee --network testnet link /home/alice/.lightning
coffee --network testnet remote add lightningd https://github.com/lightningd/plugins.git
coffee --network testnet install btcli4j
```
Expand Down
9 changes: 8 additions & 1 deletion docs/docs-book/src/using-coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ By default the Core Lightning home is stored in the `/home/<user>/.lightning`,
and you can do it with the following command

```bash
coffee setup /home/alice/.lightning
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
Expand Down
22 changes: 9 additions & 13 deletions tests/src/coffee_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub async fn init_coffee_test_with_cln() -> anyhow::Result<()> {
let lightning_dir = lightning_dir.strip_suffix("/regtest").unwrap();
log::info!("lightning path: {lightning_dir}");

manager.coffee().setup(&lightning_dir).await?;
manager.coffee().link(&lightning_dir).await?;

Ok(())
}
Expand All @@ -103,7 +103,7 @@ pub async fn init_coffee_test_add_remote() {
let lightning_dir = lightning_dir.strip_suffix("/regtest").unwrap();
log::info!("lightning path: {lightning_dir}");

manager.coffee().setup(&lightning_dir).await.unwrap();
manager.coffee().link(&lightning_dir).await.unwrap();

manager
.coffee()
Expand Down Expand Up @@ -135,7 +135,7 @@ pub async fn test_add_remove_plugins() {
let lightning_dir = lightning_dir.strip_suffix("/regtest").unwrap();
log::info!("lightning path: {lightning_dir}");

manager.coffee().setup(&lightning_dir).await.unwrap();
manager.coffee().link(&lightning_dir).await.unwrap();
// Add lightningd remote repository
let repo_name = "lightningd";
let repo_url = "https://github.com/lightningd/plugins.git";
Expand Down Expand Up @@ -254,7 +254,7 @@ pub async fn test_errors_and_show() {
let lightning_dir = lightning_dir.strip_suffix("/regtest").unwrap();
log::info!("lightning path: {lightning_dir}");

manager.coffee().setup(&lightning_dir).await.unwrap();
manager.coffee().link(&lightning_dir).await.unwrap();
// Add lightningd remote repository
let repo_name = "lightningd";
let repo_url = "https://github.com/lightningd/plugins.git";
Expand Down Expand Up @@ -341,7 +341,7 @@ pub async fn install_plugin_in_two_networks() -> anyhow::Result<()> {
network: "regtest".to_string(),
};
let mut manager = CoffeeTesting::tmp_with_args(&args, dir.clone()).await?;
let result = manager.coffee().setup(&lightning_regtest_dir).await;
let result = manager.coffee().link(&lightning_regtest_dir).await;
assert!(result.is_ok(), "{:?}", result);
// Add lightningd remote repository
manager
Expand Down Expand Up @@ -378,11 +378,7 @@ pub async fn install_plugin_in_two_networks() -> anyhow::Result<()> {
let mut manager = CoffeeTesting::tmp_with_args(&new_args, dir.clone()).await?;
let new_root_path = manager.root_path().to_owned();
assert_eq!(dir.path(), new_root_path.path());
manager
.coffee()
.setup(&lightning_testnet_dir)
.await
.unwrap();
manager.coffee().link(&lightning_testnet_dir).await.unwrap();

let result = manager
.coffee()
Expand Down Expand Up @@ -416,7 +412,7 @@ pub async fn test_double_slash() {
let lightning_dir = cln.rpc().getinfo().unwrap().ligthning_dir;
let lightning_dir = lightning_dir.strip_suffix("/regtest").unwrap();
log::info!("lightning path: {lightning_dir}");
manager.coffee().setup(&lightning_dir).await.unwrap();
manager.coffee().link(&lightning_dir).await.unwrap();

// Add lightningd remote repository
let repo_name = "lightningd";
Expand Down Expand Up @@ -481,7 +477,7 @@ pub async fn test_plugin_installation_path() {
let lightning_dir = lightning_dir.strip_suffix("/regtest").unwrap();
log::info!("lightning path: {lightning_dir}");

manager.coffee().setup(&lightning_dir).await.unwrap();
manager.coffee().link(&lightning_dir).await.unwrap();

// Add lightningd remote repository
manager
Expand Down Expand Up @@ -589,7 +585,7 @@ pub async fn test_nurse_repository_missing_on_disk() {
let lightning_dir = lightning_dir.strip_suffix("/regtest").unwrap();
log::info!("lightning path: {lightning_dir}");

manager.coffee().setup(&lightning_dir).await.unwrap();
manager.coffee().link(&lightning_dir).await.unwrap();

// Construct the root path
let root_path = manager
Expand Down
Loading