From eddfc7bf5ed18bf2ff228af23d9cdf73aee614f9 Mon Sep 17 00:00:00 2001 From: Torkel Rogstad Date: Thu, 28 Dec 2023 12:26:19 +0100 Subject: [PATCH] Display path to default datadir in help output (#2881) --- src/index.rs | 9 ++++----- src/options.rs | 23 +++++++++-------------- src/subcommand/preview.rs | 2 +- src/subcommand/server.rs | 16 +++++----------- 4 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/index.rs b/src/index.rs index 97ac0c027c..38ce19a9b3 100644 --- a/src/index.rs +++ b/src/index.rs @@ -209,11 +209,10 @@ impl Index { pub fn open(options: &Options) -> Result { let client = options.bitcoin_rpc_client()?; - let path = if let Some(path) = &options.index { - path.clone() - } else { - options.data_dir()?.join("index.redb") - }; + let path = options + .index + .clone() + .unwrap_or(options.data_dir().clone().join("index.redb")); if let Err(err) = fs::create_dir_all(path.parent().unwrap()) { bail!( diff --git a/src/options.rs b/src/options.rs index 0830c27739..e3a2b1f068 100644 --- a/src/options.rs +++ b/src/options.rs @@ -26,8 +26,8 @@ pub struct Options { pub(crate) config_dir: Option, #[arg(long, help = "Load Bitcoin Core RPC cookie file from .")] pub(crate) cookie_file: Option, - #[arg(long, help = "Store index in .")] - pub(crate) data_dir: Option, + #[arg(long, help = "Store index in .", default_value_os_t = Options::default_data_dir())] + pub(crate) data_dir: PathBuf, #[arg( long, help = "Set index cache to bytes. By default takes 1/4 of available RAM." @@ -139,15 +139,14 @@ impl Options { Ok(path.join(".cookie")) } - pub(crate) fn data_dir(&self) -> Result { - let base = match &self.data_dir { - Some(base) => base.clone(), - None => dirs::data_dir() - .ok_or_else(|| anyhow!("failed to retrieve data dir"))? - .join("ord"), - }; + fn default_data_dir() -> PathBuf { + dirs::data_dir() + .map(|dir| dir.join("ord")) + .expect("failed to retrieve data dir") + } - Ok(self.chain().join_with_data_dir(&base)) + pub(crate) fn data_dir(&self) -> PathBuf { + self.chain().join_with_data_dir(&self.data_dir) } pub(crate) fn load_config(&self) -> Result { @@ -443,7 +442,6 @@ mod tests { .unwrap() .options .data_dir() - .unwrap() .display() .to_string(); assert!( @@ -458,7 +456,6 @@ mod tests { .unwrap() .options .data_dir() - .unwrap() .display() .to_string(); assert!( @@ -484,7 +481,6 @@ mod tests { .unwrap() .options .data_dir() - .unwrap() .display() .to_string(); assert!( @@ -504,7 +500,6 @@ mod tests { .unwrap() .options .data_dir() - .unwrap() .display() .to_string(); diff --git a/src/subcommand/preview.rs b/src/subcommand/preview.rs index f0dc76d69c..acf0a3b3da 100644 --- a/src/subcommand/preview.rs +++ b/src/subcommand/preview.rs @@ -65,7 +65,7 @@ impl Preview { let options = Options { chain_argument: Chain::Regtest, bitcoin_data_dir: Some(bitcoin_data_dir), - data_dir: Some(tmpdir.path().into()), + data_dir: tmpdir.path().into(), rpc_url: Some(format!("127.0.0.1:{rpc_port}")), index_sats: true, ..Options::default() diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 4a6864f315..0c5dad2ce7 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -397,14 +397,10 @@ impl Server { })) } - fn acme_cache(acme_cache: Option<&PathBuf>, options: &Options) -> Result { - let acme_cache = if let Some(acme_cache) = acme_cache { - acme_cache.clone() - } else { - options.data_dir()?.join("acme-cache") - }; - - Ok(acme_cache) + fn acme_cache(acme_cache: Option<&PathBuf>, options: &Options) -> PathBuf { + acme_cache + .unwrap_or(&options.data_dir().join("acme-cache")) + .to_path_buf() } fn acme_domains(&self) -> Result> { @@ -439,7 +435,7 @@ impl Server { .cache_option(Some(DirCache::new(Self::acme_cache( self.acme_cache.as_ref(), options, - )?))) + )))) .directory(if cfg!(test) { LETS_ENCRYPT_STAGING_DIRECTORY } else { @@ -1913,7 +1909,6 @@ mod tests { fn acme_cache_defaults_to_data_dir() { let arguments = Arguments::try_parse_from(["ord", "--data-dir", "foo", "server"]).unwrap(); let acme_cache = Server::acme_cache(None, &arguments.options) - .unwrap() .display() .to_string(); assert!( @@ -1932,7 +1927,6 @@ mod tests { Arguments::try_parse_from(["ord", "--data-dir", "foo", "server", "--acme-cache", "bar"]) .unwrap(); let acme_cache = Server::acme_cache(Some(&"bar".into()), &arguments.options) - .unwrap() .display() .to_string(); assert_eq!(acme_cache, "bar")