Make config argument global

The `config` argument apply to all commands. It is now optional and
needs to be passed before a command.
E.g. `cli --config ./config.toml history`
This commit is contained in:
Franck Royer 2021-02-11 15:21:34 +11:00
parent 83dcf4ba3c
commit 8fada42074
No known key found for this signature in database
GPG key ID: A82ED75A8DFC50A4
5 changed files with 73 additions and 84 deletions

View file

@ -6,11 +6,11 @@ use uuid::Uuid;
#[derive(structopt::StructOpt, Debug)]
pub struct Arguments {
#[structopt(
long = "data-dir",
help = "Provide a custom path to the data directory.",
long = "config",
help = "Provide a custom path to the configuration file. The configuration file must be a toml file.",
parse(from_os_str)
)]
pub data_dir: Option<PathBuf>,
pub config: Option<PathBuf>,
#[structopt(subcommand)]
pub cmd: Command,
@ -31,9 +31,6 @@ 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(flatten)]
config: Config,
},
History,
Resume(Resume),
@ -52,9 +49,6 @@ pub enum Resume {
#[structopt(long = "counterpart-addr")]
alice_addr: Multiaddr,
#[structopt(flatten)]
config: Config,
},
}
@ -71,9 +65,6 @@ pub enum Cancel {
#[structopt(long = "counterpart-addr")]
alice_addr: Multiaddr,
#[structopt(flatten)]
config: Config,
#[structopt(short, long)]
force: bool,
},
@ -92,24 +83,11 @@ pub enum Refund {
#[structopt(long = "counterpart-addr")]
alice_addr: Multiaddr,
#[structopt(flatten)]
config: Config,
#[structopt(short, long)]
force: bool,
},
}
#[derive(structopt::StructOpt, Debug)]
pub struct Config {
#[structopt(
long = "config",
help = "Provide a custom path to the configuration file. The configuration file must be a toml file.",
parse(from_os_str)
)]
pub 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

@ -1,4 +1,4 @@
use crate::fs::ensure_directory_exists;
use crate::fs::{default_data_dir, ensure_directory_exists};
use anyhow::{Context, Result};
use config::ConfigError;
use dialoguer::{theme::ColorfulTheme, Input};
@ -16,6 +16,7 @@ const DEFAULT_MONERO_WALLET_RPC_TESTNET_URL: &str = "http://127.0.0.1:38083/json
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct Config {
pub data: Data,
pub bitcoin: Bitcoin,
pub monero: Monero,
}
@ -33,6 +34,12 @@ impl Config {
}
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Data {
pub dir: PathBuf,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Bitcoin {
@ -86,6 +93,18 @@ where
pub fn query_user_for_initial_testnet_config() -> Result<Config> {
println!();
let data_dir = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Enter data directory for the swap CLI or hit return to use default")
.default(
default_data_dir()
.context("No default data dir value for this system")?
.to_str()
.context("Unsupported characters in default path")?
.to_string(),
)
.interact_text()?;
let data_dir = data_dir.as_str().parse()?;
let bitcoind_url = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Enter Bitcoind URL (including username and password if applicable) or hit return to use default")
.default(DEFAULT_BITCOIND_TESTNET_URL.to_owned())
@ -104,6 +123,7 @@ pub fn query_user_for_initial_testnet_config() -> Result<Config> {
println!();
Ok(Config {
data: Data { dir: data_dir },
bitcoin: Bitcoin {
bitcoind_url,
wallet_name: bitcoin_wallet_name,
@ -126,6 +146,9 @@ mod tests {
let config_path = Path::join(&temp_dir, "config.toml");
let expected = Config {
data: Data {
dir: Default::default(),
},
bitcoin: Bitcoin {
bitcoind_url: Url::from_str("http://127.0.0.1:18332").unwrap(),
wallet_name: "alice".to_string(),