Add --config param to make configuration file configurable

This commit is contained in:
Daniel Karzel 2021-01-28 14:34:18 +11:00
parent 345c57915e
commit 75bbae2afe
2 changed files with 29 additions and 7 deletions

View file

@ -1,5 +1,6 @@
use crate::{bitcoin, monero};
use libp2p::{core::Multiaddr, PeerId};
use std::path::PathBuf;
use uuid::Uuid;
#[derive(structopt::StructOpt, Debug)]
@ -24,6 +25,9 @@ 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>,
},
BuyXmr {
#[structopt(long = "connect-peer-id")]
@ -37,6 +41,9 @@ 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>,
},
History,
Resume(Resume),
@ -50,6 +57,9 @@ 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>,
},
BuyXmr {
#[structopt(long = "swap-id")]
@ -60,6 +70,9 @@ pub enum Resume {
#[structopt(long = "counterpart-addr")]
alice_addr: Multiaddr,
#[structopt(long = "config", parse(from_os_str))]
config_path: Option<PathBuf>,
},
}

View file

@ -24,7 +24,7 @@ use fs::default_config_path;
use prettytable::{row, Table};
use protocol::{alice, bob, bob::Builder, SwapAmounts};
use settings::Settings;
use std::sync::Arc;
use std::{path::PathBuf, sync::Arc};
use structopt::StructOpt;
use trace::init_tracing;
use tracing::{info, log::LevelFilter};
@ -69,8 +69,9 @@ async fn main() -> Result<()> {
listen_addr,
send_monero,
receive_bitcoin,
config_path,
} => {
let settings = init_settings()?;
let settings = init_settings(config_path)?;
let swap_amounts = SwapAmounts {
xmr: send_monero,
@ -107,8 +108,9 @@ async fn main() -> Result<()> {
alice_addr,
send_bitcoin,
receive_monero,
config_path,
} => {
let settings = init_settings()?;
let settings = init_settings(config_path)?;
let swap_amounts = SwapAmounts {
btc: send_bitcoin,
@ -156,8 +158,9 @@ async fn main() -> Result<()> {
Command::Resume(Resume::SellXmr {
swap_id,
listen_addr,
config_path,
}) => {
let settings = init_settings()?;
let settings = init_settings(config_path)?;
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
@ -180,8 +183,9 @@ async fn main() -> Result<()> {
swap_id,
alice_peer_id,
alice_addr,
config_path,
}) => {
let settings = init_settings()?;
let settings = init_settings(config_path)?;
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
@ -205,8 +209,13 @@ async fn main() -> Result<()> {
Ok(())
}
fn init_settings() -> Result<Settings> {
let config_path = default_config_path()?;
fn init_settings(config_path: Option<PathBuf>) -> Result<Settings> {
let config_path = if let Some(config_path) = config_path {
config_path
} else {
default_config_path()?
};
let config = match read_config(config_path.clone())? {
Ok(config) => config,
Err(ConfigNotInitialized {}) => {