From f52567155a9a3a2ed2eba099c6f5926189f5219c Mon Sep 17 00:00:00 2001 From: rishflab Date: Thu, 25 Feb 2021 10:53:50 +1100 Subject: [PATCH] Use default testnet config if config file path not specified --- swap/src/bin/swap_cli.rs | 21 ++-------- swap/src/cli/config.rs | 89 +++++++++++++--------------------------- 2 files changed, 33 insertions(+), 77 deletions(-) diff --git a/swap/src/bin/swap_cli.rs b/swap/src/bin/swap_cli.rs index c4fed8c1..13a2725c 100644 --- a/swap/src/bin/swap_cli.rs +++ b/swap/src/bin/swap_cli.rs @@ -21,15 +21,11 @@ use swap::{ bitcoin, cli::{ command::{Arguments, Cancel, Command, Refund, Resume}, - config::{ - initial_setup, query_user_for_initial_testnet_config, read_config, Config, - ConfigNotInitialized, - }, + config::{read_config, Config}, }, database::Database, execution_params, execution_params::GetExecutionParams, - fs::default_config_path, monero, monero::{CreateWallet, OpenWallet}, protocol::{ @@ -54,18 +50,9 @@ async fn main() -> Result<()> { let opt = Arguments::from_args(); - let config_path = if let Some(config_path) = opt.config { - config_path - } else { - 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") - } + let config = match opt.config { + Some(config_path) => read_config(config_path)??, + None => Config::testnet(), }; info!( diff --git a/swap/src/cli/config.rs b/swap/src/cli/config.rs index 156fe269..2efa1b35 100644 --- a/swap/src/cli/config.rs +++ b/swap/src/cli/config.rs @@ -1,11 +1,9 @@ -use crate::fs::{default_data_dir, ensure_directory_exists}; +use crate::fs::default_data_dir; use anyhow::{Context, Result}; use config::ConfigError; -use dialoguer::{theme::ColorfulTheme, Input}; use serde::{Deserialize, Serialize}; use std::{ ffi::OsStr, - fs, path::{Path, PathBuf}, }; use tracing::info; @@ -31,6 +29,22 @@ impl Config { config.merge(config::File::from(config_file))?; 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)] @@ -66,67 +80,22 @@ pub fn read_config(config_path: PathBuf) -> Result(config_path: PathBuf, config_file: F) -> Result<()> -where - F: Fn() -> Result, -{ - 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 { - 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)] mod tests { use super::*; - use std::str::FromStr; + use crate::fs::ensure_directory_exists; + use std::{fs, str::FromStr}; 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] fn config_roundtrip() { 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(); assert_eq!(expected, actual);