Skip to content

Commit 9fde067

Browse files
authored
Create which.rs
1 parent d405225 commit 9fde067

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

source-code/src/commands/which.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use anyhow::{anyhow, Result};
2+
use std::process::Command;
3+
use crate::{config, output, state};
4+
5+
pub fn run(package: &str) -> Result<()> {
6+
// Try `which` in the HNM profile bin dir first
7+
let profile_bin = config::profile_dir().join("bin").join(package);
8+
9+
if profile_bin.exists() {
10+
output::ok(&format!("{}", profile_bin.display()));
11+
return Ok(());
12+
}
13+
14+
// Fallback: `which` from PATH
15+
let out = Command::new("which").arg(package).output();
16+
match out {
17+
Ok(o) if o.status.success() => {
18+
let path = String::from_utf8_lossy(&o.stdout).trim().to_string();
19+
output::ok(&path);
20+
}
21+
_ => {
22+
// Not in profile bin or PATH — maybe installed as a different binary name
23+
if state::is_installed(package) {
24+
let profile = config::profile_dir();
25+
output::warn(&format!(
26+
"'{}' is installed but its binary was not found in {}",
27+
package,
28+
profile.join("bin").display()
29+
));
30+
output::dim("The package may provide binaries under different names.");
31+
output::dim(&format!("Check: ls {}",
32+
profile.join("bin").display()));
33+
} else {
34+
return Err(anyhow!("'{}' is not installed — run `hnm install {}`", package, package));
35+
}
36+
}
37+
}
38+
Ok(())
39+
}

0 commit comments

Comments
 (0)