mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-14 11:53:05 +00:00
New is_path_newer command
This commit is contained in:
parent
093ddd5972
commit
0f63612f73
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
### v0.6.1
|
### v0.6.1
|
||||||
|
|
||||||
|
* New is_path_newer command.
|
||||||
* New get_last_modified_time command.
|
* New get_last_modified_time command.
|
||||||
|
|
||||||
### v0.6.0 (2020-07-03)
|
### v0.6.0 (2020-07-03)
|
||||||
|
|
|
@ -28,7 +28,7 @@ base64 = "^0.12"
|
||||||
cfg-if = "^0.1"
|
cfg-if = "^0.1"
|
||||||
duckscript = { version = "^0.5.0", path = "../duckscript" }
|
duckscript = { version = "^0.5.0", path = "../duckscript" }
|
||||||
fs_extra = "^1"
|
fs_extra = "^1"
|
||||||
fsio = { version = "^0.1", features = ["temp-path"] }
|
fsio = { version = "^0.1.3", features = ["temp-path"] }
|
||||||
ftp = "^3"
|
ftp = "^3"
|
||||||
glob = "^0.3"
|
glob = "^0.3"
|
||||||
home = "^0.5"
|
home = "^0.5"
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
use crate::utils::pckg;
|
use crate::utils::{io, pckg};
|
||||||
use duckscript::types::command::{Command, CommandResult};
|
use duckscript::types::command::{Command, CommandResult};
|
||||||
use std::fs;
|
|
||||||
use std::time::SystemTime;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[path = "./mod_test.rs"]
|
#[path = "./mod_test.rs"]
|
||||||
|
@ -33,17 +31,9 @@ impl Command for CommandImpl {
|
||||||
if arguments.is_empty() {
|
if arguments.is_empty() {
|
||||||
CommandResult::Error("Path not provided.".to_string())
|
CommandResult::Error("Path not provided.".to_string())
|
||||||
} else {
|
} else {
|
||||||
match fs::metadata(&arguments[0]) {
|
match io::get_last_modified_time(&arguments[0]) {
|
||||||
Ok(metadata) => match metadata.modified() {
|
Ok(time) => CommandResult::Continue(Some(time.to_string())),
|
||||||
Ok(time) => match time.duration_since(SystemTime::UNIX_EPOCH) {
|
Err(error) => CommandResult::Error(error),
|
||||||
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()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
21
duckscript_sdk/src/sdk/std/fs/is_path_newer/help.md
Normal file
21
duckscript_sdk/src/sdk/std/fs/is_path_newer/help.md
Normal 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
|
||||||
|
```
|
54
duckscript_sdk/src/sdk/std/fs/is_path_newer/mod.rs
Executable file
54
duckscript_sdk/src/sdk/std/fs/is_path_newer/mod.rs
Executable 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(),
|
||||||
|
})
|
||||||
|
}
|
21
duckscript_sdk/src/sdk/std/fs/is_path_newer/mod_test.rs
Normal file
21
duckscript_sdk/src/sdk/std/fs/is_path_newer/mod_test.rs
Normal 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",
|
||||||
|
);
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ mod get_last_modified_time;
|
||||||
mod glob_array;
|
mod glob_array;
|
||||||
mod is_directory;
|
mod is_directory;
|
||||||
mod is_file;
|
mod is_file;
|
||||||
|
mod is_path_newer;
|
||||||
mod is_readonly;
|
mod is_readonly;
|
||||||
mod list;
|
mod list;
|
||||||
mod mkdir;
|
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(glob_array::create(&package))?;
|
||||||
commands.set(is_directory::create(&package))?;
|
commands.set(is_directory::create(&package))?;
|
||||||
commands.set(is_file::create(&package))?;
|
commands.set(is_file::create(&package))?;
|
||||||
|
commands.set(is_path_newer::create(&package))?;
|
||||||
commands.set(is_readonly::create(&package))?;
|
commands.set(is_readonly::create(&package))?;
|
||||||
commands.set(list::create(&package))?;
|
commands.set(list::create(&package))?;
|
||||||
commands.set(mkdir::create(&package))?;
|
commands.set(mkdir::create(&package))?;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use duckscript::types::error::{ErrorInfo, ScriptError};
|
use duckscript::types::error::{ErrorInfo, ScriptError};
|
||||||
|
use fsio;
|
||||||
use fsio::file::{append_file, ensure_exists, read_file, write_file};
|
use fsio::file::{append_file, ensure_exists, read_file, write_file};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -52,3 +53,10 @@ pub(crate) fn create_empty_file(file: &str) -> Result<(), String> {
|
||||||
Err(error) => Err(error.to_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()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
20
test/std/fs/is_path_newer_test.ds
Normal file
20
test/std/fs/is_path_newer_test.ds
Normal 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
|
||||||
|
|
Loading…
Reference in a new issue