mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-04 15:09:08 +00:00
touch command #4
This commit is contained in:
parent
cd9fb8f7bd
commit
950d07abe1
|
@ -2,6 +2,7 @@
|
|||
|
||||
### v0.1.2
|
||||
|
||||
* New **touch** command #4
|
||||
* New **dirname** command #6
|
||||
* New **canonicalize** command #21
|
||||
* New **basename** command #5
|
||||
|
|
29
docs/sdk.md
29
docs/sdk.md
|
@ -16,6 +16,7 @@
|
|||
* [sdk::env::Set](#sdk__env__Set)
|
||||
* [sdk::env::SetCurrentDirectory](#sdk__env__SetCurrentDirectory)
|
||||
* [sdk::fs::CreateDirectory](#sdk__fs__CreateDirectory)
|
||||
* [sdk::fs::CreateEmptyFile](#sdk__fs__CreateEmptyFile)
|
||||
* [sdk::fs::GetCanonicalPath](#sdk__fs__GetCanonicalPath)
|
||||
* [sdk::fs::GetFileName](#sdk__fs__GetFileName)
|
||||
* [sdk::fs::GetParentDirectory](#sdk__fs__GetParentDirectory)
|
||||
|
@ -766,6 +767,34 @@ exists = mkdir ./dir/subdir
|
|||
#### Aliases:
|
||||
mkdir
|
||||
|
||||
<a name="sdk__fs__CreateEmptyFile"></a>
|
||||
## sdk::fs::CreateEmptyFile
|
||||
```sh
|
||||
var = touch file
|
||||
```
|
||||
|
||||
This command will create an empty file and return true/false if the file exists.<br>
|
||||
If file exits, it will not be modified.
|
||||
|
||||
#### Parameters
|
||||
|
||||
The file path.
|
||||
|
||||
#### Return Value
|
||||
|
||||
If the file exists after the command, it will return true.<br>
|
||||
In case of any error, it will return false.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
exists = touch ./dir/file.txt
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
touch
|
||||
|
||||
<a name="sdk__fs__GetCanonicalPath"></a>
|
||||
## sdk::fs::GetCanonicalPath
|
||||
```sh
|
||||
|
|
|
@ -7,8 +7,17 @@ additional_profiles = [
|
|||
"publish-pre-cleanup"
|
||||
]
|
||||
|
||||
[tasks.clean-target]
|
||||
script_runner = "@shell"
|
||||
script = [
|
||||
"rm -Rf ./target"
|
||||
]
|
||||
|
||||
[tasks.test]
|
||||
clear = true
|
||||
dependencies = [
|
||||
"clean-target"
|
||||
]
|
||||
run_task = { name = ["test-thread-safe", "test-single-threaded"] }
|
||||
|
||||
[tasks.test-thread-safe]
|
||||
|
|
|
@ -4,6 +4,7 @@ mod dirname;
|
|||
mod mkdir;
|
||||
mod print;
|
||||
mod read;
|
||||
mod touch;
|
||||
mod write;
|
||||
|
||||
use crate::utils::pckg;
|
||||
|
@ -21,6 +22,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
|||
commands.set(mkdir::create(&package))?;
|
||||
commands.set(print::create(&package))?;
|
||||
commands.set(read::create(&package))?;
|
||||
commands.set(touch::create(&package))?;
|
||||
commands.set(write::create(&package))?;
|
||||
|
||||
Ok(())
|
||||
|
|
21
duckscript_sdk/src/sdk/std/fs/touch/help.md
Normal file
21
duckscript_sdk/src/sdk/std/fs/touch/help.md
Normal file
|
@ -0,0 +1,21 @@
|
|||
```sh
|
||||
var = touch file
|
||||
```
|
||||
|
||||
This command will create an empty file and return true/false if the file exists.<br>
|
||||
If file exits, it will not be modified.
|
||||
|
||||
#### Parameters
|
||||
|
||||
The file path.
|
||||
|
||||
#### Return Value
|
||||
|
||||
If the file exists after the command, it will return true.<br>
|
||||
In case of any error, it will return false.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
exists = touch ./dir/file.txt
|
||||
```
|
41
duckscript_sdk/src/sdk/std/fs/touch/mod.rs
Executable file
41
duckscript_sdk/src/sdk/std/fs/touch/mod.rs
Executable file
|
@ -0,0 +1,41 @@
|
|||
use crate::utils::{io, pckg};
|
||||
use duckscript::types::command::{Command, CommandResult};
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "./mod_test.rs"]
|
||||
mod mod_test;
|
||||
|
||||
struct CommandImpl {
|
||||
package: String,
|
||||
}
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> String {
|
||||
pckg::concat(&self.package, "CreateEmptyFile")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["touch".to_string()]
|
||||
}
|
||||
|
||||
fn help(&self) -> String {
|
||||
include_str!("help.md").to_string()
|
||||
}
|
||||
|
||||
fn run(&self, arguments: Vec<String>) -> CommandResult {
|
||||
if arguments.is_empty() {
|
||||
CommandResult::Error("Path not provided.".to_string())
|
||||
} else {
|
||||
match io::create_empty_file(&arguments[0]) {
|
||||
Ok(_) => CommandResult::Continue(Some("true".to_string())),
|
||||
Err(_) => CommandResult::Continue(Some("false".to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
34
duckscript_sdk/src/sdk/std/fs/touch/mod_test.rs
Normal file
34
duckscript_sdk/src/sdk/std/fs/touch/mod_test.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
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_fail(vec![create("")], "touch");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_provided() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = touch ./target/_duckscript/touch/new/file.txt",
|
||||
CommandValidation::Match("out".to_string(), "true".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_path_to_existing_directory() {
|
||||
let result = io::create_directory("./target/_duckscript/touch/existing_dir");
|
||||
assert!(result.is_ok());
|
||||
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = touch ./target/_duckscript/touch/existing_dir",
|
||||
CommandValidation::Match("out".to_string(), "false".to_string()),
|
||||
);
|
||||
}
|
|
@ -105,3 +105,33 @@ pub(crate) fn write_text_file(file: &str, text: &str) -> Result<(), ScriptError>
|
|||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create_empty_file(file: &str) -> Result<(), String> {
|
||||
let file_path = Path::new(file);
|
||||
|
||||
if file_path.exists() {
|
||||
if file_path.is_file() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!(
|
||||
"Unable to create file: {} directory with that path exists.",
|
||||
file
|
||||
)
|
||||
.to_string())
|
||||
}
|
||||
} else {
|
||||
// create parent directory
|
||||
let directory = file_path.parent();
|
||||
match directory {
|
||||
Some(directory_path) => match create_directory_for_path(&directory_path) {
|
||||
_ => (),
|
||||
},
|
||||
None => (),
|
||||
};
|
||||
|
||||
match File::create(&file_path) {
|
||||
Ok(_) => Ok(()),
|
||||
_ => Err(format!("Unable to create file: {}", file).to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,3 +45,30 @@ fn write_text_file_valid() {
|
|||
|
||||
assert_eq!(text, "test file");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_empty_file_not_exists() {
|
||||
let path = "./target/_duckscript/temp/create_empty_file_not_exists/file.txt";
|
||||
let file_path = Path::new(path);
|
||||
assert!(!file_path.exists());
|
||||
|
||||
let result = create_empty_file(path);
|
||||
|
||||
assert!(result.is_ok());
|
||||
assert!(file_path.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_empty_file_exists() {
|
||||
let path = "./target/_duckscript/temp/create_empty_file_exists/file.txt";
|
||||
let file_path = Path::new(path);
|
||||
assert!(!file_path.exists());
|
||||
|
||||
let result = create_empty_file(path);
|
||||
|
||||
assert!(result.is_ok());
|
||||
assert!(file_path.exists());
|
||||
|
||||
let result = create_empty_file(path);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue