Add realpath
applet
This commit is contained in:
parent
862ad82b47
commit
7630e016c9
@ -44,6 +44,7 @@ code between applets, making for an overall smaller binary.
|
|||||||
- nproc
|
- nproc
|
||||||
- printenv
|
- printenv
|
||||||
- pwd
|
- pwd
|
||||||
|
- readlink
|
||||||
- rev
|
- rev
|
||||||
- rm
|
- rm
|
||||||
- rmdir
|
- rmdir
|
||||||
|
@ -35,6 +35,7 @@ mod nologin;
|
|||||||
mod nproc;
|
mod nproc;
|
||||||
mod printenv;
|
mod printenv;
|
||||||
mod pwd;
|
mod pwd;
|
||||||
|
mod realpath;
|
||||||
mod rev;
|
mod rev;
|
||||||
mod rm;
|
mod rm;
|
||||||
mod rmdir;
|
mod rmdir;
|
||||||
@ -92,6 +93,7 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
|
|||||||
"nproc" => Some(Box::new(nproc::Nproc::default())),
|
"nproc" => Some(Box::new(nproc::Nproc::default())),
|
||||||
"printenv" => Some(Box::new(printenv::Printenv::default())),
|
"printenv" => Some(Box::new(printenv::Printenv::default())),
|
||||||
"pwd" => Some(Box::new(pwd::Pwd::default())),
|
"pwd" => Some(Box::new(pwd::Pwd::default())),
|
||||||
|
"realpath" => Some(Box::new(realpath::Realpath::default())),
|
||||||
"rev" => Some(Box::new(rev::Rev::default())),
|
"rev" => Some(Box::new(rev::Rev::default())),
|
||||||
"rm" => Some(Box::new(rm::Rm::default())),
|
"rm" => Some(Box::new(rm::Rm::default())),
|
||||||
"rmdir" => Some(Box::new(rmdir::Rmdir::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",
|
"base32",
|
||||||
"base64",
|
"base64",
|
||||||
"basename",
|
"basename",
|
||||||
@ -135,6 +137,7 @@ pub static COMMANDS: [&str; 38] = [
|
|||||||
"nproc",
|
"nproc",
|
||||||
"printenv",
|
"printenv",
|
||||||
"pwd",
|
"pwd",
|
||||||
|
"realpath",
|
||||||
"rev",
|
"rev",
|
||||||
"rm",
|
"rm",
|
||||||
"rmdir",
|
"rmdir",
|
||||||
|
66
src/cmd/realpath/mod.rs
Normal file
66
src/cmd/realpath/mod.rs
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user