From 77e256dbfe39e6518e0da3154916ba326265b30d Mon Sep 17 00:00:00 2001 From: sagie gur ari Date: Fri, 6 Mar 2020 13:20:54 +0000 Subject: [PATCH] New is_readonly command --- CHANGELOG.md | 1 + docs/sdk.md | 27 ++++++++++ .../src/sdk/std/fs/is_readonly/help.md | 19 +++++++ .../src/sdk/std/fs/is_readonly/mod.rs | 49 +++++++++++++++++++ .../src/sdk/std/fs/is_readonly/mod_test.rs | 22 +++++++++ duckscript_sdk/src/sdk/std/fs/mod.rs | 2 + test/std/fs/is_readonly_test.ds | 20 ++++++++ 7 files changed, 140 insertions(+) create mode 100644 duckscript_sdk/src/sdk/std/fs/is_readonly/help.md create mode 100755 duckscript_sdk/src/sdk/std/fs/is_readonly/mod.rs create mode 100644 duckscript_sdk/src/sdk/std/fs/is_readonly/mod_test.rs create mode 100644 test/std/fs/is_readonly_test.ds diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d04540..e5758a2 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### v0.2.2 +* New is_readonly command. * New is_path_exists command. * New is_directory command. * New is_file command. diff --git a/docs/sdk.md b/docs/sdk.md index 6eb05a7..7c4998f 100644 --- a/docs/sdk.md +++ b/docs/sdk.md @@ -63,6 +63,7 @@ * [std::fs::GetParentDirectory (dirname)](#std__fs__GetParentDirectory) * [std::fs::IsDirectory (is_directory, is_dir)](#std__fs__IsDirectory) * [std::fs::IsFile (is_file)](#std__fs__IsFile) +* [std::fs::IsReadonly (is_readonly)](#std__fs__IsReadonly) * [std::fs::List (ls)](#std__fs__List) * [std::fs::MovePath (mv)](#std__fs__MovePath) * [std::fs::Print (cat)](#std__fs__Print) @@ -2392,6 +2393,32 @@ existing_file = is_file ./dir/somefile.txt #### Aliases: is_file + +## std::fs::IsReadonly +```sh +var = is_readonly path +``` + +This command will return true/false based if the provided path exists and is set to readonly. + +#### Parameters + +The path to check. + +#### Return Value + +True if the provided path exists and is set to readonly. + +#### Examples + +```sh +readonly = is_readonly ./dir/somefile.txt +``` + + +#### Aliases: +is_readonly + ## std::fs::List ```sh diff --git a/duckscript_sdk/src/sdk/std/fs/is_readonly/help.md b/duckscript_sdk/src/sdk/std/fs/is_readonly/help.md new file mode 100644 index 0000000..e88e8be --- /dev/null +++ b/duckscript_sdk/src/sdk/std/fs/is_readonly/help.md @@ -0,0 +1,19 @@ +```sh +var = is_readonly path +``` + +This command will return true/false based if the provided path exists and is set to readonly. + +#### Parameters + +The path to check. + +#### Return Value + +True if the provided path exists and is set to readonly. + +#### Examples + +```sh +readonly = is_readonly ./dir/somefile.txt +``` diff --git a/duckscript_sdk/src/sdk/std/fs/is_readonly/mod.rs b/duckscript_sdk/src/sdk/std/fs/is_readonly/mod.rs new file mode 100755 index 0000000..c322a34 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/fs/is_readonly/mod.rs @@ -0,0 +1,49 @@ +use crate::utils::pckg; +use duckscript::types::command::{Command, CommandResult}; +use std::fs; + +#[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, "IsReadonly") + } + + fn aliases(&self) -> Vec { + vec!["is_readonly".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 { + match fs::metadata(&arguments[0]) { + Ok(metadata) => { + CommandResult::Continue(Some(metadata.permissions().readonly().to_string())) + } + Err(error) => CommandResult::Error(error.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_readonly/mod_test.rs b/duckscript_sdk/src/sdk/std/fs/is_readonly/mod_test.rs new file mode 100644 index 0000000..f80de97 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/fs/is_readonly/mod_test.rs @@ -0,0 +1,22 @@ +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_readonly", "out"); +} + +#[test] +fn run_not_readonly() { + test::run_script_and_validate( + vec![create("")], + "out = is_readonly ./Cargo.toml", + 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 ba2e6be..412b266 100755 --- a/duckscript_sdk/src/sdk/std/fs/mod.rs +++ b/duckscript_sdk/src/sdk/std/fs/mod.rs @@ -6,6 +6,7 @@ mod dirname; mod exists; mod is_directory; mod is_file; +mod is_readonly; mod list; mod mkdir; mod mv; @@ -36,6 +37,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr commands.set(exists::create(&package))?; commands.set(is_directory::create(&package))?; commands.set(is_file::create(&package))?; + commands.set(is_readonly::create(&package))?; commands.set(list::create(&package))?; commands.set(mkdir::create(&package))?; commands.set(mv::create(&package))?; diff --git a/test/std/fs/is_readonly_test.ds b/test/std/fs/is_readonly_test.ds new file mode 100644 index 0000000..0adf598 --- /dev/null +++ b/test/std/fs/is_readonly_test.ds @@ -0,0 +1,20 @@ + +fn test_not_readonly + value = is_readonly ./Cargo.toml + + error = get_last_error + empty = is_empty ${error} + assert ${empty} + + assert_false ${value} +end + +fn test_not_found + value = is_readonly ./badpath + + error = get_last_error + empty = is_empty ${error} + assert_false ${empty} + + assert_false ${value} +end