mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-07 05:52:31 -04:00
Merge pull request #166 from comit-network/default-data-dir
Default directory for storage and data-dir per command
This commit is contained in:
commit
2adfd89ffd
6 changed files with 49 additions and 34 deletions
|
@ -5,9 +5,12 @@ use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(structopt::StructOpt, Debug)]
|
#[derive(structopt::StructOpt, Debug)]
|
||||||
pub struct Options {
|
pub struct Options {
|
||||||
// TODO: Default value should points to proper configuration folder in home folder
|
#[structopt(
|
||||||
#[structopt(long = "data-dir", default_value = "./.swap-data/")]
|
long = "data-dir",
|
||||||
pub data_dir: String,
|
help = "Provide a custom path to the data directory.",
|
||||||
|
parse(from_os_str)
|
||||||
|
)]
|
||||||
|
pub data_dir: Option<PathBuf>,
|
||||||
|
|
||||||
#[structopt(subcommand)]
|
#[structopt(subcommand)]
|
||||||
pub cmd: Command,
|
pub cmd: Command,
|
||||||
|
@ -80,7 +83,7 @@ pub enum Resume {
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
#[structopt(
|
#[structopt(
|
||||||
long = "config",
|
long = "config",
|
||||||
help = "Provide a custom path to a configuration file. The configuration file must be a toml file.",
|
help = "Provide a custom path to the configuration file. The configuration file must be a toml file.",
|
||||||
parse(from_os_str)
|
parse(from_os_str)
|
||||||
)]
|
)]
|
||||||
pub config_path: Option<PathBuf>,
|
pub config_path: Option<PathBuf>,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::fs::ensure_directory_exists;
|
use crate::{fs::ensure_directory_exists, settings::Settings};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use config::{Config, ConfigError};
|
use config::{Config, ConfigError};
|
||||||
use dialoguer::{theme::ColorfulTheme, Input};
|
use dialoguer::{theme::ColorfulTheme, Input};
|
||||||
|
@ -17,7 +17,6 @@ const DEFAULT_BITCOIND_TESTNET_URL: &str = "http://127.0.0.1:18332";
|
||||||
const DEFAULT_MONERO_WALLET_RPC_TESTNET_URL: &str = "http://127.0.0.1:38083/json_rpc";
|
const DEFAULT_MONERO_WALLET_RPC_TESTNET_URL: &str = "http://127.0.0.1:38083/json_rpc";
|
||||||
|
|
||||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)]
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)]
|
||||||
#[serde(deny_unknown_fields)]
|
|
||||||
pub struct File {
|
pub struct File {
|
||||||
pub bitcoin: Bitcoin,
|
pub bitcoin: Bitcoin,
|
||||||
pub monero: Monero,
|
pub monero: Monero,
|
||||||
|
@ -117,6 +116,14 @@ pub fn query_user_for_initial_testnet_config() -> Result<File> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn settings_from_config_file_and_defaults(config: File) -> Settings {
|
||||||
|
Settings::testnet(
|
||||||
|
config.bitcoin.bitcoind_url,
|
||||||
|
config.bitcoin.wallet_name,
|
||||||
|
config.monero.wallet_rpc_url,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -5,18 +5,23 @@ use std::path::{Path, PathBuf};
|
||||||
/// This is to store the configuration and seed files
|
/// This is to store the configuration and seed files
|
||||||
// Linux: /home/<user>/.config/xmr-btc-swap/
|
// Linux: /home/<user>/.config/xmr-btc-swap/
|
||||||
// OSX: /Users/<user>/Library/Preferences/xmr-btc-swap/
|
// OSX: /Users/<user>/Library/Preferences/xmr-btc-swap/
|
||||||
#[allow(dead_code)]
|
fn default_config_dir() -> Option<PathBuf> {
|
||||||
fn config_dir() -> Option<PathBuf> {
|
|
||||||
ProjectDirs::from("", "", "xmr-btc-swap").map(|proj_dirs| proj_dirs.config_dir().to_path_buf())
|
ProjectDirs::from("", "", "xmr-btc-swap").map(|proj_dirs| proj_dirs.config_dir().to_path_buf())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn default_config_path() -> anyhow::Result<PathBuf> {
|
pub fn default_config_path() -> anyhow::Result<PathBuf> {
|
||||||
config_dir()
|
default_config_dir()
|
||||||
.map(|dir| Path::join(&dir, "config.toml"))
|
.map(|dir| Path::join(&dir, "config.toml"))
|
||||||
.context("Could not generate default configuration path")
|
.context("Could not generate default configuration path")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This is to store the DB
|
||||||
|
// Linux: /home/<user>/.local/share/nectar/
|
||||||
|
// OSX: /Users/<user>/Library/Application Support/nectar/
|
||||||
|
pub fn default_data_dir() -> Option<std::path::PathBuf> {
|
||||||
|
ProjectDirs::from("", "", "nectar").map(|proj_dirs| proj_dirs.data_dir().to_path_buf())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn ensure_directory_exists(file: &Path) -> Result<(), std::io::Error> {
|
pub fn ensure_directory_exists(file: &Path) -> Result<(), std::io::Error> {
|
||||||
if let Some(path) = file.parent() {
|
if let Some(path) = file.parent() {
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
|
|
|
@ -17,7 +17,6 @@
|
||||||
)]
|
)]
|
||||||
|
|
||||||
pub mod bitcoin;
|
pub mod bitcoin;
|
||||||
pub mod config;
|
|
||||||
pub mod database;
|
pub mod database;
|
||||||
pub mod monero;
|
pub mod monero;
|
||||||
pub mod network;
|
pub mod network;
|
||||||
|
@ -26,5 +25,4 @@ pub mod seed;
|
||||||
pub mod settings;
|
pub mod settings;
|
||||||
pub mod trace;
|
pub mod trace;
|
||||||
|
|
||||||
mod fs;
|
|
||||||
mod serde_peer_id;
|
mod serde_peer_id;
|
||||||
|
|
|
@ -15,15 +15,16 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
cli::{Command, Options, Resume},
|
cli::{Command, Options, Resume},
|
||||||
config::{
|
config::{
|
||||||
initial_setup, query_user_for_initial_testnet_config, read_config, ConfigNotInitialized,
|
initial_setup, query_user_for_initial_testnet_config, read_config,
|
||||||
|
settings_from_config_file_and_defaults, ConfigNotInitialized,
|
||||||
},
|
},
|
||||||
|
settings::Settings,
|
||||||
};
|
};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use database::Database;
|
use database::Database;
|
||||||
use fs::default_config_path;
|
use fs::{default_config_path, default_data_dir};
|
||||||
use prettytable::{row, Table};
|
use prettytable::{row, Table};
|
||||||
use protocol::{alice, bob, bob::Builder, SwapAmounts};
|
use protocol::{alice, bob, bob::Builder, SwapAmounts};
|
||||||
use settings::Settings;
|
|
||||||
use std::{path::PathBuf, sync::Arc};
|
use std::{path::PathBuf, sync::Arc};
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use trace::init_tracing;
|
use trace::init_tracing;
|
||||||
|
@ -53,13 +54,18 @@ async fn main() -> Result<()> {
|
||||||
|
|
||||||
let opt = Options::from_args();
|
let opt = Options::from_args();
|
||||||
|
|
||||||
|
let data_dir = if let Some(data_dir) = opt.data_dir {
|
||||||
|
data_dir
|
||||||
|
} else {
|
||||||
|
default_data_dir().context("unable to determine default data path")?
|
||||||
|
};
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
"Database and Seed will be stored in directory: {}",
|
"Database and Seed will be stored in directory: {}",
|
||||||
opt.data_dir
|
data_dir.display()
|
||||||
);
|
);
|
||||||
let data_dir = std::path::Path::new(opt.data_dir.as_str()).to_path_buf();
|
|
||||||
let db_path = data_dir.join("database");
|
|
||||||
|
|
||||||
|
let db_path = data_dir.join("database");
|
||||||
let seed = config::seed::Seed::from_file_or_generate(&data_dir)
|
let seed = config::seed::Seed::from_file_or_generate(&data_dir)
|
||||||
.expect("Could not retrieve/initialize seed")
|
.expect("Could not retrieve/initialize seed")
|
||||||
.into();
|
.into();
|
||||||
|
@ -78,7 +84,7 @@ async fn main() -> Result<()> {
|
||||||
btc: receive_bitcoin,
|
btc: receive_bitcoin,
|
||||||
};
|
};
|
||||||
|
|
||||||
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
|
let (bitcoin_wallet, monero_wallet) = init_wallets(settings.wallets).await?;
|
||||||
|
|
||||||
let swap_id = Uuid::new_v4();
|
let swap_id = Uuid::new_v4();
|
||||||
|
|
||||||
|
@ -117,7 +123,7 @@ async fn main() -> Result<()> {
|
||||||
xmr: receive_monero,
|
xmr: receive_monero,
|
||||||
};
|
};
|
||||||
|
|
||||||
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
|
let (bitcoin_wallet, monero_wallet) = init_wallets(settings.wallets).await?;
|
||||||
|
|
||||||
let swap_id = Uuid::new_v4();
|
let swap_id = Uuid::new_v4();
|
||||||
|
|
||||||
|
@ -162,7 +168,7 @@ async fn main() -> Result<()> {
|
||||||
}) => {
|
}) => {
|
||||||
let settings = init_settings(config.config_path)?;
|
let settings = init_settings(config.config_path)?;
|
||||||
|
|
||||||
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
|
let (bitcoin_wallet, monero_wallet) = init_wallets(settings.wallets).await?;
|
||||||
|
|
||||||
let alice_factory = alice::Builder::new(
|
let alice_factory = alice::Builder::new(
|
||||||
seed,
|
seed,
|
||||||
|
@ -187,7 +193,7 @@ async fn main() -> Result<()> {
|
||||||
}) => {
|
}) => {
|
||||||
let settings = init_settings(config.config_path)?;
|
let settings = init_settings(config.config_path)?;
|
||||||
|
|
||||||
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
|
let (bitcoin_wallet, monero_wallet) = init_wallets(settings.wallets).await?;
|
||||||
|
|
||||||
let bob_factory = Builder::new(
|
let bob_factory = Builder::new(
|
||||||
seed,
|
seed,
|
||||||
|
@ -224,12 +230,12 @@ fn init_settings(config_path: Option<PathBuf>) -> Result<Settings> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let settings = Settings::from_config_file_and_defaults(config);
|
let settings = settings_from_config_file_and_defaults(config);
|
||||||
|
|
||||||
Ok(settings)
|
Ok(settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn setup_wallets(settings: settings::Wallets) -> Result<(bitcoin::Wallet, monero::Wallet)> {
|
async fn init_wallets(settings: settings::Wallets) -> Result<(bitcoin::Wallet, monero::Wallet)> {
|
||||||
let bitcoin_wallet = bitcoin::Wallet::new(
|
let bitcoin_wallet = bitcoin::Wallet::new(
|
||||||
settings.bitcoin.wallet_name.as_str(),
|
settings.bitcoin.wallet_name.as_str(),
|
||||||
settings.bitcoin.bitcoind_url,
|
settings.bitcoin.bitcoind_url,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::{bitcoin::Timelock, config::File};
|
use crate::bitcoin::Timelock;
|
||||||
use conquer_once::Lazy;
|
use conquer_once::Lazy;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
@ -9,15 +9,11 @@ pub struct Settings {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
impl Settings {
|
||||||
pub fn from_config_file_and_defaults(config: File) -> Self {
|
pub fn testnet(
|
||||||
Settings::testnet(
|
bitcoind_url: Url,
|
||||||
config.bitcoin.bitcoind_url,
|
bitcoin_wallet_name: String,
|
||||||
config.bitcoin.wallet_name,
|
monero_wallet_rpc_url: Url,
|
||||||
config.monero.wallet_rpc_url,
|
) -> Self {
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn testnet(bitcoind_url: Url, bitcoin_wallet_name: String, monero_wallet_rpc_url: Url) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
wallets: Wallets::testnet(bitcoind_url, bitcoin_wallet_name, monero_wallet_rpc_url),
|
wallets: Wallets::testnet(bitcoind_url, bitcoin_wallet_name, monero_wallet_rpc_url),
|
||||||
protocol: Protocol::testnet(),
|
protocol: Protocol::testnet(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue