cln-plugin: Handle --help invocations better

We now have ternary outcomes for `Builder.configure()` and
`Builder.start()`:

 - Ok(Some(p)) means we were configured correctly, and can continue
   with our work normally
 - Ok(None) means that `lightningd` was invoked with `--help`, we
   weren't configured (which is not an error since the `lightningd` just
   implicitly told us to shut down) and user code should clean up and
   exit as well
 - Err(e) something went wrong, user code may report an error and exit.
This commit is contained in:
Christian Decker
2022-04-10 16:56:13 +02:00
committed by Rusty Russell
parent c36cef08bc
commit b359a24772
3 changed files with 41 additions and 13 deletions

View File

@@ -6,7 +6,7 @@ use cln_plugin::{options, Builder, Error, Plugin};
use tokio;
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let plugin = Builder::new((), tokio::io::stdin(), tokio::io::stdout())
if let Some(plugin) = Builder::new((), tokio::io::stdin(), tokio::io::stdout())
.option(options::ConfigOption::new(
"test-option",
options::Value::Integer(42),
@@ -16,8 +16,12 @@ async fn main() -> Result<(), anyhow::Error> {
.subscribe("connect", connect_handler)
.hook("peer_connected", peer_connected_handler)
.start()
.await?;
plugin.join().await
.await?
{
plugin.join().await
} else {
Ok(())
}
}
async fn testmethod(_p: Plugin<()>, _v: serde_json::Value) -> Result<serde_json::Value, Error> {
@@ -29,7 +33,10 @@ async fn connect_handler(_p: Plugin<()>, v: serde_json::Value) -> Result<(), Err
Ok(())
}
async fn peer_connected_handler(_p: Plugin<()>, v: serde_json::Value) -> Result<serde_json::Value, Error> {
async fn peer_connected_handler(
_p: Plugin<()>,
v: serde_json::Value,
) -> Result<serde_json::Value, Error> {
log::info!("Got a connect hook call: {}", v);
Ok(json!({"result": "continue"}))
}