From e6af7f75a108775eba485fbc86c0b328f3fa01e3 Mon Sep 17 00:00:00 2001 From: soumil-07 <82497827+soumil-07@users.noreply.github.com> Date: Sun, 25 Jul 2021 08:31:53 +0530 Subject: [PATCH] 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 --- .../nu-command/src/commands/filesystem/rm.rs | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/nu-command/src/commands/filesystem/rm.rs b/crates/nu-command/src/commands/filesystem/rm.rs index 35c3762ddf..2769b79391 100644 --- a/crates/nu-command/src/commands/filesystem/rm.rs +++ b/crates/nu-command/src/commands/filesystem/rm.rs @@ -2,7 +2,8 @@ use crate::prelude::*; use nu_engine::shell::RemoveArgs; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{Signature, SyntaxShape}; +use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue}; +use nu_source::Tagged; pub struct Remove; @@ -66,7 +67,7 @@ fn rm(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let shell_manager = args.shell_manager(); - let args = RemoveArgs { + let mut rm_args = RemoveArgs { rest: args.rest(0)?, recursive: args.has_flag("recursive"), trash: args.has_flag("trash"), @@ -74,7 +75,7 @@ fn rm(args: CommandArgs) -> Result { 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( "only one of --permanent and --trash can be used", "conflicting flags", @@ -82,7 +83,19 @@ fn rm(args: CommandArgs) -> Result { )))); } - 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)]