Skip to content

Commit

Permalink
Display path to default datadir in help output (ordinals#2881)
Browse files Browse the repository at this point in the history
  • Loading branch information
torkelrogstad committed Dec 28, 2023
1 parent 2db64cb commit eddfc7b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 31 deletions.
9 changes: 4 additions & 5 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,10 @@ impl Index {
pub fn open(options: &Options) -> Result<Self> {
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!(
Expand Down
23 changes: 9 additions & 14 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub struct Options {
pub(crate) config_dir: Option<PathBuf>,
#[arg(long, help = "Load Bitcoin Core RPC cookie file from <COOKIE_FILE>.")]
pub(crate) cookie_file: Option<PathBuf>,
#[arg(long, help = "Store index in <DATA_DIR>.")]
pub(crate) data_dir: Option<PathBuf>,
#[arg(long, help = "Store index in <DATA_DIR>.", default_value_os_t = Options::default_data_dir())]
pub(crate) data_dir: PathBuf,
#[arg(
long,
help = "Set index cache to <DB_CACHE_SIZE> bytes. By default takes 1/4 of available RAM."
Expand Down Expand Up @@ -139,15 +139,14 @@ impl Options {
Ok(path.join(".cookie"))
}

pub(crate) fn data_dir(&self) -> Result<PathBuf> {
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<Config> {
Expand Down Expand Up @@ -443,7 +442,6 @@ mod tests {
.unwrap()
.options
.data_dir()
.unwrap()
.display()
.to_string();
assert!(
Expand All @@ -458,7 +456,6 @@ mod tests {
.unwrap()
.options
.data_dir()
.unwrap()
.display()
.to_string();
assert!(
Expand All @@ -484,7 +481,6 @@ mod tests {
.unwrap()
.options
.data_dir()
.unwrap()
.display()
.to_string();
assert!(
Expand All @@ -504,7 +500,6 @@ mod tests {
.unwrap()
.options
.data_dir()
.unwrap()
.display()
.to_string();

Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 5 additions & 11 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,10 @@ impl Server {
}))
}

fn acme_cache(acme_cache: Option<&PathBuf>, options: &Options) -> Result<PathBuf> {
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<Vec<String>> {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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!(
Expand All @@ -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")
Expand Down

0 comments on commit eddfc7b

Please sign in to comment.