Skip to content

Commit

Permalink
Append network to data dir (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Oct 13, 2022
1 parent ce3d0c9 commit b22cd1a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 30 deletions.
8 changes: 7 additions & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ impl Index {
let client = Client::new(&rpc_url, Auth::CookieFile(cookie_file))
.context("Failed to connect to RPC URL")?;

let database_path = options.data_dir()?.join("index.redb");
let data_dir = options.data_dir()?;

if let Err(err) = fs::create_dir_all(&data_dir) {
bail!("Failed to create data dir `{}`: {err}", data_dir.display());
}

let database_path = data_dir.join("index.redb");

let database = match unsafe { redb::Database::open(&database_path) } {
Ok(database) => database,
Expand Down
63 changes: 37 additions & 26 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,14 @@ impl Options {
}

pub(crate) fn data_dir(&self) -> Result<PathBuf> {
if let Some(data_dir) = &self.data_dir {
return Ok(data_dir.clone());
}

let path = dirs::data_dir()
.ok_or_else(|| anyhow!("Failed to retrieve data dir"))?
.join("ord");

let path = self.chain.join_with_data_dir(&path);

if let Err(err) = fs::create_dir_all(&path) {
bail!("Failed to create data dir `{}`: {err}", path.display());
}
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"),
};

Ok(path)
Ok(self.chain.join_with_data_dir(&base))
}

pub(crate) fn bitcoin_rpc_client(&self) -> Result<Client> {
Expand Down Expand Up @@ -228,9 +221,8 @@ mod tests {

#[test]
fn mainnet_cookie_file_path() {
let arguments = Arguments::try_parse_from(&["ord", "index"]).unwrap();

let cookie_file = arguments
let cookie_file = Arguments::try_parse_from(&["ord", "index"])
.unwrap()
.options
.cookie_file()
.unwrap()
Expand Down Expand Up @@ -280,20 +272,39 @@ mod tests {

#[test]
fn mainnet_data_dir() {
let arguments = Arguments::try_parse_from(&["ord", "index"]).unwrap();

let data_dir = arguments.options.data_dir().unwrap().display().to_string();

assert!(data_dir.ends_with("/ord"));
let data_dir = Arguments::try_parse_from(&["ord", "index"])
.unwrap()
.options
.data_dir()
.unwrap()
.display()
.to_string();
assert!(data_dir.ends_with("/ord"), "{data_dir}");
}

#[test]
fn othernet_data_dir() {
let arguments = Arguments::try_parse_from(&["ord", "--chain=signet", "index"]).unwrap();

let data_dir = arguments.options.data_dir().unwrap().display().to_string();
let data_dir = Arguments::try_parse_from(&["ord", "--chain=signet", "index"])
.unwrap()
.options
.data_dir()
.unwrap()
.display()
.to_string();
assert!(data_dir.ends_with("/ord/signet"), "{data_dir}");
}

assert!(data_dir.ends_with("/ord/signet"));
#[test]
fn network_is_joined_with_data_dir() {
let data_dir =
Arguments::try_parse_from(&["ord", "--chain=signet", "--data-dir", "foo", "index"])
.unwrap()
.options
.data_dir()
.unwrap()
.display()
.to_string();
assert!(data_dir.ends_with("foo/signet"), "{data_dir}");
}

#[test]
Expand Down
15 changes: 12 additions & 3 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,20 @@ impl Server {
}

fn acme_cache(acme_cache: Option<&PathBuf>, options: &Options) -> Result<PathBuf> {
if let Some(acme_cache) = acme_cache {
Ok(acme_cache.clone())
let acme_cache = if let Some(acme_cache) = acme_cache {
acme_cache.clone()
} else {
Ok(options.data_dir()?.join("acme-cache"))
options.data_dir()?.join("acme-cache")
};

if let Err(err) = fs::create_dir_all(&acme_cache) {
bail!(
"Failed to create acme cache dir `{}`: {err}",
acme_cache.display()
);
}

Ok(acme_cache)
}

fn acme_domains(acme_domain: &Vec<String>) -> Result<Vec<String>> {
Expand Down

0 comments on commit b22cd1a

Please sign in to comment.