2023-01-22 00:26:26 -05:00
|
|
|
use super::Cmd;
|
|
|
|
use clap::{Arg, ArgAction, Command, ValueHint};
|
2023-02-04 08:54:27 -05:00
|
|
|
use std::{env, fs, process};
|
2023-01-22 00:26:26 -05:00
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct Realpath;
|
|
|
|
|
|
|
|
impl Cmd for Realpath {
|
|
|
|
fn cli(&self) -> clap::Command {
|
|
|
|
Command::new("realpath")
|
|
|
|
.about("return resolved physical path")
|
|
|
|
.author("Nathan Fisher")
|
|
|
|
.version(env!("CARGO_PKG_VERSION"))
|
|
|
|
.args([
|
|
|
|
Arg::new("quiet")
|
|
|
|
.short('q')
|
|
|
|
.long("quiet")
|
|
|
|
.help("ignore warnings")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
Arg::new("path")
|
|
|
|
.value_hint(ValueHint::AnyPath)
|
|
|
|
.num_args(1..)
|
|
|
|
.required(false),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2023-02-04 08:54:27 -05:00
|
|
|
fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
|
2023-01-22 00:26:26 -05:00
|
|
|
if let Some(files) = matches.get_many::<String>("path") {
|
|
|
|
let mut erred = false;
|
|
|
|
for f in files {
|
|
|
|
match fs::canonicalize(f) {
|
|
|
|
Ok(p) => println!("{}", p.display()),
|
|
|
|
Err(e) => {
|
|
|
|
if !matches.get_flag("quiet") {
|
|
|
|
erred = true;
|
|
|
|
} else {
|
|
|
|
return Err(e.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if erred {
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match env::current_dir().and_then(|d| fs::canonicalize(d)) {
|
|
|
|
Ok(p) => println!("{}", p.display()),
|
|
|
|
Err(e) => {
|
|
|
|
if matches.get_flag("quiet") {
|
|
|
|
process::exit(1);
|
|
|
|
} else {
|
|
|
|
return Err(e.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path(&self) -> Option<crate::Path> {
|
|
|
|
Some(crate::Path::Bin)
|
|
|
|
}
|
|
|
|
}
|