Skip to content

Commit ea7e625

Browse files
committed
Make cli output json
Previously we would output the debug format of each of our return times which worked, but was not very usable. You could not easily pipe results with cli tools or anything like that. This changes the results to be output to json to make it easier to use and more similiar to what people expect. This had one annoyance because since our structs are generated using prost, we cannot just simply add the serde traits to the structs, so instead we recreate the types we are going to output in the cli and covert them.
1 parent 281b5e8 commit ea7e625

File tree

4 files changed

+783
-18
lines changed

4 files changed

+783
-18
lines changed

Cargo.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ldk-server-cli/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ edition = "2021"
77
ldk-server-client = { path = "../ldk-server-client" }
88
clap = { version = "4.0.5", default-features = false, features = ["derive", "std", "error-context", "suggestions", "help"] }
99
tokio = { version = "1.38.0", default-features = false, features = ["rt-multi-thread", "macros"] }
10-
prost = { version = "0.11.6", default-features = false}
10+
serde = { version = "1.0", features = ["derive"] }
11+
serde_json = "1.0"
12+
hex-conservative = "0.2.1"

ldk-server-cli/src/main.rs

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
mod types;
2+
13
use clap::{Parser, Subcommand};
24
use ldk_server_client::client::LdkServerClient;
35
use ldk_server_client::error::LdkServerError;
@@ -13,7 +15,7 @@ use ldk_server_client::ldk_server_protos::api::{
1315
use ldk_server_client::ldk_server_protos::types::{
1416
bolt11_invoice_description, Bolt11InvoiceDescription, PageToken, Payment,
1517
};
16-
use std::fmt::Debug;
18+
use serde::Serialize;
1719

1820
#[derive(Parser, Debug)]
1921
#[command(version, about, long_about = None)]
@@ -119,16 +121,22 @@ async fn main() {
119121

120122
match cli.command {
121123
Commands::GetNodeInfo => {
122-
handle_response_result(client.get_node_info(GetNodeInfoRequest {}).await);
124+
handle_response_result::<_, types::GetNodeInfoResponse>(
125+
client.get_node_info(GetNodeInfoRequest {}).await,
126+
);
123127
},
124128
Commands::GetBalances => {
125-
handle_response_result(client.get_balances(GetBalancesRequest {}).await);
129+
handle_response_result::<_, types::GetBalancesResponse>(
130+
client.get_balances(GetBalancesRequest {}).await,
131+
);
126132
},
127133
Commands::OnchainReceive => {
128-
handle_response_result(client.onchain_receive(OnchainReceiveRequest {}).await);
134+
handle_response_result::<_, types::OnchainReceiveResponse>(
135+
client.onchain_receive(OnchainReceiveRequest {}).await,
136+
);
129137
},
130138
Commands::OnchainSend { address, amount_sats, send_all, fee_rate_sat_per_vb } => {
131-
handle_response_result(
139+
handle_response_result::<_, types::OnchainSendResponse>(
132140
client
133141
.onchain_send(OnchainSendRequest {
134142
address,
@@ -159,15 +167,17 @@ async fn main() {
159167
let request =
160168
Bolt11ReceiveRequest { description: invoice_description, expiry_secs, amount_msat };
161169

162-
handle_response_result(client.bolt11_receive(request).await);
170+
handle_response_result::<_, types::Bolt11ReceiveResponse>(
171+
client.bolt11_receive(request).await,
172+
);
163173
},
164174
Commands::Bolt11Send { invoice, amount_msat } => {
165-
handle_response_result(
175+
handle_response_result::<_, types::Bolt11SendResponse>(
166176
client.bolt11_send(Bolt11SendRequest { invoice, amount_msat }).await,
167177
);
168178
},
169179
Commands::Bolt12Receive { description, amount_msat, expiry_secs, quantity } => {
170-
handle_response_result(
180+
handle_response_result::<_, types::Bolt12ReceiveResponse>(
171181
client
172182
.bolt12_receive(Bolt12ReceiveRequest {
173183
description,
@@ -179,14 +189,14 @@ async fn main() {
179189
);
180190
},
181191
Commands::Bolt12Send { offer, amount_msat, quantity, payer_note } => {
182-
handle_response_result(
192+
handle_response_result::<_, types::Bolt12SendResponse>(
183193
client
184194
.bolt12_send(Bolt12SendRequest { offer, amount_msat, quantity, payer_note })
185195
.await,
186196
);
187197
},
188198
Commands::CloseChannel { user_channel_id, counterparty_node_id } => {
189-
handle_response_result(
199+
handle_response_result::<_, types::CloseChannelResponse>(
190200
client
191201
.close_channel(CloseChannelRequest { user_channel_id, counterparty_node_id })
192202
.await,
@@ -197,7 +207,7 @@ async fn main() {
197207
counterparty_node_id,
198208
force_close_reason,
199209
} => {
200-
handle_response_result(
210+
handle_response_result::<_, types::ForceCloseChannelResponse>(
201211
client
202212
.force_close_channel(ForceCloseChannelRequest {
203213
user_channel_id,
@@ -214,7 +224,7 @@ async fn main() {
214224
push_to_counterparty_msat,
215225
announce_channel,
216226
} => {
217-
handle_response_result(
227+
handle_response_result::<_, types::OpenChannelResponse>(
218228
client
219229
.open_channel(OpenChannelRequest {
220230
node_pubkey,
@@ -228,10 +238,14 @@ async fn main() {
228238
);
229239
},
230240
Commands::ListChannels => {
231-
handle_response_result(client.list_channels(ListChannelsRequest {}).await);
241+
handle_response_result::<_, types::ListChannelsResponse>(
242+
client.list_channels(ListChannelsRequest {}).await,
243+
);
232244
},
233245
Commands::ListPayments { number_of_payments } => {
234-
handle_response_result(list_n_payments(client, number_of_payments).await);
246+
handle_response_result::<_, types::ListPaymentsResponse>(
247+
list_n_payments(client, number_of_payments).await,
248+
);
235249
},
236250
}
237251
}
@@ -256,10 +270,21 @@ async fn list_n_payments(
256270
Ok(payments)
257271
}
258272

259-
fn handle_response_result<Rs: Debug>(response: Result<Rs, LdkServerError>) {
273+
fn handle_response_result<Rs, Js>(response: Result<Rs, LdkServerError>)
274+
where
275+
Rs: Into<Js>,
276+
Js: Serialize + std::fmt::Debug,
277+
{
260278
match response {
261279
Ok(response) => {
262-
println!("Success: {:?}", response);
280+
let json_response: Js = response.into();
281+
match serde_json::to_string_pretty(&json_response) {
282+
Ok(json) => println!("{json}"),
283+
Err(e) => {
284+
eprintln!("Error serializing response ({json_response:?}) to JSON: {e}");
285+
std::process::exit(1);
286+
},
287+
}
263288
},
264289
Err(e) => {
265290
handle_error(e);

0 commit comments

Comments
 (0)