remove without check path exist, rm -f (#2590)

This commit is contained in:
Luccas Mateus 2020-09-22 18:11:31 -03:00 committed by GitHub
parent ebba89ea31
commit e8ec5027ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 2 deletions

View File

@ -16,6 +16,7 @@ pub struct RemoveArgs {
pub trash: Tagged<bool>, pub trash: Tagged<bool>,
#[allow(unused)] #[allow(unused)]
pub permanent: Tagged<bool>, pub permanent: Tagged<bool>,
pub force: Tagged<bool>,
} }
#[async_trait] #[async_trait]
@ -37,6 +38,7 @@ impl WholeStreamCommand for Remove {
Some('p'), Some('p'),
) )
.switch("recursive", "delete subdirectories recursively", Some('r')) .switch("recursive", "delete subdirectories recursively", Some('r'))
.switch("force", "suppress error when no file", Some('f'))
.rest(SyntaxShape::Pattern, "the file path(s) to remove") .rest(SyntaxShape::Pattern, "the file path(s) to remove")
} }
@ -69,6 +71,11 @@ impl WholeStreamCommand for Remove {
example: "rm --permanent file.txt", example: "rm --permanent file.txt",
result: None, result: None,
}, },
Example {
description: "Delete a file, and suppress errors if no file is found",
example: "rm --force file.txt",
result: None,
}
] ]
} }
} }

View File

@ -496,6 +496,7 @@ impl Shell for FilesystemShell {
recursive, recursive,
trash: _trash, trash: _trash,
permanent: _permanent, permanent: _permanent,
force: _force,
}: RemoveArgs, }: RemoveArgs,
name: Tag, name: Tag,
path: &str, path: &str,
@ -556,7 +557,7 @@ impl Shell for FilesystemShell {
}; };
} }
if all_targets.is_empty() { if all_targets.is_empty() && !_force.item {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"No valid paths", "No valid paths",
"no valid paths", "no valid paths",

View File

@ -125,7 +125,6 @@ fn removes_directory_contents_with_recursive_flag() {
fn errors_if_attempting_to_delete_a_directory_with_content_without_recursive_flag() { fn errors_if_attempting_to_delete_a_directory_with_content_without_recursive_flag() {
Playground::setup("rm_test_6", |dirs, sandbox| { Playground::setup("rm_test_6", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("some_empty_file.txt")]); sandbox.with_files(vec![EmptyFile("some_empty_file.txt")]);
let actual = nu!( let actual = nu!(
cwd: dirs.root(), cwd: dirs.root(),
"rm rm_test_6" "rm rm_test_6"
@ -265,3 +264,15 @@ fn remove_files_from_two_parents_up_using_multiple_dots_and_glob() {
)); ));
}) })
} }
#[test]
fn no_errors_if_attempting_to_delete_non_existent_file_with_f_flag() {
Playground::setup("rm_test_14", |dirs, _| {
let actual = nu!(
cwd: dirs.root(),
"rm -f non_existent_file.txt"
);
assert!(!actual.err.contains("no valid path"));
})
}