shitbox/corebox/commands/readlink/mod.rs

60 lines
2.0 KiB
Rust
Raw Permalink Normal View History

2023-01-22 10:24:21 -05:00
use clap::{Arg, ArgAction, ArgMatches, Command, ValueHint};
use shitbox::Cmd;
use std::{fs, path::PathBuf};
2023-01-22 10:24:21 -05:00
#[derive(Debug)]
2023-01-22 10:24:21 -05:00
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: &ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
2023-01-22 10:24:21 -05:00
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<shitbox::Path> {
Some(shitbox::Path::UsrBin)
2023-01-22 10:24:21 -05:00
}
}