New current_time command

This commit is contained in:
sagie gur ari 2020-01-18 17:10:44 +00:00
parent 2a3bfb2f99
commit 3884b5c144
10 changed files with 138 additions and 3 deletions

View file

@ -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.

View file

@ -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)
<a name="std__Alias"></a>
@ -2476,6 +2477,33 @@ echo Waited for ${time} milliseconds.
#### Aliases:
sleep
<a name="std__time__CurrentTimeMillies"></a>
## 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.

View file

@ -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(())
}

View file

@ -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);

View file

@ -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}
```

View file

@ -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<String> {
vec!["current_time".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
fn run(&self, _arguments: Vec<String>) -> 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<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -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()),
);
}

View file

@ -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(())
}

View file

@ -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<String>),
@ -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

View file

@ -0,0 +1,8 @@
function test_current_time
value = current_time
result = greater_than ${value} 0
assert ${result}
end