From e2b7f6643592e383491e98cd4b1b4684bcd6302b Mon Sep 17 00:00:00 2001 From: teesh3rt Date: Thu, 20 Mar 2025 17:07:50 +0200 Subject: [PATCH] feat: add a very basic command registry --- Cargo.lock | 15 +++++++++++++++ Cargo.toml | 7 +++++++ coreutils/Cargo.toml | 7 +++++++ coreutils/src/commands/hello.rs | 9 +++++++++ coreutils/src/commands/mod.rs | 3 +++ coreutils/src/lib.rs | 1 + src/boxcmd.rs | 18 ++++++++++++++++++ src/main.rs | 20 ++++++++++++++++++-- src/registry.rs | 11 +++++++++++ utils/Cargo.toml | 6 ++++++ utils/src/commands.rs | 3 +++ utils/src/lib.rs | 2 ++ utils/src/registry.rs | 30 ++++++++++++++++++++++++++++++ 13 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 coreutils/Cargo.toml create mode 100644 coreutils/src/commands/hello.rs create mode 100644 coreutils/src/commands/mod.rs create mode 100644 coreutils/src/lib.rs create mode 100644 src/boxcmd.rs create mode 100644 src/registry.rs create mode 100644 utils/Cargo.toml create mode 100644 utils/src/commands.rs create mode 100644 utils/src/lib.rs create mode 100644 utils/src/registry.rs diff --git a/Cargo.lock b/Cargo.lock index 59b38cc..b4bbf77 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "boxutils" +version = "0.1.0" + +[[package]] +name = "coreutils" +version = "0.1.0" +dependencies = [ + "boxutils", +] + [[package]] name = "rebox" version = "0.1.0" +dependencies = [ + "boxutils", + "coreutils", +] diff --git a/Cargo.toml b/Cargo.toml index d73f317..95d6c0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,10 @@ version = "0.1.0" edition = "2024" [dependencies] +boxutils = { path = "./utils" } +coreutils = { path = "./coreutils" } + + +[[bin]] +name = "box" +path = "./src/main.rs" diff --git a/coreutils/Cargo.toml b/coreutils/Cargo.toml new file mode 100644 index 0000000..5c88adc --- /dev/null +++ b/coreutils/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "coreutils" +version = "0.1.0" +edition = "2024" + +[dependencies] +boxutils = { path = "../utils" } diff --git a/coreutils/src/commands/hello.rs b/coreutils/src/commands/hello.rs new file mode 100644 index 0000000..e6763a5 --- /dev/null +++ b/coreutils/src/commands/hello.rs @@ -0,0 +1,9 @@ +use boxutils::commands::Command; + +pub struct Hello; + +impl Command for Hello { + fn execute(&self) { + println!("Hello, world!"); + } +} diff --git a/coreutils/src/commands/mod.rs b/coreutils/src/commands/mod.rs new file mode 100644 index 0000000..86bbeff --- /dev/null +++ b/coreutils/src/commands/mod.rs @@ -0,0 +1,3 @@ +mod hello; + +pub use hello::Hello; \ No newline at end of file diff --git a/coreutils/src/lib.rs b/coreutils/src/lib.rs new file mode 100644 index 0000000..6be336e --- /dev/null +++ b/coreutils/src/lib.rs @@ -0,0 +1 @@ +pub mod commands; \ No newline at end of file diff --git a/src/boxcmd.rs b/src/boxcmd.rs new file mode 100644 index 0000000..1372760 --- /dev/null +++ b/src/boxcmd.rs @@ -0,0 +1,18 @@ +use super::registry::get_registry; +use boxutils::commands::Command; +use std::env; + +pub struct Boxcmd; + +impl Command for Boxcmd { + fn execute(&self) { + let args: Vec = env::args().collect(); + let registry = get_registry(); + let command = &args.get(1); + if let Some(cmd) = command { + registry.execute(cmd); + return; + } + println!("No command provided."); + } +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..7adb407 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,19 @@ -fn main() { - println!("Hello, world!"); +mod boxcmd; +mod registry; + +use std::env; +use std::path::Path; + +fn being_called_as() -> String { + let args = env::args().collect::>(); + let exe_path = args[0].clone(); + let path = Path::new(&exe_path); + let being_called = path.file_name().unwrap().to_str().unwrap().to_string(); + let formatted = being_called.replace(".exe", ""); + formatted +} + +fn main() { + let utility_name = being_called_as(); + registry::get_registry().execute(&utility_name); } diff --git a/src/registry.rs b/src/registry.rs new file mode 100644 index 0000000..50f5f75 --- /dev/null +++ b/src/registry.rs @@ -0,0 +1,11 @@ +use boxutils::registry::CommandRegistry; +use super::boxcmd::Boxcmd; + +pub fn get_registry() -> CommandRegistry { + let mut registry = CommandRegistry::new(); + + registry.register("hello", Box::new(coreutils::commands::Hello)); + registry.register("box", Box::new(Boxcmd)); + + registry +} diff --git a/utils/Cargo.toml b/utils/Cargo.toml new file mode 100644 index 0000000..c6d1cb8 --- /dev/null +++ b/utils/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "boxutils" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/utils/src/commands.rs b/utils/src/commands.rs new file mode 100644 index 0000000..a8547d1 --- /dev/null +++ b/utils/src/commands.rs @@ -0,0 +1,3 @@ +pub trait Command { + fn execute(&self); +} diff --git a/utils/src/lib.rs b/utils/src/lib.rs new file mode 100644 index 0000000..c09ea16 --- /dev/null +++ b/utils/src/lib.rs @@ -0,0 +1,2 @@ +pub mod commands; +pub mod registry; diff --git a/utils/src/registry.rs b/utils/src/registry.rs new file mode 100644 index 0000000..05a7f66 --- /dev/null +++ b/utils/src/registry.rs @@ -0,0 +1,30 @@ +use super::commands::Command; +use std::collections::HashMap; + +pub struct CommandRegistry { + commands: HashMap>, +} + +impl CommandRegistry { + pub fn new() -> Self { + CommandRegistry { + commands: HashMap::new(), + } + } + + pub fn register(&mut self, name: &str, command: Box) { + self.commands.insert(name.to_string(), command); + } + + pub fn get(&self, name: &str) -> Option<&Box> { + self.commands.get(name) + } + + pub fn execute(&self, name: &str) { + if let Some(command) = self.get(name) { + command.execute(); + } else { + println!("Command not found: {}", name); + } + } +}