From 3884b5c144bac1855c53b1196ff0c32cc4b1668b Mon Sep 17 00:00:00 2001 From: sagie gur ari Date: Sat, 18 Jan 2020 17:10:44 +0000 Subject: [PATCH] New current_time command --- CHANGELOG.md | 1 + docs/sdk.md | 28 ++++++++++++++ duckscript_sdk/src/sdk/std/mod.rs | 2 + .../src/sdk/std/net/http_client/mod_test.rs | 2 +- .../src/sdk/std/time/current_time/help.md | 20 ++++++++++ .../src/sdk/std/time/current_time/mod.rs | 38 +++++++++++++++++++ .../src/sdk/std/time/current_time/mod_test.rs | 17 +++++++++ duckscript_sdk/src/sdk/std/time/mod.rs | 15 ++++++++ duckscript_sdk/src/test/mod.rs | 10 ++++- test/std/time/current_time_test.ds | 8 ++++ 10 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 duckscript_sdk/src/sdk/std/time/current_time/help.md create mode 100755 duckscript_sdk/src/sdk/std/time/current_time/mod.rs create mode 100644 duckscript_sdk/src/sdk/std/time/current_time/mod_test.rs create mode 100755 duckscript_sdk/src/sdk/std/time/mod.rs create mode 100644 test/std/time/current_time_test.ds diff --git a/CHANGELOG.md b/CHANGELOG.md index 6aad2d6..8853751 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### v0.1.8 +* New current_time command. * New greater_than and less_than commands. * New wget (http_client) command #20 * Reduce binary executable size. diff --git a/docs/sdk.md b/docs/sdk.md index f338b7a..d7cc7ac 100644 --- a/docs/sdk.md +++ b/docs/sdk.md @@ -70,6 +70,7 @@ * [std::test::AssertFail (assert_fail)](#std__test__AssertFail) * [std::test::AssertFalse (assert_false)](#std__test__AssertFalse) * [std::thread::Sleep (sleep)](#std__thread__Sleep) +* [std::time::CurrentTimeMillies (current_time)](#std__time__CurrentTimeMillies) @@ -2476,6 +2477,33 @@ echo Waited for ${time} milliseconds. #### Aliases: sleep + +## std::time::CurrentTimeMillies +```sh +var = current_time +``` + +Returns the current time in milliseconds (from January 1, 1970 UTC). + +#### Parameters + +None + +#### Return Value + +The current time in milliseconds. + +#### Examples + +```sh +result = current_time +echo ${result} +``` + + +#### Aliases: +current_time + ### License Developed by Sagie Gur-Ari and licensed under the [Apache 2](https://github.com/sagiegurari/duckscript/blob/master/LICENSE) open source license. diff --git a/duckscript_sdk/src/sdk/std/mod.rs b/duckscript_sdk/src/sdk/std/mod.rs index d6c3c24..a151081 100755 --- a/duckscript_sdk/src/sdk/std/mod.rs +++ b/duckscript_sdk/src/sdk/std/mod.rs @@ -21,6 +21,7 @@ mod set; pub(crate) mod string; mod test; mod thread; +mod time; mod unalias; use duckscript::types::command::Commands; @@ -54,6 +55,7 @@ pub(crate) fn load(commands: &mut Commands) -> Result<(), ScriptError> { string::load(commands, PACKAGE)?; test::load(commands, PACKAGE)?; thread::load(commands, PACKAGE)?; + time::load(commands, PACKAGE)?; Ok(()) } diff --git a/duckscript_sdk/src/sdk/std/net/http_client/mod_test.rs b/duckscript_sdk/src/sdk/std/net/http_client/mod_test.rs index 0be98bb..3444721 100644 --- a/duckscript_sdk/src/sdk/std/net/http_client/mod_test.rs +++ b/duckscript_sdk/src/sdk/std/net/http_client/mod_test.rs @@ -30,7 +30,7 @@ fn run_get_to_file() { test::run_script_and_validate( vec![create("")], "out = http_client -O ./target/_duckscript/http_client/page.html https://www.rust-lang.org/", - CommandValidation::Ignore + CommandValidation::PositiveNumber("out".to_string()) ); let read_result = io::read_text_file(file); diff --git a/duckscript_sdk/src/sdk/std/time/current_time/help.md b/duckscript_sdk/src/sdk/std/time/current_time/help.md new file mode 100644 index 0000000..3c84d2a --- /dev/null +++ b/duckscript_sdk/src/sdk/std/time/current_time/help.md @@ -0,0 +1,20 @@ +```sh +var = current_time +``` + +Returns the current time in milliseconds (from January 1, 1970 UTC). + +#### Parameters + +None + +#### Return Value + +The current time in milliseconds. + +#### Examples + +```sh +result = current_time +echo ${result} +``` diff --git a/duckscript_sdk/src/sdk/std/time/current_time/mod.rs b/duckscript_sdk/src/sdk/std/time/current_time/mod.rs new file mode 100755 index 0000000..8fb9596 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/time/current_time/mod.rs @@ -0,0 +1,38 @@ +use crate::utils::pckg; +use duckscript::types::command::{Command, CommandResult}; +use std::time::{SystemTime, UNIX_EPOCH}; + +#[cfg(test)] +#[path = "./mod_test.rs"] +mod mod_test; + +struct CommandImpl { + package: String, +} + +impl Command for CommandImpl { + fn name(&self) -> String { + pckg::concat(&self.package, "CurrentTimeMillies") + } + + fn aliases(&self) -> Vec { + vec!["current_time".to_string()] + } + + fn help(&self) -> String { + include_str!("help.md").to_string() + } + + fn run(&self, _arguments: Vec) -> CommandResult { + match SystemTime::now().duration_since(UNIX_EPOCH) { + Ok(value) => CommandResult::Continue(Some(value.as_millis().to_string())), + Err(error) => CommandResult::Error(error.to_string()), + } + } +} + +pub(crate) fn create(package: &str) -> Box { + Box::new(CommandImpl { + package: package.to_string(), + }) +} diff --git a/duckscript_sdk/src/sdk/std/time/current_time/mod_test.rs b/duckscript_sdk/src/sdk/std/time/current_time/mod_test.rs new file mode 100644 index 0000000..0b24cb0 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/time/current_time/mod_test.rs @@ -0,0 +1,17 @@ +use super::*; +use crate::test; +use crate::test::CommandValidation; + +#[test] +fn common_functions() { + test::test_common_command_functions(create("")); +} + +#[test] +fn run_no_args() { + test::run_script_and_validate( + vec![create("")], + "out = current_time", + CommandValidation::PositiveNumber("out".to_string()), + ); +} diff --git a/duckscript_sdk/src/sdk/std/time/mod.rs b/duckscript_sdk/src/sdk/std/time/mod.rs new file mode 100755 index 0000000..4e1c7ee --- /dev/null +++ b/duckscript_sdk/src/sdk/std/time/mod.rs @@ -0,0 +1,15 @@ +mod current_time; + +use crate::utils::pckg; +use duckscript::types::command::Commands; +use duckscript::types::error::ScriptError; + +static PACKAGE: &str = "time"; + +pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptError> { + let package = pckg::concat(parent, PACKAGE); + + commands.set(current_time::create(&package))?; + + Ok(()) +} diff --git a/duckscript_sdk/src/test/mod.rs b/duckscript_sdk/src/test/mod.rs index 7b12b18..673f501 100644 --- a/duckscript_sdk/src/test/mod.rs +++ b/duckscript_sdk/src/test/mod.rs @@ -139,7 +139,7 @@ impl Command for OnErrorCommand { pub(crate) enum CommandValidation { None, - Ignore, + PositiveNumber(String), Match(String, String), Contains(String, String), Any(String, Vec), @@ -237,7 +237,13 @@ pub(crate) fn run_script_and_validate( &values ) } - CommandValidation::Ignore => (), + CommandValidation::PositiveNumber(key) => { + assert!(!context.variables.is_empty()); + + let var_value = context.variables.get(&key).unwrap(); + let numeric_value: u128 = var_value.parse().unwrap(); + assert!(numeric_value > 0) + } }; context diff --git a/test/std/time/current_time_test.ds b/test/std/time/current_time_test.ds new file mode 100644 index 0000000..85bcb12 --- /dev/null +++ b/test/std/time/current_time_test.ds @@ -0,0 +1,8 @@ + +function test_current_time + value = current_time + + result = greater_than ${value} 0 + + assert ${result} +end