Skip to content

Commit

Permalink
Add --config-dir option (ordinals#1697)
Browse files Browse the repository at this point in the history
  • Loading branch information
terror committed Feb 17, 2023
1 parent 3591ce0 commit f473aa6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion deploy/ord.service
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
40 changes: 39 additions & 1 deletion src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub(crate) struct Options {
pub(crate) chain_argument: Chain,
#[clap(long, help = "Load configuration from <CONFIG>.")]
pub(crate) config: Option<PathBuf>,
#[clap(long, help = "Load configuration from <CONFIG_DIR>.")]
pub(crate) config_dir: Option<PathBuf>,
#[clap(long, help = "Load Bitcoin Core RPC cookie file from <COOKIE_FILE>.")]
pub(crate) cookie_file: Option<PathBuf>,
#[clap(long, help = "Store index in <DATA_DIR>.")]
Expand Down Expand Up @@ -116,7 +118,12 @@ impl Options {
pub(crate) fn load_config(&self) -> Result<Config> {
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()),
},
}
}

Expand Down Expand Up @@ -557,4 +564,35 @@ mod tests {
}
);
}

#[test]
fn config_is_loaded_from_config_dir_option_path() {
let id = "8d363b28528b0cb86b5fd48615493fb175bdf132d2a3d20b4251bba3f130a5abi0"
.parse::<InscriptionId>()
.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(),
}
);
}
}

0 comments on commit f473aa6

Please sign in to comment.