New is_readonly command

This commit is contained in:
sagie gur ari 2020-03-06 13:20:54 +00:00
parent c197b042f4
commit 77e256dbfe
7 changed files with 140 additions and 0 deletions

View file

@ -2,6 +2,7 @@
### v0.2.2
* New is_readonly command.
* New is_path_exists command.
* New is_directory command.
* New is_file command.

View file

@ -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
<a name="std__fs__IsReadonly"></a>
## 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
<a name="std__fs__List"></a>
## std::fs::List
```sh

View file

@ -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
```

View file

@ -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<String> {
vec!["is_readonly".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.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<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -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()),
);
}

View file

@ -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))?;

View file

@ -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