diff --git a/CHANGELOG.md b/CHANGELOG.md index 3be2e13..0b1f6ac 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### v0.6.1 +* New is_path_newer command. * New get_last_modified_time command. ### v0.6.0 (2020-07-03) diff --git a/duckscript_sdk/Cargo.toml b/duckscript_sdk/Cargo.toml index cfd3f14..127ee85 100644 --- a/duckscript_sdk/Cargo.toml +++ b/duckscript_sdk/Cargo.toml @@ -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" diff --git a/duckscript_sdk/src/sdk/std/fs/get_last_modified_time/mod.rs b/duckscript_sdk/src/sdk/std/fs/get_last_modified_time/mod.rs index 26bbd94..ab2c98f 100755 --- a/duckscript_sdk/src/sdk/std/fs/get_last_modified_time/mod.rs +++ b/duckscript_sdk/src/sdk/std/fs/get_last_modified_time/mod.rs @@ -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), } } } diff --git a/duckscript_sdk/src/sdk/std/fs/is_path_newer/help.md b/duckscript_sdk/src/sdk/std/fs/is_path_newer/help.md new file mode 100644 index 0000000..794774b --- /dev/null +++ b/duckscript_sdk/src/sdk/std/fs/is_path_newer/help.md @@ -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 +``` diff --git a/duckscript_sdk/src/sdk/std/fs/is_path_newer/mod.rs b/duckscript_sdk/src/sdk/std/fs/is_path_newer/mod.rs new file mode 100755 index 0000000..df36685 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/fs/is_path_newer/mod.rs @@ -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 { + vec!["is_path_newer".to_string()] + } + + fn help(&self) -> String { + include_str!("help.md").to_string() + } + + fn clone_and_box(&self) -> Box { + Box::new((*self).clone()) + } + + fn run(&self, arguments: Vec) -> 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 { + Box::new(CommandImpl { + package: package.to_string(), + }) +} diff --git a/duckscript_sdk/src/sdk/std/fs/is_path_newer/mod_test.rs b/duckscript_sdk/src/sdk/std/fs/is_path_newer/mod_test.rs new file mode 100644 index 0000000..7aebf91 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/fs/is_path_newer/mod_test.rs @@ -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", + ); +} diff --git a/duckscript_sdk/src/sdk/std/fs/mod.rs b/duckscript_sdk/src/sdk/std/fs/mod.rs index 714c728..0b28e72 100755 --- a/duckscript_sdk/src/sdk/std/fs/mod.rs +++ b/duckscript_sdk/src/sdk/std/fs/mod.rs @@ -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))?; diff --git a/duckscript_sdk/src/utils/io.rs b/duckscript_sdk/src/utils/io.rs index 76fbf25..59f071f 100644 --- a/duckscript_sdk/src/utils/io.rs +++ b/duckscript_sdk/src/utils/io.rs @@ -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 { + match fsio::path::get_last_modified_time(path) { + Ok(time) => Ok(time), + Err(error) => Err(error.to_string()), + } +} diff --git a/test/std/fs/is_path_newer_test.ds b/test/std/fs/is_path_newer_test.ds new file mode 100644 index 0000000..077f503 --- /dev/null +++ b/test/std/fs/is_path_newer_test.ds @@ -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 +