From 84c2afb614301257d7cc998090f134c5a496eeaa Mon Sep 17 00:00:00 2001 From: prsabahrami Date: Sun, 22 Sep 2024 13:03:14 -0400 Subject: [PATCH] Add date support --- Cargo.lock | 80 +++++++++++++++++++++++++++++++ crates/shell/Cargo.toml | 3 +- crates/shell/src/commands/date.rs | 31 ++++++++++++ crates/shell/src/commands/mod.rs | 6 +++ crates/tests/src/lib.rs | 17 +++++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 crates/shell/src/commands/date.rs diff --git a/Cargo.lock b/Cargo.lock index 8a68334..1ee73ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "android-tzdata" version = "0.1.1" @@ -695,6 +704,12 @@ dependencies = [ "syn", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.8.0" @@ -737,6 +752,16 @@ dependencies = [ "libc", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "nu-ansi-term" version = "0.49.0" @@ -830,6 +855,17 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "parse_datetime" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8720474e3dd4af20cea8716703498b9f3b690f318fa9d9d9e2e38eaf44b96d0" +dependencies = [ + "chrono", + "nom", + "regex", +] + [[package]] name = "path-dedot" version = "3.1.1" @@ -988,6 +1024,35 @@ dependencies = [ "thiserror", ] +[[package]] +name = "regex" +version = "1.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -1107,6 +1172,7 @@ dependencies = [ "futures", "rustyline", "tokio", + "uu_date", "uu_ls", "uu_uname", "which", @@ -1347,6 +1413,20 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uu_date" +version = "0.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09b64040b532d67a399c8670da5c5ba12bf38ee8b9d845330580f3ba7b1eabc7" +dependencies = [ + "chrono", + "clap", + "libc", + "parse_datetime", + "uucore", + "windows-sys 0.48.0", +] + [[package]] name = "uu_ls" version = "0.0.27" diff --git a/crates/shell/Cargo.toml b/crates/shell/Cargo.toml index 78797fe..e134ed6 100644 --- a/crates/shell/Cargo.toml +++ b/crates/shell/Cargo.toml @@ -33,7 +33,8 @@ uu_ls = "0.0.27" dirs = "5.0.1" which = "6.0.3" uu_uname = "0.0.27" +uu_date = "0.0.27" [package.metadata.release] # Dont publish the binary -release = false \ No newline at end of file +release = false diff --git a/crates/shell/src/commands/date.rs b/crates/shell/src/commands/date.rs new file mode 100644 index 0000000..3086548 --- /dev/null +++ b/crates/shell/src/commands/date.rs @@ -0,0 +1,31 @@ +use std::ffi::OsString; + +use deno_task_shell::{ExecuteResult, ShellCommand, ShellCommandContext}; +use futures::future::LocalBoxFuture; +use uu_date::uumain as uu_date; + +pub struct DateCommand; + +impl ShellCommand for DateCommand { + fn execute(&self, mut context: ShellCommandContext) -> LocalBoxFuture<'static, ExecuteResult> { + Box::pin(futures::future::ready(match execute_date(&mut context) { + Ok(_) => ExecuteResult::from_exit_code(0), + Err(exit_code) => ExecuteResult::from_exit_code(exit_code), + })) + } +} + +fn execute_date(context: &mut ShellCommandContext) -> Result<(), i32> { + let mut args: Vec = vec![OsString::from("date")]; + + context + .args + .iter() + .for_each(|arg| args.push(OsString::from(arg))); + + let exit_code = uu_date(args.into_iter()); + if exit_code != 0 { + return Err(exit_code); + } + Ok(()) +} diff --git a/crates/shell/src/commands/mod.rs b/crates/shell/src/commands/mod.rs index 810485b..f8dff0a 100644 --- a/crates/shell/src/commands/mod.rs +++ b/crates/shell/src/commands/mod.rs @@ -7,9 +7,11 @@ use uu_ls::uumain as uu_ls; use crate::execute; +pub mod date; pub mod uname; pub mod which; +pub use date::DateCommand; pub use uname::UnameCommand; pub use which::WhichCommand; @@ -44,6 +46,10 @@ pub fn get_commands() -> HashMap> { "uname".to_string(), Rc::new(UnameCommand) as Rc, ), + ( + "date".to_string(), + Rc::new(DateCommand) as Rc, + ), ]) } diff --git a/crates/tests/src/lib.rs b/crates/tests/src/lib.rs index 7eb8367..eb6e23e 100644 --- a/crates/tests/src/lib.rs +++ b/crates/tests/src/lib.rs @@ -870,6 +870,23 @@ async fn arithmetic() { .await; } +#[tokio::test] +async fn date() { + TestBuilder::new() + .command("date") + .assert_exit_code(0) + .check_stdout(false) + .run() + .await; + + TestBuilder::new() + .command("date +%Y-%m-%d") + .assert_exit_code(0) + .check_stdout(false) + .run() + .await; +} + #[cfg(test)] fn no_such_file_error_text() -> &'static str { if cfg!(windows) {