diff --git a/CHANGELOG.md b/CHANGELOG.md index 03bf703..3d04540 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,9 @@ ### v0.2.2 -* New is_directory command -* New is_file command +* New is_path_exists command. +* New is_directory command. +* New is_file command. ### v0.2.1 (2020-02-21) diff --git a/docs/sdk.md b/docs/sdk.md index 8f96e64..6eb05a7 100644 --- a/docs/sdk.md +++ b/docs/sdk.md @@ -57,6 +57,7 @@ * [std::fs::CreateEmptyFile (touch)](#std__fs__CreateEmptyFile) * [std::fs::DeleteEmptyDirectory (rmdir)](#std__fs__DeleteEmptyDirectory) * [std::fs::DeletePath (rm)](#std__fs__DeletePath) +* [std::fs::Exists (is_path_exists)](#std__fs__Exists) * [std::fs::GetCanonicalPath (canonicalize)](#std__fs__GetCanonicalPath) * [std::fs::GetFileName (basename)](#std__fs__GetFileName) * [std::fs::GetParentDirectory (dirname)](#std__fs__GetParentDirectory) @@ -2231,6 +2232,33 @@ deleted = rm -r ./target #### Aliases: rm + +## std::fs::Exists +```sh +var = is_path_exists path +``` + +This command will return true/false based if the provided path points to an existing file system entry. + +#### Parameters + +The path to check. + +#### Return Value + +True if the path points to an existing file system entry. + +#### Examples + +```sh +existing = is_path_exists ./dir +existing = is_path_exists ./dir/somefile.txt +``` + + +#### Aliases: +is_path_exists + ## std::fs::GetCanonicalPath ```sh diff --git a/duckscript_sdk/src/sdk/std/fs/exists/help.md b/duckscript_sdk/src/sdk/std/fs/exists/help.md new file mode 100644 index 0000000..0bf7515 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/fs/exists/help.md @@ -0,0 +1,20 @@ +```sh +var = is_path_exists path +``` + +This command will return true/false based if the provided path points to an existing file system entry. + +#### Parameters + +The path to check. + +#### Return Value + +True if the path points to an existing file system entry. + +#### Examples + +```sh +existing = is_path_exists ./dir +existing = is_path_exists ./dir/somefile.txt +``` diff --git a/duckscript_sdk/src/sdk/std/fs/exists/mod.rs b/duckscript_sdk/src/sdk/std/fs/exists/mod.rs new file mode 100755 index 0000000..d226b9a --- /dev/null +++ b/duckscript_sdk/src/sdk/std/fs/exists/mod.rs @@ -0,0 +1,45 @@ +use crate::utils::pckg; +use duckscript::types::command::{Command, CommandResult}; +use fsio::path::as_path::AsPath; + +#[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, "Exists") + } + + fn aliases(&self) -> Vec { + vec!["is_path_exists".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.is_empty() { + CommandResult::Error("Path not provided.".to_string()) + } else { + let path = &arguments[0].as_path(); + CommandResult::Continue(Some(path.exists().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/exists/mod_test.rs b/duckscript_sdk/src/sdk/std/fs/exists/mod_test.rs new file mode 100644 index 0000000..512b2ea --- /dev/null +++ b/duckscript_sdk/src/sdk/std/fs/exists/mod_test.rs @@ -0,0 +1,40 @@ +use super::*; +use crate::test; +use crate::test::CommandValidation; + +#[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_exists", "out"); +} + +#[test] +fn run_file() { + test::run_script_and_validate( + vec![create("")], + "out = is_path_exists ./Cargo.toml", + CommandValidation::Match("out".to_string(), "true".to_string()), + ); +} + +#[test] +fn run_directory() { + test::run_script_and_validate( + vec![create("")], + "out = is_path_exists ./src", + CommandValidation::Match("out".to_string(), "true".to_string()), + ); +} + +#[test] +fn run_not_found() { + test::run_script_and_validate( + vec![create("")], + "out = is_path_exists ./badpath", + CommandValidation::Match("out".to_string(), "false".to_string()), + ); +} diff --git a/duckscript_sdk/src/sdk/std/fs/mod.rs b/duckscript_sdk/src/sdk/std/fs/mod.rs index af5f143..ba2e6be 100755 --- a/duckscript_sdk/src/sdk/std/fs/mod.rs +++ b/duckscript_sdk/src/sdk/std/fs/mod.rs @@ -3,6 +3,7 @@ mod basename; mod canonical; mod cp; mod dirname; +mod exists; mod is_directory; mod is_file; mod list; @@ -32,6 +33,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr commands.set(canonical::create(&package))?; commands.set(cp::create(&package))?; commands.set(dirname::create(&package))?; + commands.set(exists::create(&package))?; commands.set(is_directory::create(&package))?; commands.set(is_file::create(&package))?; commands.set(list::create(&package))?; diff --git a/test/std/fs/exists_test.ds b/test/std/fs/exists_test.ds new file mode 100644 index 0000000..3a21ad8 --- /dev/null +++ b/test/std/fs/exists_test.ds @@ -0,0 +1,18 @@ + +fn test_file + value = is_path_exists ./Cargo.toml + + assert ${value} +end + +fn test_directory + value = is_path_exists ./target + + assert ${value} +end + +fn test_not_found + value = is_path_exists ./badpath + + assert_false ${value} +end