rm command support for multiple paths

This commit is contained in:
sagie gur ari 2021-01-20 16:57:57 +00:00
parent 89611873b9
commit 8629fd39e0
5 changed files with 63 additions and 27 deletions

View file

@ -2,6 +2,7 @@
### v0.7.2
* rm command support for multiple paths.
* Fix: Command `rm` remove file with flag `-r` #158 (thanks @umaYnit)
* Upgraded rand 0.8

View file

@ -3300,20 +3300,20 @@ rmdir
<a name="std__fs__DeletePath"></a>
## std::fs::DeletePath
```sh
var = rm [-r] path
var = rm [-r] [path]+
```
This command delete the requested file, empty directory or recursively deletes a directory
and all its content (files and sub directories) if the **-r** flag is provided.
This command delete the requested file/s, empty directories or recursively deletes directories
and all their content (files and sub directories) if the **-r** flag is provided.
#### Parameters
* Optional flags (currently only -r is supported which indicates recursive deletion)
* The path to delete
* The path/s to delete
#### Return Value
**true** if the path was deleted.
**true** if all paths were deleted.
#### Examples
@ -3323,6 +3323,9 @@ deleted = rm ./target
# deletes a directory and all its content
deleted = rm -r ./target
# delete all provided paths
deleted = rm -r ./dir ./somefile ./anotherdir/subdir/file
```

View file

@ -1,18 +1,18 @@
```sh
var = rm [-r] path
var = rm [-r] [path]+
```
This command delete the requested file, empty directory or recursively deletes a directory
and all its content (files and sub directories) if the **-r** flag is provided.
This command delete the requested file/s, empty directories or recursively deletes directories
and all their content (files and sub directories) if the **-r** flag is provided.
#### Parameters
* Optional flags (currently only -r is supported which indicates recursive deletion)
* The path to delete
* The path/s to delete
#### Return Value
**true** if the path was deleted.
**true** if all paths were deleted.
#### Examples
@ -22,4 +22,7 @@ deleted = rm ./target
# deletes a directory and all its content
deleted = rm -r ./target
# delete all provided paths
deleted = rm -r ./dir ./somefile ./anotherdir/subdir/file
```

View file

@ -35,31 +35,35 @@ impl Command for CommandImpl {
{
CommandResult::Error("Path not provided.".to_string())
} else {
let (path_str, recursive) = if arguments.len() == 1 {
(&arguments[0], false)
let (start_index, recursive) = if arguments.len() == 1 {
(0, false)
} else if flags::is_unix_flags_argument(&arguments[0]) {
let recursive = flags::is_unix_flag_exists('r', &arguments[0]);
(&arguments[1], recursive)
(1, recursive)
} else {
(&arguments[0], false)
(0, false)
};
let path = Path::new(path_str);
let end_index = arguments.len();
for index in start_index..end_index {
let path = Path::new(&arguments[index]);
let result = if !path.exists() {
Ok(())
} else if path.is_file() {
fs::remove_file(&path)
} else if recursive {
fs::remove_dir_all(&path)
} else {
fs::remove_dir(&path)
};
let result = if !path.exists() {
Ok(())
} else if path.is_file() {
fs::remove_file(&path)
} else if recursive {
fs::remove_dir_all(&path)
} else {
fs::remove_dir(&path)
};
match result {
Ok(_) => CommandResult::Continue(Some("true".to_string())),
Err(error) => CommandResult::Error(error.to_string()),
if let Err(error) = result {
return CommandResult::Error(error.to_string());
}
}
CommandResult::Continue(Some("true".to_string()))
}
}
}

View file

@ -87,3 +87,28 @@ fn run_path_recursive() {
assert!(!path.exists());
}
#[test]
fn run_multiple_paths() {
let path1 = Path::new("./target/_duckscript/rm/multiple_paths/file1.txt");
assert!(ensure_exists("./target/_duckscript/rm/multiple_paths/file1.txt").is_ok());
assert!(path1.exists());
let path2 = Path::new("./target/_duckscript/rm/multiple_paths/file2.txt");
assert!(ensure_exists("./target/_duckscript/rm/multiple_paths/file2.txt").is_ok());
assert!(path2.exists());
let path3 = Path::new("./target/_duckscript/rm/multiple_paths/dir/file.txt");
assert!(ensure_exists("./target/_duckscript/rm/multiple_paths/dir/file.txt").is_ok());
assert!(path3.exists());
test::run_script_and_validate(
vec![create("")],
"out = rm -r ./target/_duckscript/rm/multiple_paths/file1.txt ./target/_duckscript/rm/multiple_paths/file2.txt ./target/_duckscript/rm/multiple_paths/dir",
CommandValidation::Match("out".to_string(), "true".to_string()),
);
assert!(!path1.exists());
assert!(!path2.exists());
assert!(!path3.exists());
fs::remove_dir_all(&Path::new("./target/_duckscript/rm/multiple_paths")).unwrap();
}