Make config parameter re-useable

Extract config param into separate struct.
Use struct-opt flatten.
This commit is contained in:
Daniel Karzel 2021-01-28 14:55:29 +11:00
parent 75bbae2afe
commit 967736766b
2 changed files with 26 additions and 16 deletions

View File

@ -26,8 +26,8 @@ pub enum Command {
#[structopt(long = "receive-btc", help = "Bitcoin amount as floating point nr without denomination (e.g. 1.25)", parse(try_from_str = parse_btc))]
receive_bitcoin: bitcoin::Amount,
#[structopt(long = "config", parse(from_os_str))]
config_path: Option<PathBuf>,
#[structopt(flatten)]
config: Config,
},
BuyXmr {
#[structopt(long = "connect-peer-id")]
@ -42,8 +42,8 @@ pub enum Command {
#[structopt(long = "receive-xmr", help = "Monero amount as floating point nr without denomination (e.g. 125.1)", parse(try_from_str = parse_xmr))]
receive_monero: monero::Amount,
#[structopt(long = "config", parse(from_os_str))]
config_path: Option<PathBuf>,
#[structopt(flatten)]
config: Config,
},
History,
Resume(Resume),
@ -58,8 +58,8 @@ pub enum Resume {
#[structopt(long = "listen-address", default_value = "/ip4/127.0.0.1/tcp/9876")]
listen_addr: Multiaddr,
#[structopt(long = "config", parse(from_os_str))]
config_path: Option<PathBuf>,
#[structopt(flatten)]
config: Config,
},
BuyXmr {
#[structopt(long = "swap-id")]
@ -71,11 +71,21 @@ pub enum Resume {
#[structopt(long = "counterpart-addr")]
alice_addr: Multiaddr,
#[structopt(long = "config", parse(from_os_str))]
config_path: Option<PathBuf>,
#[structopt(flatten)]
config: Config,
},
}
#[derive(structopt::StructOpt, Debug)]
pub struct Config {
#[structopt(
long = "config",
help = "Provide a custom path to a configuration file. The configuration file must be a toml file.",
parse(from_os_str)
)]
pub config_path: Option<PathBuf>,
}
fn parse_btc(str: &str) -> anyhow::Result<bitcoin::Amount> {
let amount = bitcoin::Amount::from_str_in(str, ::bitcoin::Denomination::Bitcoin)?;
Ok(amount)

View File

@ -69,9 +69,9 @@ async fn main() -> Result<()> {
listen_addr,
send_monero,
receive_bitcoin,
config_path,
config,
} => {
let settings = init_settings(config_path)?;
let settings = init_settings(config.config_path)?;
let swap_amounts = SwapAmounts {
xmr: send_monero,
@ -108,9 +108,9 @@ async fn main() -> Result<()> {
alice_addr,
send_bitcoin,
receive_monero,
config_path,
config,
} => {
let settings = init_settings(config_path)?;
let settings = init_settings(config.config_path)?;
let swap_amounts = SwapAmounts {
btc: send_bitcoin,
@ -158,9 +158,9 @@ async fn main() -> Result<()> {
Command::Resume(Resume::SellXmr {
swap_id,
listen_addr,
config_path,
config,
}) => {
let settings = init_settings(config_path)?;
let settings = init_settings(config.config_path)?;
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
@ -183,9 +183,9 @@ async fn main() -> Result<()> {
swap_id,
alice_peer_id,
alice_addr,
config_path,
config,
}) => {
let settings = init_settings(config_path)?;
let settings = init_settings(config.config_path)?;
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;