Add readlink
applet
This commit is contained in:
parent
7630e016c9
commit
9a7af682b7
@ -45,6 +45,7 @@ code between applets, making for an overall smaller binary.
|
|||||||
- printenv
|
- printenv
|
||||||
- pwd
|
- pwd
|
||||||
- readlink
|
- readlink
|
||||||
|
- realpath
|
||||||
- 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 readlink;
|
||||||
mod realpath;
|
mod realpath;
|
||||||
mod rev;
|
mod rev;
|
||||||
mod rm;
|
mod rm;
|
||||||
@ -93,6 +94,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())),
|
||||||
|
"readlink" => Some(Box::new(readlink::Readlink::default())),
|
||||||
"realpath" => Some(Box::new(realpath::Realpath::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())),
|
||||||
@ -109,7 +111,7 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub static COMMANDS: [&str; 39] = [
|
pub static COMMANDS: [&str; 40] = [
|
||||||
"base32",
|
"base32",
|
||||||
"base64",
|
"base64",
|
||||||
"basename",
|
"basename",
|
||||||
@ -137,6 +139,7 @@ pub static COMMANDS: [&str; 39] = [
|
|||||||
"nproc",
|
"nproc",
|
||||||
"printenv",
|
"printenv",
|
||||||
"pwd",
|
"pwd",
|
||||||
|
"readlink",
|
||||||
"realpath",
|
"realpath",
|
||||||
"rev",
|
"rev",
|
||||||
"rm",
|
"rm",
|
||||||
|
62
src/cmd/readlink/mod.rs
Normal file
62
src/cmd/readlink/mod.rs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
use crate::Cmd;
|
||||||
|
use clap::{Arg, ArgAction, ArgMatches, Command, ValueHint};
|
||||||
|
use std::{fs, io, path::PathBuf};
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct Readlink;
|
||||||
|
|
||||||
|
impl Cmd for Readlink {
|
||||||
|
fn cli(&self) -> clap::Command {
|
||||||
|
Command::new("readlink")
|
||||||
|
.about("Print symbolic link target or canonical file name")
|
||||||
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
|
.author("Nathan Fisher")
|
||||||
|
.args([
|
||||||
|
Arg::new("path")
|
||||||
|
.value_name("PATH")
|
||||||
|
.value_hint(ValueHint::AnyPath)
|
||||||
|
.num_args(1..)
|
||||||
|
.required(true),
|
||||||
|
Arg::new("canon")
|
||||||
|
.help("Canonicalize path")
|
||||||
|
.short('f')
|
||||||
|
.visible_short_alias('c')
|
||||||
|
.long("canonicalize")
|
||||||
|
.action(ArgAction::SetTrue),
|
||||||
|
Arg::new("newline")
|
||||||
|
.help("Do not print the terminating newline.")
|
||||||
|
.short('n')
|
||||||
|
.long("no-newline")
|
||||||
|
.action(ArgAction::SetTrue),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, matches: Option<&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(paths) = matches.get_many::<String>("path") {
|
||||||
|
let paths: Vec<_> = paths.collect();
|
||||||
|
let len = paths.len();
|
||||||
|
if matches.get_flag("newline") && len > 1 {
|
||||||
|
eprintln!("readlink: ignoring --no-newline with multiple arguments");
|
||||||
|
}
|
||||||
|
for p in paths {
|
||||||
|
let path = if matches.get_flag("canon") {
|
||||||
|
PathBuf::from(p).canonicalize()?
|
||||||
|
} else {
|
||||||
|
fs::read_link(p)?
|
||||||
|
};
|
||||||
|
print!("{}", path.display());
|
||||||
|
if !matches.get_flag("newline") || len > 1 {
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn path(&self) -> Option<crate::Path> {
|
||||||
|
Some(crate::Path::UsrBin)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user