Use default testnet config if config file path not specified

This commit is contained in:
rishflab 2021-02-25 10:53:50 +11:00
parent fffa679f42
commit f52567155a
2 changed files with 33 additions and 77 deletions

View File

@ -21,15 +21,11 @@ use swap::{
bitcoin, bitcoin,
cli::{ cli::{
command::{Arguments, Cancel, Command, Refund, Resume}, command::{Arguments, Cancel, Command, Refund, Resume},
config::{ config::{read_config, Config},
initial_setup, query_user_for_initial_testnet_config, read_config, Config,
ConfigNotInitialized,
},
}, },
database::Database, database::Database,
execution_params, execution_params,
execution_params::GetExecutionParams, execution_params::GetExecutionParams,
fs::default_config_path,
monero, monero,
monero::{CreateWallet, OpenWallet}, monero::{CreateWallet, OpenWallet},
protocol::{ protocol::{
@ -54,18 +50,9 @@ async fn main() -> Result<()> {
let opt = Arguments::from_args(); let opt = Arguments::from_args();
let config_path = if let Some(config_path) = opt.config { let config = match opt.config {
config_path Some(config_path) => read_config(config_path)??,
} else { None => Config::testnet(),
default_config_path()?
};
let config = match read_config(config_path.clone())? {
Ok(config) => config,
Err(ConfigNotInitialized {}) => {
initial_setup(config_path.clone(), query_user_for_initial_testnet_config)?;
read_config(config_path)?.expect("after initial setup config can be read")
}
}; };
info!( info!(

View File

@ -1,11 +1,9 @@
use crate::fs::{default_data_dir, ensure_directory_exists}; use crate::fs::default_data_dir;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use config::ConfigError; use config::ConfigError;
use dialoguer::{theme::ColorfulTheme, Input};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
ffi::OsStr, ffi::OsStr,
fs,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use tracing::info; use tracing::info;
@ -31,6 +29,22 @@ impl Config {
config.merge(config::File::from(config_file))?; config.merge(config::File::from(config_file))?;
config.try_into() config.try_into()
} }
pub fn testnet() -> Self {
Self {
data: Data {
dir: default_data_dir().expect("computed valid path for data dir"),
},
bitcoin: Bitcoin {
electrum_http_url: DEFAULT_ELECTRUM_HTTP_URL
.parse()
.expect("default electrum http str is a valid url"),
electrum_rpc_url: DEFAULT_ELECTRUM_RPC_URL
.parse()
.expect("default electrum rpc str is a valid url"),
},
}
}
} }
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] #[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
@ -66,67 +80,22 @@ pub fn read_config(config_path: PathBuf) -> Result<Result<Config, ConfigNotIniti
Ok(Ok(file)) Ok(Ok(file))
} }
pub fn initial_setup<F>(config_path: PathBuf, config_file: F) -> Result<()>
where
F: Fn() -> Result<Config>,
{
info!("Config file not found, running initial setup...");
ensure_directory_exists(config_path.as_path())?;
let initial_config = config_file()?;
let toml = toml::to_string(&initial_config)?;
fs::write(&config_path, toml)?;
info!(
"Initial setup complete, config file created at {} ",
config_path.as_path().display()
);
Ok(())
}
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 electrum_http_url: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Enter Electrum HTTP URL or hit return to use default")
.default(DEFAULT_ELECTRUM_HTTP_URL.to_owned())
.interact_text()?;
let electrum_http_url = Url::parse(electrum_http_url.as_str())?;
let electrum_rpc_url: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Enter Electrum RPC URL or hit return to use default")
.default(DEFAULT_ELECTRUM_RPC_URL.to_owned())
.interact_text()?;
let electrum_rpc_url = Url::parse(electrum_rpc_url.as_str())?;
println!();
Ok(Config {
data: Data { dir: data_dir },
bitcoin: Bitcoin {
electrum_http_url,
electrum_rpc_url,
},
})
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use std::str::FromStr; use crate::fs::ensure_directory_exists;
use std::{fs, str::FromStr};
use tempfile::tempdir; use tempfile::tempdir;
pub fn initial_setup(config_path: PathBuf, config: Config) -> Result<()> {
ensure_directory_exists(config_path.as_path())?;
let toml = toml::to_string(&config)?;
fs::write(&config_path, toml)?;
Ok(())
}
#[test] #[test]
fn config_roundtrip() { fn config_roundtrip() {
let temp_dir = tempdir().unwrap().path().to_path_buf(); let temp_dir = tempdir().unwrap().path().to_path_buf();
@ -142,7 +111,7 @@ mod tests {
}, },
}; };
initial_setup(config_path.clone(), || Ok(expected.clone())).unwrap(); initial_setup(config_path.clone(), expected.clone()).unwrap();
let actual = read_config(config_path).unwrap().unwrap(); let actual = read_config(config_path).unwrap().unwrap();
assert_eq!(expected, actual); assert_eq!(expected, actual);