Skip to content

Commit

Permalink
Merge pull request fedimint#5882 from joschisan/lnv2_cli_opts
Browse files Browse the repository at this point in the history
cleanup: use subcommand
  • Loading branch information
elsirion authored Aug 19, 2024
2 parents 92ac1f1 + f3c52bf commit b2a8f0b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
55 changes: 32 additions & 23 deletions modules/fedimint-lnv2-client/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{ffi, iter};

use clap::Parser;
use clap::{Parser, Subcommand};
use fedimint_core::core::OperationId;
use fedimint_core::util::SafeUrl;
use fedimint_core::{Amount, PeerId};
Expand All @@ -24,12 +24,19 @@ enum Opts {
Receive { gateway: SafeUrl, amount: Amount },
/// Await the final state of the receive operation
AwaitReceive { operation_id: OperationId },
/// Fetch vetted gateways
Gateways { peer: Option<PeerId> },
/// Gateway subcommands
#[command(subcommand)]
Gateway(GatewayOpts),
}

#[derive(Clone, Subcommand, Serialize)]
enum GatewayOpts {
/// List vetted gateways
List { peer: Option<PeerId> },
/// Add a vetted gateway
AddGateway { gateway: SafeUrl },
Add { gateway: SafeUrl },
/// Remove a vetted gateway
RemoveGateway { gateway: SafeUrl },
Remove { gateway: SafeUrl },
}

pub(crate) async fn handle_cli_command(
Expand All @@ -43,26 +50,28 @@ pub(crate) async fn handle_cli_command(
Opts::AwaitSend { operation_id } => json(lightning.await_send(operation_id).await?),
Opts::Receive { gateway, amount } => json(lightning.receive(gateway, amount).await?),
Opts::AwaitReceive { operation_id } => json(lightning.await_receive(operation_id).await?),
Opts::Gateways { peer } => match peer {
Some(peer) => json(lightning.module_api.fetch_gateways_from_peer(peer).await?),
None => json(lightning.module_api.fetch_gateways().await?),
},
Opts::AddGateway { gateway } => {
let auth = lightning
.admin_auth
.clone()
.ok_or(anyhow::anyhow!("Admin auth not set"))?;
Opts::Gateway(gateway_opts) => match gateway_opts {
GatewayOpts::List { peer } => match peer {
Some(peer) => json(lightning.module_api.fetch_gateways_from_peer(peer).await?),
None => json(lightning.module_api.fetch_gateways().await?),
},
GatewayOpts::Add { gateway } => {
let auth = lightning
.admin_auth
.clone()
.ok_or(anyhow::anyhow!("Admin auth not set"))?;

json(lightning.module_api.add_gateway(auth, gateway).await?)
}
Opts::RemoveGateway { gateway } => {
let auth = lightning
.admin_auth
.clone()
.ok_or(anyhow::anyhow!("Admin auth not set"))?;
json(lightning.module_api.add_gateway(auth, gateway).await?)
}
GatewayOpts::Remove { gateway } => {
let auth = lightning
.admin_auth
.clone()
.ok_or(anyhow::anyhow!("Admin auth not set"))?;

json(lightning.module_api.remove_gateway(auth, gateway).await?)
}
json(lightning.module_api.remove_gateway(auth, gateway).await?)
}
},
};

Ok(value)
Expand Down
10 changes: 6 additions & 4 deletions modules/fedimint-lnv2-tests/src/bin/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ async fn test_gateway_registration(dev_fed: &DevJitFed) -> anyhow::Result<()> {
"pass",
"module",
"lnv2",
"add-gateway",
"gateway",
"add",
gateway.clone().to_string(),
)
.out_json()
Expand All @@ -133,7 +134,7 @@ async fn test_gateway_registration(dev_fed: &DevJitFed) -> anyhow::Result<()> {
);

assert_eq!(
cmd!(client, "module", "lnv2", "gateways", "0")
cmd!(client, "module", "lnv2", "gateway", "list", "0")
.out_json()
.await?,
serde_json::to_value(vec![gateway.clone()]).expect("JSON serialization failed")
Expand All @@ -148,7 +149,8 @@ async fn test_gateway_registration(dev_fed: &DevJitFed) -> anyhow::Result<()> {
"pass",
"module",
"lnv2",
"remove-gateway",
"gateway",
"remove",
gateway.to_string(),
)
.out_json()
Expand All @@ -157,7 +159,7 @@ async fn test_gateway_registration(dev_fed: &DevJitFed) -> anyhow::Result<()> {
);

assert_eq!(
cmd!(client, "module", "lnv2", "gateways", "0",)
cmd!(client, "module", "lnv2", "gateway", "list", "0",)
.out_json()
.await?,
serde_json::to_value(Vec::<SafeUrl>::new()).expect("JSON serialization failed")
Expand Down

0 comments on commit b2a8f0b

Please sign in to comment.