Skip to content

Commit f7a5cd6

Browse files
committed
search bitcoind executable also in the PATH
when neither BITCOIND_EXE env var or feature is set, this looks for bitcoind executable in the PATH
1 parent 6f08f75 commit f7a5cd6

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ bitcoincore-rpc = "0.14.0"
1313
tempfile = "3.1"
1414
log = "0.4"
1515
home = "0.5.3" # use same ver in build-dep
16+
which = "4.2.5"
1617

1718
[dev-dependencies]
1819
env_logger = "0.8"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Utility to run a regtest bitcoind process, useful in integration testing environ
77

88
```rust
99
use bitcoincore_rpc::RpcApi;
10-
let bitcoind = bitcoind::BitcoinD::new("/usr/local/bin/bitcoind").unwrap();
10+
let exe_path = exe_path().expect("bitcoind executable must be provided in BITCOIND_EXE, or with a feature like '22_0', or be in PATH");
11+
let bitcoind = bitcoind::BitcoinD::new(exe_path).unwrap();
1112
assert_eq!(0, bitcoind.client.get_blockchain_info().unwrap().blocks);
1213
```
1314

src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ pub enum Error {
9595
NoFeature,
9696
/// Returned when calling methods requiring a env var to exist, but it's not
9797
NoEnvVar,
98-
/// Returned when calling methods requiring either a feature or env var, but both are absent
99-
NeitherFeatureNorEnvVar,
98+
/// Returned when calling methods requiring the bitcoind executable but none is found
99+
/// (no feature, no `BITCOIND_EXE`, no `bitcoind` in `PATH` )
100+
NoBitcoindExecutableFound,
100101
/// Returned when calling methods requiring either a feature or anv var, but both are present
101102
BothFeatureAndEnvVar,
102103
/// Wrapper of early exit status
@@ -112,7 +113,7 @@ impl fmt::Debug for Error {
112113
Error::Rpc(e) => write!(f, "{:?}", e),
113114
Error::NoFeature => write!(f, "Called a method requiring a feature to be set, but it's not"),
114115
Error::NoEnvVar => write!(f, "Called a method requiring env var `BITCOIND_EXE` to be set, but it's not"),
115-
Error::NeitherFeatureNorEnvVar => write!(f, "Called a method requiring env var `BITCOIND_EXE` or a feature to be set, but neither are set"),
116+
Error::NoBitcoindExecutableFound => write!(f, "`bitcoind` executable is required, provide it with one of the following: set env var `BITCOIND_EXE` or use a feature like \"22_0\" or have `bitcoind` executable in the `PATH`"),
116117
Error::BothFeatureAndEnvVar => write!(f, "Called a method requiring env var `BITCOIND_EXE` or a feature to be set, but both are set"),
117118
Error::EarlyExit(e) => write!(f, "The bitcoind process terminated early with exit code {}", e),
118119
Error::BothDirsSpecified => write!(f, "tempdir and staticdir cannot be enabled at same time in configuration options")
@@ -417,7 +418,9 @@ pub fn exe_path() -> Result<String, Error> {
417418
(Ok(_), Ok(_)) => Err(Error::BothFeatureAndEnvVar),
418419
(Ok(path), Err(_)) => Ok(path),
419420
(Err(_), Ok(path)) => Ok(path),
420-
(Err(_), Err(_)) => Err(Error::NeitherFeatureNorEnvVar),
421+
(Err(_), Err(_)) => which::which("bitcoind")
422+
.map_err(|_| Error::NoBitcoindExecutableFound)
423+
.map(|p| p.display().to_string()),
421424
}
422425
}
423426

0 commit comments

Comments
 (0)