From 091bf713bd1c2e08a95a2a9009e191d1e2b35a44 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Fri, 20 Jan 2023 22:46:16 -0500 Subject: [PATCH] Add `pwd` applet --- src/cmd/mod.rs | 70 ++++++++++++++++++++-------------------------- src/cmd/pwd/mod.rs | 46 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 39 deletions(-) diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 6d718d6..426884e 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -42,16 +42,6 @@ mod which; mod whoami; mod yes; -#[allow(clippy::module_name_repetitions)] -pub use { - self::hostname::Hostname, self::shitbox::Shitbox, base32::Base32, base64::Base64, - basename::Basename, bootstrap::Bootstrap, chown::Chown, clear::Clear, cut::Cut, - dirname::Dirname, echo::Echo, factor::Factor, fold::Fold, groups::Groups, head::Head, - link::Link, mountpoint::Mountpoint, nologin::Nologin, nproc::Nproc, r#false::False, - r#true::True, rev::Rev, rm::Rm, rmdir::Rmdir, sleep::Sleep, sync::Sync as SyncCmd, - unlink::Unlink, which::Which, whoami::Whoami, yes::Yes, -}; - /// Defines a command or applet, it's cli interface, and it's installation directory /// relative to the binary pub trait Cmd: fmt::Debug + Sync { @@ -71,41 +61,42 @@ pub trait Cmd: fmt::Debug + Sync { #[allow(clippy::box_default)] pub fn get(name: &str) -> Option> { match name { - "base64" => Some(Box::new(Base64::default())), - "base32" => Some(Box::new(Base32::default())), - "basename" => Some(Box::new(Basename::default())), - "bootstrap" => Some(Box::new(Bootstrap::default())), + "base64" => Some(Box::new(base64::Base64::default())), + "base32" => Some(Box::new(base32::Base32::default())), + "basename" => Some(Box::new(basename::Basename::default())), + "bootstrap" => Some(Box::new(bootstrap::Bootstrap::default())), "chmod" => Some(Box::new(chmod::Chmod::default())), "chown" => Some(Box::new(chown::Chown::default())), - "clear" => Some(Box::new(Clear::default())), - "cut" => Some(Box::new(Cut::default())), - "dirname" => Some(Box::new(Dirname::default())), - "echo" => Some(Box::new(Echo::default())), - "factor" => Some(Box::new(Factor::default())), - "false" => Some(Box::new(False::default())), - "fold" => Some(Box::new(Fold::default())), - "groups" => Some(Box::new(Groups::default())), - "head" => Some(Box::new(Head::default())), - "link" => Some(Box::new(Link::default())), - "mountpoint" => Some(Box::new(Mountpoint::default())), - "nologin" => Some(Box::new(Nologin::default())), - "nproc" => Some(Box::new(Nproc::default())), - "rev" => Some(Box::new(Rev::default())), - "rm" => Some(Box::new(Rm::default())), - "rmdir" => Some(Box::new(Rmdir::default())), - "shitbox" => Some(Box::new(Shitbox::default())), - "sleep" => Some(Box::new(Sleep::default())), - "sync" => Some(Box::new(SyncCmd::default())), - "true" => Some(Box::new(True::default())), - "unlink" => Some(Box::new(Unlink::default())), - "which" => Some(Box::new(Which::default())), - "whoami" => Some(Box::new(Whoami::default())), - "yes" => Some(Box::new(Yes::default())), + "clear" => Some(Box::new(clear::Clear::default())), + "cut" => Some(Box::new(cut::Cut::default())), + "dirname" => Some(Box::new(dirname::Dirname::default())), + "echo" => Some(Box::new(echo::Echo::default())), + "factor" => Some(Box::new(factor::Factor::default())), + "false" => Some(Box::new(r#false::False::default())), + "fold" => Some(Box::new(fold::Fold::default())), + "groups" => Some(Box::new(groups::Groups::default())), + "head" => Some(Box::new(head::Head::default())), + "link" => Some(Box::new(link::Link::default())), + "mountpoint" => Some(Box::new(mountpoint::Mountpoint::default())), + "nologin" => Some(Box::new(nologin::Nologin::default())), + "nproc" => Some(Box::new(nproc::Nproc::default())), + "pwd" => Some(Box::new(pwd::Pwd::default())), + "rev" => Some(Box::new(rev::Rev::default())), + "rm" => Some(Box::new(rm::Rm::default())), + "rmdir" => Some(Box::new(rmdir::Rmdir::default())), + "shitbox" => Some(Box::new(shitbox::Shitbox::default())), + "sleep" => Some(Box::new(sleep::Sleep::default())), + "sync" => Some(Box::new(sync::Sync::default())), + "true" => Some(Box::new(r#true::True::default())), + "unlink" => Some(Box::new(unlink::Unlink::default())), + "which" => Some(Box::new(which::Which::default())), + "whoami" => Some(Box::new(whoami::Whoami::default())), + "yes" => Some(Box::new(yes::Yes::default())), _ => None, } } -pub static COMMANDS: [&str; 31] = [ +pub static COMMANDS: [&str; 32] = [ "base32", "base64", "basename", @@ -126,6 +117,7 @@ pub static COMMANDS: [&str; 31] = [ "mountpoint", "nologin", "nproc", + "pwd", "rev", "rm", "rmdir", diff --git a/src/cmd/pwd/mod.rs b/src/cmd/pwd/mod.rs index 8b13789..5705d32 100644 --- a/src/cmd/pwd/mod.rs +++ b/src/cmd/pwd/mod.rs @@ -1 +1,47 @@ +use super::Cmd; +use clap::{Arg, ArgAction, Command}; +use std::{env, io, path::PathBuf}; +#[derive(Debug, Default)] +pub struct Pwd; + +impl Cmd for Pwd { + fn cli(&self) -> clap::Command { + Command::new("pwd") + .about("return working directory name") + .author("Nathan Fisher") + .version(env!("CARGO_PKG_VERSION")) + .args([ + Arg::new("logical") + .short('L') + .long("logical") + .help("use PWD from environment, even if it contains symlinks") + .action(ArgAction::SetTrue), + Arg::new("physical") + .short('P') + .long("physical") + .help("avoid all symlinks") + .conflicts_with("logical") + .action(ArgAction::SetTrue), + ]) + } + + fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { + let Some(matches) = matches else { + return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); + }; + let cwd = if matches.get_flag("logical") { + PathBuf::from(env::var("PWD")?) + } else if matches.get_flag("physical") { + env::current_dir()?.canonicalize()? + } else { + env::current_dir()? + }; + println!("{}", cwd.display()); + Ok(()) + } + + fn path(&self) -> Option { + Some(crate::Path::UsrBin) + } +}