Skip to content

Commit 3a20581

Browse files
authored
Add date support (#156)
1 parent eeab94c commit 3a20581

File tree

5 files changed

+136
-1
lines changed

5 files changed

+136
-1
lines changed

Cargo.lock

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/shell/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ uu_ls = "0.0.27"
3333
dirs = "5.0.1"
3434
which = "6.0.3"
3535
uu_uname = "0.0.27"
36+
uu_date = "0.0.27"
3637

3738
[package.metadata.release]
3839
# Dont publish the binary
39-
release = false
40+
release = false

crates/shell/src/commands/date.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::ffi::OsString;
2+
3+
use deno_task_shell::{ExecuteResult, ShellCommand, ShellCommandContext};
4+
use futures::future::LocalBoxFuture;
5+
use uu_date::uumain as uu_date;
6+
7+
pub struct DateCommand;
8+
9+
impl ShellCommand for DateCommand {
10+
fn execute(&self, mut context: ShellCommandContext) -> LocalBoxFuture<'static, ExecuteResult> {
11+
Box::pin(futures::future::ready(match execute_date(&mut context) {
12+
Ok(_) => ExecuteResult::from_exit_code(0),
13+
Err(exit_code) => ExecuteResult::from_exit_code(exit_code),
14+
}))
15+
}
16+
}
17+
18+
fn execute_date(context: &mut ShellCommandContext) -> Result<(), i32> {
19+
let mut args: Vec<OsString> = vec![OsString::from("date")];
20+
21+
context
22+
.args
23+
.iter()
24+
.for_each(|arg| args.push(OsString::from(arg)));
25+
26+
let exit_code = uu_date(args.into_iter());
27+
if exit_code != 0 {
28+
return Err(exit_code);
29+
}
30+
Ok(())
31+
}

crates/shell/src/commands/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ use uu_ls::uumain as uu_ls;
77

88
use crate::execute;
99

10+
pub mod date;
1011
pub mod uname;
1112
pub mod which;
1213

14+
pub use date::DateCommand;
1315
pub use uname::UnameCommand;
1416
pub use which::WhichCommand;
1517

@@ -44,6 +46,10 @@ pub fn get_commands() -> HashMap<String, Rc<dyn ShellCommand>> {
4446
"uname".to_string(),
4547
Rc::new(UnameCommand) as Rc<dyn ShellCommand>,
4648
),
49+
(
50+
"date".to_string(),
51+
Rc::new(DateCommand) as Rc<dyn ShellCommand>,
52+
),
4753
])
4854
}
4955

crates/tests/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,23 @@ async fn arithmetic() {
870870
.await;
871871
}
872872

873+
#[tokio::test]
874+
async fn date() {
875+
TestBuilder::new()
876+
.command("date")
877+
.assert_exit_code(0)
878+
.check_stdout(false)
879+
.run()
880+
.await;
881+
882+
TestBuilder::new()
883+
.command("date +%Y-%m-%d")
884+
.assert_exit_code(0)
885+
.check_stdout(false)
886+
.run()
887+
.await;
888+
}
889+
873890
#[cfg(test)]
874891
fn no_such_file_error_text() -> &'static str {
875892
if cfg!(windows) {

0 commit comments

Comments
 (0)