From 9c27697d3ae95795b4a9ec56ed74d5952748d3d2 Mon Sep 17 00:00:00 2001 From: liam <31192478+terror@users.noreply.github.com> Date: Fri, 17 Feb 2023 14:43:36 -0500 Subject: [PATCH] Add `--config-dir` option (#1697) --- deploy/ord.service | 2 +- src/options.rs | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/deploy/ord.service b/deploy/ord.service index e24060d518..f1d7b74043 100644 --- a/deploy/ord.service +++ b/deploy/ord.service @@ -11,7 +11,7 @@ Environment=RUST_LOG=info ExecStart=/usr/local/bin/ord \ --bitcoin-data-dir /var/lib/bitcoind \ --data-dir /var/lib/ord \ - --config /var/lib/ord/ord.yaml \ + --config-dir /var/lib/ord \ --chain ${CHAIN} \ --index-sats \ server \ diff --git a/src/options.rs b/src/options.rs index ea51a3905e..6b0823f2a2 100644 --- a/src/options.rs +++ b/src/options.rs @@ -18,6 +18,8 @@ pub(crate) struct Options { pub(crate) chain_argument: Chain, #[clap(long, help = "Load configuration from .")] pub(crate) config: Option, + #[clap(long, help = "Load configuration from .")] + pub(crate) config_dir: Option, #[clap(long, help = "Load Bitcoin Core RPC cookie file from .")] pub(crate) cookie_file: Option, #[clap(long, help = "Store index in .")] @@ -116,7 +118,12 @@ impl Options { pub(crate) fn load_config(&self) -> Result { match &self.config { Some(path) => Ok(serde_yaml::from_reader(File::open(path)?)?), - None => Ok(Default::default()), + None => match &self.config_dir { + Some(dir) if dir.join("ord.yaml").exists() => { + Ok(serde_yaml::from_reader(File::open(dir.join("ord.yaml"))?)?) + } + Some(_) | None => Ok(Default::default()), + }, } } @@ -557,4 +564,35 @@ mod tests { } ); } + + #[test] + fn config_is_loaded_from_config_dir_option_path() { + let id = "8d363b28528b0cb86b5fd48615493fb175bdf132d2a3d20b4251bba3f130a5abi0" + .parse::() + .unwrap(); + + let tempdir = TempDir::new().unwrap(); + + fs::write( + tempdir.path().join("ord.yaml"), + format!("hidden:\n- \"{id}\""), + ) + .unwrap(); + + assert_eq!( + Arguments::try_parse_from([ + "ord", + "--config-dir", + tempdir.path().to_str().unwrap(), + "index", + ]) + .unwrap() + .options + .load_config() + .unwrap(), + Config { + hidden: iter::once(id).collect(), + } + ); + } }