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))] #[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, receive_bitcoin: bitcoin::Amount,
#[structopt(long = "config", parse(from_os_str))] #[structopt(flatten)]
config_path: Option<PathBuf>, config: Config,
}, },
BuyXmr { BuyXmr {
#[structopt(long = "connect-peer-id")] #[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))] #[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, receive_monero: monero::Amount,
#[structopt(long = "config", parse(from_os_str))] #[structopt(flatten)]
config_path: Option<PathBuf>, config: Config,
}, },
History, History,
Resume(Resume), Resume(Resume),
@ -58,8 +58,8 @@ pub enum Resume {
#[structopt(long = "listen-address", default_value = "/ip4/127.0.0.1/tcp/9876")] #[structopt(long = "listen-address", default_value = "/ip4/127.0.0.1/tcp/9876")]
listen_addr: Multiaddr, listen_addr: Multiaddr,
#[structopt(long = "config", parse(from_os_str))] #[structopt(flatten)]
config_path: Option<PathBuf>, config: Config,
}, },
BuyXmr { BuyXmr {
#[structopt(long = "swap-id")] #[structopt(long = "swap-id")]
@ -71,11 +71,21 @@ pub enum Resume {
#[structopt(long = "counterpart-addr")] #[structopt(long = "counterpart-addr")]
alice_addr: Multiaddr, alice_addr: Multiaddr,
#[structopt(long = "config", parse(from_os_str))] #[structopt(flatten)]
config_path: Option<PathBuf>, 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> { fn parse_btc(str: &str) -> anyhow::Result<bitcoin::Amount> {
let amount = bitcoin::Amount::from_str_in(str, ::bitcoin::Denomination::Bitcoin)?; let amount = bitcoin::Amount::from_str_in(str, ::bitcoin::Denomination::Bitcoin)?;
Ok(amount) Ok(amount)

View file

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