diff --git a/swap/src/cli.rs b/swap/src/cli.rs index 1f72e89d..6996f76a 100644 --- a/swap/src/cli.rs +++ b/swap/src/cli.rs @@ -47,6 +47,10 @@ pub enum Command { }, History, Resume(Resume), + ResetConfig { + #[structopt(flatten)] + config: Config, + }, } #[derive(structopt::StructOpt, Debug)] diff --git a/swap/src/config.rs b/swap/src/config.rs index 4eb21ceb..55aabb0c 100644 --- a/swap/src/config.rs +++ b/swap/src/config.rs @@ -1,4 +1,4 @@ -use crate::fs::ensure_directory_exists; +use crate::fs::{default_config_path, ensure_directory_exists}; use anyhow::{Context, Result}; use config::{Config, ConfigError}; use dialoguer::{theme::ColorfulTheme, Input}; @@ -117,6 +117,21 @@ pub fn query_user_for_initial_testnet_config() -> Result { }) } +pub fn reset_config(config_path: Option) -> anyhow::Result<()> { + let config_path = if let Some(config_path) = config_path { + config_path + } else { + default_config_path()? + }; + + fs::remove_file(&config_path) + .with_context(|| format!("failed to remove config file {}", config_path.display()))?; + + info!("Config file at {} was removed successfully. Initial setup will be re-triggered upon starting a swap.", config_path.as_path().display()); + + Ok(()) +} + #[cfg(test)] mod tests { use super::*; @@ -139,8 +154,10 @@ mod tests { }; initial_setup(config_path.clone(), || Ok(expected.clone())).unwrap(); - let actual = read_config(config_path).unwrap().unwrap(); - + let actual = read_config(config_path.clone()).unwrap().unwrap(); assert_eq!(expected, actual); + + reset_config(Some(config_path.clone())).unwrap(); + assert!(!config_path.exists()); } } diff --git a/swap/src/main.rs b/swap/src/main.rs index 78d1d7af..41c1858e 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -15,7 +15,8 @@ use crate::{ cli::{Command, Options, Resume}, config::{ - initial_setup, query_user_for_initial_testnet_config, read_config, ConfigNotInitialized, + initial_setup, query_user_for_initial_testnet_config, read_config, reset_config, + ConfigNotInitialized, }, }; use anyhow::{Context, Result}; @@ -204,6 +205,7 @@ async fn main() -> Result<()> { tokio::spawn(async move { event_loop.run().await }); bob::run(swap).await?; } + Command::ResetConfig { config } => reset_config(config.config_path)?, }; Ok(())