Skip to content

Commit bbe0db5

Browse files
committed
use bail! macro. Fix lncli RPC call
1 parent 57cf226 commit bbe0db5

File tree

7 files changed

+46
-37
lines changed

7 files changed

+46
-37
lines changed

src/rust-cli/debug.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ pub async fn handle_debug_command(
2323
DebugCommand::GenerateCompose { graph_file_path } => {
2424
rpc_params
2525
.insert("graph_file", graph_file_path.to_str())
26-
.context("Add graph file path to params")?;
27-
let data = make_rpc_call("generate_compose", rpc_params).await?;
26+
.context("Adding graph_file_path to rpc params")?;
27+
let data = make_rpc_call("generate_compose", rpc_params)
28+
.await
29+
.context("Calling generate_compose RPC")?;
2830
println!("Docker-compose file generated: {:?}", data);
2931
}
3032
}

src/rust-cli/general.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ pub async fn handle_rpc_commands(
2626
params.insert("params", p).context("add rpc params")?;
2727
}
2828
let data = match node_type {
29-
NodeType::LnCli => make_rpc_call("tank_bcli", params)
29+
NodeType::LnCli => make_rpc_call("tank_lncli", params)
3030
.await
31-
.context("Failed to make RPC call LnCli")?,
31+
.context("make RPC call lncli")?,
3232
NodeType::BitcoinCli => make_rpc_call("tank_bcli", params)
3333
.await
34-
.context("Failed to make RPC call BitcoinCli")?,
34+
.context("make RPC call bitcoin-cli")?,
3535
};
36-
pretty_print_value(&data).context("pretty print result")?;
36+
pretty_print_value(&data).context("Pretty print the result")?;
3737
Ok(())
3838
}
3939

@@ -43,7 +43,7 @@ pub async fn handle_debug_log_command(node: &u64, mut params: ObjectParams) -> a
4343
.context("add node_index param")?;
4444
let data = make_rpc_call("tank_debug_log", params)
4545
.await
46-
.context("Failed to make RPC call tank_debug_log")?;
46+
.context("make RPC call tank_debug_log")?;
4747
pretty_print_value(&data).context("pretty print result")?;
4848
Ok(())
4949
}

