Default directory for storage and data-dir per command

With this PR we default to the proper OS default directory for storing application data.
Since the reset-config command does not require a data directory (and causes side effects with the default one), the data directory is not initialized per command.
This commit is contained in:
Daniel Karzel 2021-01-28 16:05:14 +11:00
parent 967736766b
commit 9be6449e49
3 changed files with 31 additions and 15 deletions

View File

@ -5,9 +5,12 @@ use uuid::Uuid;
#[derive(structopt::StructOpt, Debug)]
pub struct Options {
// TODO: Default value should points to proper configuration folder in home folder
#[structopt(long = "data-dir", default_value = "./.swap-data/")]
pub data_dir: String,
#[structopt(
long = "data-dir",
help = "Provide a custom path to the data directory.",
parse(from_os_str)
)]
pub data_dir: Option<PathBuf>,
#[structopt(subcommand)]
pub cmd: Command,
@ -80,7 +83,7 @@ pub enum Resume {
pub struct Config {
#[structopt(
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)
)]
pub config_path: Option<PathBuf>,

View File

@ -6,17 +6,25 @@ use std::path::{Path, PathBuf};
// Linux: /home/<user>/.config/xmr-btc-swap/
// OSX: /Users/<user>/Library/Preferences/xmr-btc-swap/
#[allow(dead_code)]
fn config_dir() -> Option<PathBuf> {
fn default_config_dir() -> Option<PathBuf> {
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> {
config_dir()
default_config_dir()
.map(|dir| Path::join(&dir, "config.toml"))
.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/
#[allow(dead_code)]
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> {
if let Some(path) = file.parent() {
if !path.exists() {

View File

@ -20,7 +20,7 @@ use crate::{
};
use anyhow::{Context, Result};
use database::Database;
use fs::default_config_path;
use fs::{default_config_path, default_data_dir};
use prettytable::{row, Table};
use protocol::{alice, bob, bob::Builder, SwapAmounts};
use settings::Settings;
@ -53,13 +53,18 @@ async fn main() -> Result<()> {
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!(
"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)
.expect("Could not retrieve/initialize seed")
.into();
@ -78,7 +83,7 @@ async fn main() -> Result<()> {
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();
@ -117,7 +122,7 @@ async fn main() -> Result<()> {
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();
@ -162,7 +167,7 @@ async fn main() -> Result<()> {
}) => {
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(
seed,
@ -187,7 +192,7 @@ async fn main() -> Result<()> {
}) => {
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(
seed,
@ -229,7 +234,7 @@ fn init_settings(config_path: Option<PathBuf>) -> Result<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(
settings.bitcoin.wallet_name.as_str(),
settings.bitcoin.bitcoind_url,