Read from standard input in rm (#3763)

* Read from standard input in `rm`

With this change, rm falls back to reading from the standard input if no
arguments are supplied. This leads to more intuitive pipes as seen in
https://github.com/nushell/nushell/issues/2824

 ls | get name | sort-by | first 20 | each {rm $it} becomes
 ls | get name | sort-by | first 20 | rm

* [Fix] Run cargo fmt, make files a rest parameter, and fix cargo build warnings

* [Fix] Fix clippy suggestions
This commit is contained in:
soumil-07 2021-07-25 08:31:53 +05:30 committed by GitHub
parent 27f1e7b60c
commit e6af7f75a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,8 @@ use crate::prelude::*;
use nu_engine::shell::RemoveArgs; use nu_engine::shell::RemoveArgs;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape}; use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged;
pub struct Remove; pub struct Remove;
@ -66,7 +67,7 @@ fn rm(args: CommandArgs) -> Result<ActionStream, ShellError> {
let name = args.call_info.name_tag.clone(); let name = args.call_info.name_tag.clone();
let shell_manager = args.shell_manager(); let shell_manager = args.shell_manager();
let args = RemoveArgs { let mut rm_args = RemoveArgs {
rest: args.rest(0)?, rest: args.rest(0)?,
recursive: args.has_flag("recursive"), recursive: args.has_flag("recursive"),
trash: args.has_flag("trash"), trash: args.has_flag("trash"),
@ -74,7 +75,7 @@ fn rm(args: CommandArgs) -> Result<ActionStream, ShellError> {
force: args.has_flag("force"), force: args.has_flag("force"),
}; };
if args.trash && args.permanent { if rm_args.trash && rm_args.permanent {
return Ok(ActionStream::one(Err(ShellError::labeled_error( return Ok(ActionStream::one(Err(ShellError::labeled_error(
"only one of --permanent and --trash can be used", "only one of --permanent and --trash can be used",
"conflicting flags", "conflicting flags",
@ -82,7 +83,19 @@ fn rm(args: CommandArgs) -> Result<ActionStream, ShellError> {
)))); ))));
} }
shell_manager.rm(args, name) if rm_args.rest.is_empty() {
let mut input_peek = args.input.peekable();
while let Some(v) = &input_peek.next() {
if let UntaggedValue::Primitive(Primitive::FilePath(path)) = &v.value {
rm_args.rest.push(Tagged {
item: path.to_path_buf(),
tag: args.call_info.name_tag.clone(),
})
};
}
}
shell_manager.rm(rm_args, name)
} }
#[cfg(test)] #[cfg(test)]