New is_path_newer command

This commit is contained in:
sagie gur ari 2020-07-07 22:24:11 +00:00
parent 093ddd5972
commit 0f63612f73
9 changed files with 132 additions and 15 deletions

View file

@ -2,6 +2,7 @@
### v0.6.1
* New is_path_newer command.
* New get_last_modified_time command.
### v0.6.0 (2020-07-03)

View file

@ -28,7 +28,7 @@ base64 = "^0.12"
cfg-if = "^0.1"
duckscript = { version = "^0.5.0", path = "../duckscript" }
fs_extra = "^1"
fsio = { version = "^0.1", features = ["temp-path"] }
fsio = { version = "^0.1.3", features = ["temp-path"] }
ftp = "^3"
glob = "^0.3"
home = "^0.5"

View file

@ -1,7 +1,5 @@
use crate::utils::pckg;
use crate::utils::{io, pckg};
use duckscript::types::command::{Command, CommandResult};
use std::fs;
use std::time::SystemTime;
#[cfg(test)]
#[path = "./mod_test.rs"]
@ -33,17 +31,9 @@ impl Command for CommandImpl {
if arguments.is_empty() {
CommandResult::Error("Path not provided.".to_string())
} else {
match fs::metadata(&arguments[0]) {
Ok(metadata) => match metadata.modified() {
Ok(time) => match time.duration_since(SystemTime::UNIX_EPOCH) {
Ok(duration) => {
CommandResult::Continue(Some(duration.as_millis().to_string()))
}
Err(error) => CommandResult::Error(error.to_string()),
},
Err(error) => CommandResult::Error(error.to_string()),
},
Err(error) => CommandResult::Error(error.to_string()),
match io::get_last_modified_time(&arguments[0]) {
Ok(time) => CommandResult::Continue(Some(time.to_string())),
Err(error) => CommandResult::Error(error),
}
}
}

View file

@ -0,0 +1,21 @@
```sh
var = is_path_newer newer older
```
This command will return true if the 'newer' path last modified time is after the 'older' path last modified time.
#### Parameters
* newer - The file/directory path to check.
* older - The file/directory path to check.
#### Return Value
True if the 'newer' path last modified time is after the 'older' path last modified time.
Otherwise or in case of an error, false will be returned.
#### Examples
```sh
newer = is_path_newer ./new_file.txt ./old_file.txt
```

View file

@ -0,0 +1,54 @@
use crate::utils::{io, pckg};
use duckscript::types::command::{Command, CommandResult};
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
#[derive(Clone)]
pub(crate) struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "IsPathNewer")
}
fn aliases(&self) -> Vec<String> {
vec!["is_path_newer".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
fn clone_and_box(&self) -> Box<dyn Command> {
Box::new((*self).clone())
}
fn run(&self, arguments: Vec<String>) -> CommandResult {
if arguments.len() < 2 {
CommandResult::Error("Paths not provided.".to_string())
} else {
let newer = match io::get_last_modified_time(&arguments[0]) {
Ok(time) => time,
Err(error) => return CommandResult::Error(error),
};
let older = match io::get_last_modified_time(&arguments[1]) {
Ok(time) => time,
Err(error) => return CommandResult::Error(error),
};
let result = if newer > older { true } else { false };
CommandResult::Continue(Some(result.to_string()))
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,21 @@
use super::*;
use crate::test;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_path_provided() {
test::run_script_and_error(vec![create("")], "out = is_path_newer", "out");
}
#[test]
fn run_not_found() {
test::run_script_and_error(
vec![create("")],
"out = is_path_newer ./badpath ./Cargo.toml",
"out",
);
}

View file

@ -8,6 +8,7 @@ mod get_last_modified_time;
mod glob_array;
mod is_directory;
mod is_file;
mod is_path_newer;
mod is_readonly;
mod list;
mod mkdir;
@ -44,6 +45,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(glob_array::create(&package))?;
commands.set(is_directory::create(&package))?;
commands.set(is_file::create(&package))?;
commands.set(is_path_newer::create(&package))?;
commands.set(is_readonly::create(&package))?;
commands.set(list::create(&package))?;
commands.set(mkdir::create(&package))?;

View file

@ -1,4 +1,5 @@
use duckscript::types::error::{ErrorInfo, ScriptError};
use fsio;
use fsio::file::{append_file, ensure_exists, read_file, write_file};
#[cfg(test)]
@ -52,3 +53,10 @@ pub(crate) fn create_empty_file(file: &str) -> Result<(), String> {
Err(error) => Err(error.to_string()),
}
}
pub(crate) fn get_last_modified_time(path: &str) -> Result<u128, String> {
match fsio::path::get_last_modified_time(path) {
Ok(time) => Ok(time),
Err(error) => Err(error.to_string()),
}
}

View file

@ -0,0 +1,20 @@
fn test_valid
dir = set ./target/_duckscript_test/fs/is_path_newer/
older = set "${dir}/older.txt"
newer = set "${dir}/newer.txt"
touch ${older}
sleep 10
touch ${newer}
result = is_path_newer ${older} ${newer}
assert_false ${result}
result = is_path_newer ${newer} ${older}
assert ${result}
result = is_path_newer ${older} ${older}
assert_false ${result}
end