mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-14 11:53:05 +00:00
New is_readonly command
This commit is contained in:
parent
c197b042f4
commit
77e256dbfe
|
@ -2,6 +2,7 @@
|
|||
|
||||
### v0.2.2
|
||||
|
||||
* New is_readonly command.
|
||||
* New is_path_exists command.
|
||||
* New is_directory command.
|
||||
* New is_file command.
|
||||
|
|
27
docs/sdk.md
27
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
|
||||
|
||||
<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
|
||||
|
|
19
duckscript_sdk/src/sdk/std/fs/is_readonly/help.md
Normal file
19
duckscript_sdk/src/sdk/std/fs/is_readonly/help.md
Normal 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
|
||||
```
|
49
duckscript_sdk/src/sdk/std/fs/is_readonly/mod.rs
Executable file
49
duckscript_sdk/src/sdk/std/fs/is_readonly/mod.rs
Executable 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(),
|
||||
})
|
||||
}
|
22
duckscript_sdk/src/sdk/std/fs/is_readonly/mod_test.rs
Normal file
22
duckscript_sdk/src/sdk/std/fs/is_readonly/mod_test.rs
Normal 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()),
|
||||
);
|
||||
}
|
|
@ -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))?;
|
||||
|
|
20
test/std/fs/is_readonly_test.ds
Normal file
20
test/std/fs/is_readonly_test.ds
Normal 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
|
Loading…
Reference in a new issue