Skip to content

Commit

Permalink
Merge pull request #38 from BP-WG/fix/arg-resolver
Browse files Browse the repository at this point in the history
Improve specification for resolvers in cli arguments
  • Loading branch information
dr-orlovsky authored Jun 8, 2024
2 parents b86deb4 + 8880352 commit 32a2840
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
15 changes: 13 additions & 2 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use std::fmt::Debug;
use std::path::PathBuf;
use std::process::exit;

use clap::Subcommand;
use descriptors::Descriptor;
Expand All @@ -47,6 +48,9 @@ pub struct Args<C: Clone + Eq + Debug + Subcommand, O: DescriptorOpts = DescrStd
#[command(flatten)]
pub resolver: ResolverOpt,

#[clap(long, global = true)]
pub sync: bool,

#[command(flatten)]
pub general: GeneralOpts,

Expand All @@ -61,6 +65,7 @@ impl<C: Clone + Eq + Debug + Subcommand, O: DescriptorOpts> Args<C, O> {
verbose: self.verbose.clone(),
wallet: self.wallet.clone(),
resolver: self.resolver.clone(),
sync: self.sync,
general: self.general.clone(),
command: cmd.clone(),
}
Expand Down Expand Up @@ -103,7 +108,7 @@ impl<C: Clone + Eq + Debug + Subcommand, O: DescriptorOpts> Args<C, O> {
eprint!(" from wallet {wallet_name} ... ");
Runtime::load_standard(self.general.wallet_dir(wallet_name))?
};
let mut sync = self.resolver.sync;
let mut sync = self.sync;
if runtime.warnings().is_empty() {
eprintln!("success");
} else {
Expand All @@ -122,7 +127,13 @@ impl<C: Clone + Eq + Debug + Subcommand, O: DescriptorOpts> Args<C, O> {
(Some(url), None) => {
AnyIndexer::Esplora(Box::new(esplora::Builder::new(url).build_blocking()?))
}
_ => unreachable!("clap is broken"),
_ => {
eprintln!(
" - error: no blockchain indexer specified; use either --esplora or \
--electrum argument"
);
exit(1);
}
};
if let Err(errors) = runtime.sync(&indexer) {
eprintln!(" partial, some requests has failed:");
Expand Down
6 changes: 3 additions & 3 deletions src/cli/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl<O: DescriptorOpts> Exec for Args<BpCommand, O> {
addr: false,
utxo: false,
};
self.resolver.sync = false;
self.sync = false;
self.exec(config, name)?;
}
BpCommand::Balance {
Expand All @@ -281,7 +281,7 @@ impl<O: DescriptorOpts> Exec for Args<BpCommand, O> {
addr: false,
utxo: false,
};
self.resolver.sync = false;
self.sync = false;
self.exec(config, name)?;
}
BpCommand::Balance {
Expand All @@ -301,7 +301,7 @@ impl<O: DescriptorOpts> Exec for Args<BpCommand, O> {
addr: false,
utxo: false,
};
self.resolver.sync = false;
self.sync = false;
self.exec(config, name)?;
}
BpCommand::History { txid, details } => {
Expand Down
5 changes: 1 addition & 4 deletions src/cli/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub const DEFAULT_ELECTRUM: &str = "example.com:50001";
pub const DEFAULT_ESPLORA: &str = "https://blockstream.info/testnet/api";

#[derive(Args, Clone, PartialEq, Eq, Debug)]
#[group(args = ["electrum", "esplora"], required = true)]
#[group(args = ["electrum", "esplora"])]
pub struct ResolverOpt {
/// Electrum server to use.
#[arg(
Expand Down Expand Up @@ -75,9 +75,6 @@ pub struct ResolverOpt {
value_name = "URL"
)]
pub esplora: Option<String>,

#[clap(long, global = true)]
pub sync: bool,
}

pub trait DescriptorOpts: clap::Args + Clone + Eq + Debug {
Expand Down

0 comments on commit 32a2840

Please sign in to comment.