From 9ea7cdfc3315f434144cc6353dd22413998c1927 Mon Sep 17 00:00:00 2001 From: Gabriel B Gutierrez Date: Wed, 13 Oct 2021 19:29:08 -0300 Subject: [PATCH] -i flag on signaure --- crates/nu-command/src/filesystem/cp.rs | 9 +++++++++ crates/nu-command/src/filesystem/mv.rs | 19 +++++++++++++++++++ crates/nu-command/src/filesystem/rm.rs | 13 +++++++++++++ 3 files changed, 41 insertions(+) diff --git a/crates/nu-command/src/filesystem/cp.rs b/crates/nu-command/src/filesystem/cp.rs index b042d50f11..f8e2b2ba61 100644 --- a/crates/nu-command/src/filesystem/cp.rs +++ b/crates/nu-command/src/filesystem/cp.rs @@ -29,6 +29,7 @@ impl Command for Cp { "copy recursively through subdirectories", Some('r'), ) + .switch("interactive", "ask user to confirm action", Some('i')) } fn run( @@ -39,6 +40,14 @@ impl Command for Cp { ) -> Result { let source: String = call.req(context, 0)?; let destination: String = call.req(context, 1)?; + let interactive = call.has_flag("interactive"); + + if interactive { + println!( + "Are you shure that you want to move {} to {}?", + source, destination + ); + } let path: PathBuf = current_dir().unwrap(); let source = path.join(source.as_str()); diff --git a/crates/nu-command/src/filesystem/mv.rs b/crates/nu-command/src/filesystem/mv.rs index 98dd8a3217..15d92241f0 100644 --- a/crates/nu-command/src/filesystem/mv.rs +++ b/crates/nu-command/src/filesystem/mv.rs @@ -29,6 +29,8 @@ impl Command for Mv { SyntaxShape::Filepath, "the location to move files/directories to", ) + .switch("interactive", "ask user to confirm action", Some('i')) + .switch("force", "suppress error when no file", Some('f')) } fn run( @@ -40,6 +42,8 @@ impl Command for Mv { // TODO: handle invalid directory or insufficient permissions when moving let source: String = call.req(context, 0)?; let destination: String = call.req(context, 1)?; + let interactive = call.has_flag("interactive"); + let force = call.has_flag("force"); let path: PathBuf = current_dir().unwrap(); let source = path.join(source.as_str()); @@ -54,6 +58,21 @@ impl Command for Mv { )); } + if interactive && !force { + for file in &sources { + println!( + "Are you shure that you want to move {} to {}?", + file.as_ref() + .unwrap() + .file_name() + .unwrap() + .to_str() + .unwrap(), + destination.file_name().unwrap().to_str().unwrap() + ); + } + } + if (destination.exists() && !destination.is_dir() && sources.len() > 1) || (!destination.exists() && sources.len() > 1) { diff --git a/crates/nu-command/src/filesystem/rm.rs b/crates/nu-command/src/filesystem/rm.rs index e10206a1c6..96fd7e228c 100644 --- a/crates/nu-command/src/filesystem/rm.rs +++ b/crates/nu-command/src/filesystem/rm.rs @@ -19,6 +19,7 @@ struct RmArgs { trash: bool, permanent: bool, force: bool, + interactive: bool, } impl Command for Rm { @@ -44,6 +45,7 @@ impl Command for Rm { ) .switch("recursive", "delete subdirectories recursively", Some('r')) .switch("force", "suppress error when no file", Some('f')) + .switch("interactive", "ask user to confirm action", Some('i')) .rest( "rest", SyntaxShape::GlobPattern, @@ -64,6 +66,7 @@ impl Command for Rm { fn rm(context: &EvaluationContext, call: &Call) -> Result { let trash = call.has_flag("trash"); let permanent = call.has_flag("permanent"); + let interactive = call.has_flag("interactive"); if trash && permanent { return Err(ShellError::IncompatibleParametersSingle( @@ -122,12 +125,22 @@ fn rm(context: &EvaluationContext, call: &Call) -> Result { let recursive = call.has_flag("recursive"); let force = call.has_flag("force"); + if interactive && !force { + for file in &targets { + println!( + "Are you shure that you want to delete {}?", + file.1.file_name().unwrap().to_str().unwrap() + ); + } + } + let args = RmArgs { targets, recursive, trash, permanent, force, + interactive, }; let response = rm_helper(call, args);