Add `realpath` applet

This commit is contained in:
Nathan Fisher 2023-01-22 00:26:26 -05:00
parent 862ad82b47
commit 7630e016c9
3 changed files with 71 additions and 1 deletions

View File

@ -44,6 +44,7 @@ code between applets, making for an overall smaller binary.
- nproc
- printenv
- pwd
- readlink
- rev
- rm
- rmdir

View File

@ -35,6 +35,7 @@ mod nologin;
mod nproc;
mod printenv;
mod pwd;
mod realpath;
mod rev;
mod rm;
mod rmdir;
@ -92,6 +93,7 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
"nproc" => Some(Box::new(nproc::Nproc::default())),
"printenv" => Some(Box::new(printenv::Printenv::default())),
"pwd" => Some(Box::new(pwd::Pwd::default())),
"realpath" => Some(Box::new(realpath::Realpath::default())),
"rev" => Some(Box::new(rev::Rev::default())),
"rm" => Some(Box::new(rm::Rm::default())),
"rmdir" => Some(Box::new(rmdir::Rmdir::default())),
@ -107,7 +109,7 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
}
}
pub static COMMANDS: [&str; 38] = [
pub static COMMANDS: [&str; 39] = [
"base32",
"base64",
"basename",
@ -135,6 +137,7 @@ pub static COMMANDS: [&str; 38] = [
"nproc",
"printenv",
"pwd",
"realpath",
"rev",
"rm",
"rmdir",

66
src/cmd/realpath/mod.rs Normal file
View File

@ -0,0 +1,66 @@
use super::Cmd;
use clap::{Arg, ArgAction, Command, ValueHint};
use std::{env, fs, io, process};
#[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),
])
}
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
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)
}
}