touch command #4

This commit is contained in:
sagie gur ari 2020-01-01 19:40:19 +00:00
parent cd9fb8f7bd
commit 950d07abe1
9 changed files with 194 additions and 0 deletions

View file

@ -2,6 +2,7 @@
### v0.1.2
* New **touch** command #4
* New **dirname** command #6
* New **canonicalize** command #21
* New **basename** command #5

View file

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

View file

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

View file

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

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

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

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

View file

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

View file

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