Skip to content

Commit

Permalink
allow passing parameters to execute_dev_command commands
Browse files Browse the repository at this point in the history
  • Loading branch information
JssDWt committed Apr 26, 2024
1 parent bf45757 commit 4de689c
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 39 deletions.
140 changes: 130 additions & 10 deletions libs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions libs/sdk-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ miniz_oxide = "0.7.1"
tokio-stream = "0.1.14"
serde_with = "3.3.0"
regex = "1.8.1"
clap = { version = "4.5.4", features = ["derive"] }
shlex = "1.3.0"

[dev-dependencies]
mockito = "1"
Expand Down
6 changes: 6 additions & 0 deletions libs/sdk-core/src/greenlight/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ impl From<TryFromIntError> for NodeError {
}
}

impl From<clap::Error> for NodeError {
fn from(err: clap::Error) -> Self {
Self::Generic(anyhow::Error::new(err))
}
}

#[allow(clippy::invalid_regex)]
pub(crate) fn parse_cln_error(status: tonic::Status) -> Result<JsonRpcErrCode> {
let re: Regex = Regex::new(r"Some\((?<code>-?\d+)\)")?;
Expand Down
19 changes: 15 additions & 4 deletions libs/sdk-core/src/greenlight/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::{anyhow, Result};
use clap::Parser;
use ecies::symmetric::{sym_decrypt, sym_encrypt};
use futures::Stream;
use gl_client::node::ClnClient;
Expand Down Expand Up @@ -1427,9 +1428,10 @@ impl NodeAPI for Greenlight {
}

async fn execute_command(&self, command: String) -> NodeResult<String> {
let node_cmd =
NodeCommand::from_str(&command).map_err(|_| anyhow!("Command not found: {command}"))?;
match node_cmd {
let mut args = shlex::split(&command).ok_or(anyhow!("Command not found: {command}"))?;
args.insert(0, String::from(" "));
let command = NodeCommand::try_parse_from(args)?;
match command {
NodeCommand::ListPeers => {
let resp = self
.get_node_client()
Expand Down Expand Up @@ -1666,37 +1668,45 @@ impl NodeAPI for Greenlight {
}
}

#[derive(Clone, PartialEq, Eq, Debug, EnumString, Display, Deserialize, Serialize)]
#[derive(Parser, Clone, PartialEq, Eq, Debug, EnumString, Display, Deserialize, Serialize)]
enum NodeCommand {
/// Generates diagnostic data report.
#[command(name = "generatediagnosticdata")]
#[strum(serialize = "generatediagnosticdata")]
GenerateDiagnosticData,

/// Closes all channels of all peers.
#[command(name = "closeallchannels")]
#[strum(serialize = "closeallchannels")]
CloseAllChannels,

/// See <https://docs.corelightning.org/reference/lightning-getinfo>
#[command(name = "getinfo")]
#[strum(serialize = "getinfo")]
GetInfo,

/// See <https://docs.corelightning.org/reference/lightning-listfunds>
#[command(name = "listfunds")]
#[strum(serialize = "listfunds")]
ListFunds,

/// See <https://docs.corelightning.org/reference/lightning-listinvoices>
#[command(name = "listinvoices")]
#[strum(serialize = "listinvoices")]
ListInvoices,

/// See <https://docs.corelightning.org/reference/lightning-listpays>
#[command(name = "listpayments")]
#[strum(serialize = "listpayments")]
ListPayments,

/// See <https://docs.corelightning.org/reference/lightning-listpeers>
#[command(name = "listpeers")]
#[strum(serialize = "listpeers")]
ListPeers,

/// See <https://docs.corelightning.org/reference/lightning-listpeerchannels>
#[command(name = "listpeerchannels")]
#[strum(serialize = "listpeerchannels")]
ListPeerChannels,

Expand All @@ -1705,6 +1715,7 @@ enum NodeCommand {
/// Note that this command will return an error, as the node is stopped before it can reply.
///
/// See <https://docs.corelightning.org/reference/lightning-stop>
#[command(name = "stop")]
#[strum(serialize = "stop")]
Stop,
}
Expand Down
Loading

0 comments on commit 4de689c

Please sign in to comment.