74 lines
2.2 KiB
Rust
74 lines
2.2 KiB
Rust
|
use super::Cmd;
|
||
|
use clap::{Arg, Command};
|
||
|
use std::{env, fs::File, io, os::unix::prelude::MetadataExt, path::PathBuf, process};
|
||
|
|
||
|
#[derive(Debug, Default)]
|
||
|
pub struct Which;
|
||
|
|
||
|
impl Cmd for Which {
|
||
|
fn name(&self) -> &str {
|
||
|
"which"
|
||
|
}
|
||
|
|
||
|
fn cli(&self) -> clap::Command {
|
||
|
Command::new("which")
|
||
|
.about("Write the full path of COMMAND(s) to standard output")
|
||
|
.author("Nathan Fisher")
|
||
|
.version(env!("CARGO_PKG_VERSION"))
|
||
|
.arg(Arg::new("COMMAND").num_args(1..))
|
||
|
}
|
||
|
|
||
|
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> {
|
||
|
let Some(matches) = matches else {
|
||
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
|
||
|
};
|
||
|
let rawpath = if let Ok(p) = env::var("PATH") {
|
||
|
p
|
||
|
} else {
|
||
|
"/usr/bin:/bin".to_string()
|
||
|
};
|
||
|
let path: Vec<_> = rawpath.split(':').collect();
|
||
|
let mut failures = 0;
|
||
|
if let Some(commands) = matches.get_many::<String>("COMMAND") {
|
||
|
for command in commands {
|
||
|
match which(command, &path) {
|
||
|
Some(p) => println!("{p}"),
|
||
|
None => {
|
||
|
println!("{}: no {} in ({})", self.name(), command, &rawpath);
|
||
|
failures += 1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
let _res = self.cli().print_help();
|
||
|
process::exit(255);
|
||
|
}
|
||
|
if failures > 0 {
|
||
|
process::exit(failures);
|
||
|
} else {
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn path(&self) -> Option<crate::Path> {
|
||
|
Some(crate::Path::UsrBin)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn which(command: &str, path: &[&str]) -> Option<String> {
|
||
|
for p in path {
|
||
|
let file = [p, command].iter().collect::<PathBuf>();
|
||
|
if file.exists() {
|
||
|
if let Ok(exe) = File::open(&file) {
|
||
|
if let Ok(meta) = exe.metadata() {
|
||
|
let mode = meta.mode();
|
||
|
if mode & 0o111 != 0 {
|
||
|
return Some(format!("{}", file.display()));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
None
|
||
|
}
|