Skip to content

Commit c810b39

Browse files
committed
feat: Replace network with config file.
1 parent 7a6e253 commit c810b39

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/bin/cli.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
use bitcoin::Network;
21
use clap::{Parser, Subcommand};
3-
use clementine_cli::{config::CliConfig, deposit, withdrawal};
2+
use clementine_cli::{config::CliConfig, debug, deposit, withdrawal};
3+
use std::path::PathBuf;
44

55
#[derive(Parser)]
66
#[command(name = "clementine")]
77
#[command(about = "Clementine CLI - wallet-agnostic Citrea bridge CLI", long_about = None)]
88
struct Cli {
9-
/// Bitcoin network to use (bitcoin, testnet, testnet4)
10-
#[arg(long, default_value = "bitcoin")]
11-
network: String,
9+
/// Path to config file. If not given, current directory will be searched for the cli_config.toml file
10+
#[arg(long)]
11+
config_file: Option<PathBuf>,
1212

1313
#[command(subcommand)]
1414
command: Commands,
1515
}
1616

1717
#[derive(Subcommand)]
1818
enum Commands {
19+
/// Deposit related operations.
1920
Deposit {
2021
#[command(subcommand)]
2122
command: DepositCommands,
2223
},
24+
/// Withdrawal related operations.
2325
Withdrawal {
2426
#[command(subcommand)]
2527
command: WithdrawalCommands,
@@ -132,23 +134,20 @@ enum WithdrawalCommands {
132134
async fn main() {
133135
let cli = Cli::parse();
134136

135-
// Parse network string using bitcoin crate's parsing
136-
let network = match cli.network.parse::<Network>() {
137-
Ok(network) => network,
138-
Err(_) => {
139-
eprintln!(
140-
"Error: Invalid network '{}'. Use: bitcoin, testnet, signet, regtest or testnet4",
141-
cli.network
142-
);
143-
std::process::exit(1);
144-
}
137+
let config = if let Some(config_file_path) = cli.config_file {
138+
debug!("Config file {config_file_path:?} is going to be used...");
139+
CliConfig::try_parse_file(config_file_path).unwrap()
140+
} else {
141+
let mut current_dir = std::env::current_dir().unwrap();
142+
current_dir.push("cli_config.toml");
143+
debug!("No config file given, looking for the current directory: {current_dir:?}...");
144+
CliConfig::try_parse_file(current_dir).unwrap()
145145
};
146-
let config = CliConfig::from_network(network);
147146

148147
match cli.command {
149148
Commands::Deposit { command } => match command {
150149
DepositCommands::GenerateRecoveryKey { y, private_key } => {
151-
if let Err(e) = deposit::generate_recovery_key(y, private_key, network) {
150+
if let Err(e) = deposit::generate_recovery_key(y, private_key, config.network) {
152151
eprintln!("Error: {}", e);
153152
std::process::exit(1);
154153
}
@@ -229,7 +228,7 @@ async fn main() {
229228
},
230229
Commands::Withdrawal { command } => match command {
231230
WithdrawalCommands::GenerateSignerAddress { y } => {
232-
if let Err(e) = withdrawal::generate_signer_address(y, network) {
231+
if let Err(e) = withdrawal::generate_signer_address(y, config.network) {
233232
eprintln!("Error: {}", e);
234233
std::process::exit(1);
235234
}
@@ -245,7 +244,7 @@ async fn main() {
245244
&withdrawal_address,
246245
&withdrawal_utxo,
247246
amount,
248-
network,
247+
config.network,
249248
) {
250249
eprintln!("Error: {}", e);
251250
std::process::exit(1);

0 commit comments

Comments
 (0)