From 11a3911f28ebbaa7f1131cb4eac9a605f43dc6d4 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 18 Feb 2024 17:04:01 -0600 Subject: [PATCH] adding a builtin rm --- README.md | 26 ++++++++++++++++++++++++++ src/builtin.rs | 6 ++++++ src/builtin/rm.rs | 25 +++++++++++++++++++++++++ src/error.rs | 2 +- 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/builtin/rm.rs diff --git a/README.md b/README.md index 2978732..47a9847 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,29 @@ # clyde A command-line shell for Windows. + +## examples + +In all examples, the Clyde prompt is written as follows: + +``` +▷ +``` + +1. Run a built-in command: + +Built-in commands are invoked by executing them with a bare string + +``` +c:\dev\clyde ▷ cd src +c:\dev\clyde\src ▷ +``` + +2. Run an external command: + +External commands are invoked in the same way: + +``` +c:\one ▷ cd two +c:\one\two ▷ +``` diff --git a/src/builtin.rs b/src/builtin.rs index 8b5437e..16e3039 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -7,6 +7,9 @@ mod echo; /// prints environment variables mod printenv; +/// removes files +mod rm; + /// the butt of a file mod tail; @@ -25,6 +28,7 @@ pub enum Builtin { Changedir, Echo, Printenv, + Rm, Tail, Which, } @@ -36,6 +40,7 @@ impl Builtin { Changedir => &cd::Changedir, Echo => &echo::Echo, Printenv => &printenv::Printenv, + Rm => &rm::Rm, Tail => &tail::Tail, Which => &which::Which, } @@ -55,6 +60,7 @@ pub fn all() -> HashMap<&'static str, Builtin> { ("cd", Changedir), ("echo", Echo), ("printenv", Printenv), + ("rm", Rm), ("tail", Tail), ("which", Which), ]) diff --git a/src/builtin/rm.rs b/src/builtin/rm.rs new file mode 100644 index 0000000..e5403da --- /dev/null +++ b/src/builtin/rm.rs @@ -0,0 +1,25 @@ +use crate::{ + error::ExecError, + run::{Call, Context, Value}, +}; +use std::fs; + +pub struct Rm; + +impl Call for Rm { + fn call(&self, _ctx: &mut Context, args: &[Value]) -> Result { + match args.len() { + 0 => { + todo!() + } + 1 => { + let path = args[0].try_as_str()?; + fs::remove_file(path).map_err(|err| ExecError::Misc(err.to_string()))?; + } + _ => { + todo!() + } + } + Ok(Value::None) + } +} diff --git a/src/error.rs b/src/error.rs index 5c1d8f0..18d1e00 100644 --- a/src/error.rs +++ b/src/error.rs @@ -112,7 +112,7 @@ pub enum ExecError { #[error("value type error: {0}")] ValueTypeError(String), - #[error("error")] + #[error("error: {0}")] Misc(String), }