src/rust-cli/graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub enum GraphCommand {
2020
Create {
2121
/// Number of nodes in the graph
2222
number: usize,
23-
/// Write graph to a this file path
23+
/// Write graph to this file path
2424
#[arg(short, long)]
2525
outfile: Option<PathBuf>,
2626
/// Bitcoin Core version to set on nodes
@@ -263,7 +263,7 @@ pub async fn handle_graph_command(command: &GraphCommand) -> anyhow::Result<()>
263263
.context("Create a graph")?,
264264

265265
GraphCommand::Validate { graph } => {
266-
let _ = validate_schema(graph);
266+
let _ = validate_schema(graph).context("Validating graph schema");
267267
}
268268
}
269269
Ok(())

src/rust-cli/image.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::anyhow;
1+
use anyhow::bail;
22
use clap::Subcommand;
33
use std::fs;
44
use std::process::{Command, Stdio};
@@ -48,7 +48,7 @@ fn run_command(command: &str) -> anyhow::Result<bool> {
4848
if output.success() {
4949
Ok(true)
5050
} else {
51-
Err(anyhow!("Command failed"))
51+
bail!("Command failed")
5252
}
5353
}
5454

@@ -75,7 +75,7 @@ fn build_image(
7575
for arch in &build_arches {
7676
if !ARCHES.contains(&arch.as_str()) {
7777
println!("Error: {} is not a supported architecture", arch);
78-
return Err(anyhow!("Unsupported architecture: {}", arch));
78+
bail!("Unsupported architecture: {}", arch);
7979
}
8080
}
8181

@@ -92,7 +92,7 @@ fn build_image(
9292
{
9393
println!("Directory src/templates does not exist.");
9494
println!("Please run this script from the project root.");
95-
return Err(anyhow!("src/templates directory not found"));
95+
bail!("src/templates directory not found");
9696
}
9797

9898
let builder_name = "bitcoind-builder";
@@ -140,10 +140,8 @@ fn build_image(
140140

141141
match res {
142142
Ok(true) => Ok(()),
143-
Ok(false) => Err(anyhow!(
144-
"Build command failed, but no specific error was provided."
145-
)),
146-
Err(e) => Err(anyhow!("Build command failed with error: {}", e)),
143+
Ok(false) => bail!("Build command failed, but no specific error was provided."),
144+
Err(e) => bail!("Build command failed with error: {}", e),
147145
}
148146
}
149147

src/rust-cli/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ async fn main() -> anyhow::Result<()> {
101101
if let Some(network_value) = &cli.network {
102102
rpc_params
103103
.insert("network", network_value)
104-
.context("add network param")?;
104+
.context("Adding --network to rpc_params")?;
105105
}
106106

107107
match &cli.command {

src/rust-cli/network.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::Context;
1+
use anyhow::{bail, Context};
22
use base64::{engine::general_purpose, Engine as _};
33
use clap::Subcommand;
44
use jsonrpsee::core::params::ObjectParams;
@@ -33,11 +33,11 @@ pub enum NetworkCommand {
3333
}
3434

3535
fn graph_file_to_b64(graph_file: &PathBuf) -> anyhow::Result<String> {
36-
let file_contents = std::fs::read(graph_file).context("Failed to read graph file")?;
36+
let file_contents = std::fs::read(graph_file).context("Failed to read graph file from fs")?;
3737
Ok(general_purpose::STANDARD.encode(file_contents))
3838
}
3939

40-
fn handle_network_status_response(data: serde_json::Value) {
40+
fn handle_network_status_response(data: serde_json::Value) -> anyhow::Result<()> {
4141
if let serde_json::Value::Array(items) = &data {
4242
for item in items {
4343
if let (Some(tank_index), Some(bitcoin_status)) = (
@@ -46,12 +46,13 @@ fn handle_network_status_response(data: serde_json::Value) {
4646
) {
4747
println!("Tank: {:<6} Bitcoin: {}", tank_index, bitcoin_status);
4848
} else {
49-
println!("Error: Response item is missing expected fields");
49+
bail!("Error: Response item is missing expected fields");
5050
}
5151
}
5252
} else {
53-
println!("Error: Expected an array in the response");
53+
bail!("Error: Expected an array in the response");
5454
}
55+
Ok(())
5556
}
5657

5758
fn handle_network_start_response(data: serde_json::Value) -> anyhow::Result<()> {
@@ -81,6 +82,8 @@ fn handle_network_start_response(data: serde_json::Value) -> anyhow::Result<()>
8182
));
8283
}
8384
table.printstd();
85+
} else {
86+
bail!("No warnet table headers found in response")
8487
}
8588
// tanks table
8689
if let Some(tank_headers) = data["tank_headers"].as_array() {
@@ -109,6 +112,8 @@ fn handle_network_start_response(data: serde_json::Value) -> anyhow::Result<()>
109112
}
110113
}
111114
table.printstd();
115+
} else {
116+
bail!("no tank headers found in response")
112117
}
113118
Ok(())
114119
}
@@ -119,7 +124,8 @@ pub async fn handle_network_command(
119124
) -> anyhow::Result<()> {
120125
let (request, params) = match command {
121126
NetworkCommand::Start { graph_file, force } => {
122-
let b64_graph = graph_file_to_b64(graph_file).context("Read graph file")?;
127+
let b64_graph =
128+
graph_file_to_b64(graph_file).context("Reading graph file to base 64")?;
123129
params
124130
.insert("graph_file", b64_graph)
125131
.context("Add base64 graph file to params")?;
@@ -138,9 +144,12 @@ pub async fn handle_network_command(
138144

139145
let data = make_rpc_call(request, params).await?;
140146
match request {
141-
"network_status" => handle_network_status_response(data),
147+
"network_status" => {
148+
handle_network_status_response(data).context("Handling network status response")?
149+
}
142150
"network_from_file" => {
143-
handle_network_start_response(data.clone())?;
151+
handle_network_start_response(data.clone())
152+
.context("Handling network start response")?;
144153
}
145154
_ => {
146155
println!("{}", data)

src/rust-cli/scenarios.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::rpc_call::make_rpc_call;
2-
use anyhow::Context;
2+
use anyhow::{bail, Context};
33
use base64::{engine::general_purpose, Engine as _};
44
use clap::Subcommand;
55
use jsonrpsee::core::params::ObjectParams;
@@ -35,7 +35,7 @@ pub enum ScenarioCommand {
3535
async fn handle_available(params: ObjectParams) -> anyhow::Result<()> {
3636
let data = make_rpc_call("scenarios_available", params)
3737
.await
38-
.context("Failed to fetch available scenarios")?;
38+
.context("Making RPC to fetch available scenarios")?;
3939
if let serde_json::Value::Array(scenarios) = data {
4040
let mut table = Table::new();
4141
table.add_row(row!["Scenario", "Description"]);
@@ -50,7 +50,7 @@ async fn handle_available(params: ObjectParams) -> anyhow::Result<()> {
5050
}
5151
table.printstd();
5252
} else {
53-
println!("Unexpected response format.");
53+
bail!("Unexpected response format.");
5454
}
5555
Ok(())
5656
}
@@ -68,7 +68,7 @@ async fn handle_run(
6868
.context("Add additional_args to params")?;
6969
let data = make_rpc_call("scenarios_run", params)
7070
.await
71-
.context("Failed to run scenario")?;
71+
.context("Making RPC call to run scenario with remote file")?;
7272
println!("{:?}", data);
7373
Ok(())
7474
}
@@ -82,21 +82,21 @@ async fn handle_run_file(
8282
let scenario_base64 = general_purpose::STANDARD.encode(file_contents);
8383
params
8484
.insert("scenario_base64", scenario_base64)
85-
.context("Add scenario to params")?;
85+
.context("Adding scenario to params")?;
8686
params
8787
.insert("additional_args", additional_args)
88-
.context("Add additional_args to params")?;
88+
.context("Adding additional_args to params")?;
8989
let data = make_rpc_call("scenarios_run_file", params)
9090
.await
91-
.context("Failed to run scenario")?;
91+
.context("Making RPC call to run scenario with local file")?;
9292
println!("{:?}", data);
9393
Ok(())
9494
}
9595

9696
async fn handle_active(params: ObjectParams) -> anyhow::Result<()> {
9797
let data = make_rpc_call("scenarios_list_running", params)
9898
.await
99-
.context("Failed to list running scenarios")?;
99+
.context("Making RPC call to list running scenarios")?;
100100
if let serde_json::Value::Array(scenarios) = data {
101101
let mut table = Table::new();
102102
table.add_row(row!["PID", "Command", "Network", "Active"]);
@@ -123,7 +123,7 @@ async fn handle_active(params: ObjectParams) -> anyhow::Result<()> {
123123
}
124124
table.printstd();
125125
} else {
126-
println!("Unexpected response format.");
126+
bail!("Unexpected response format.");
127127
}
128128
Ok(())
129129
}
@@ -132,11 +132,11 @@ async fn handle_stop(mut params: ObjectParams, pid: &u64) -> anyhow::Result<()>
132132
params.insert("pid", pid).context("Add pid to params")?;
133133
let data = make_rpc_call("scenarios_stop", params)
134134
.await
135-
.context("Failed to stop running scenario")?;
135+
.context("Making RPC call to stop running scenario")?;
136136
if let serde_json::Value::String(message) = data {
137137
println!("{}", message);
138138
} else {
139-
println!("Unexpected response format.");
139+
bail!("Unexpected response format.");
140140
}
141141
Ok(())
142142
}

0 commit comments

Comments
 (0)