From 8e7e8c17e14e54139486db9a7205e4a2970763c8 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sat, 11 Apr 2020 18:53:53 +1200 Subject: [PATCH] Make trash support optional (#1572) --- Cargo.toml | 3 +- crates/nu-cli/Cargo.toml | 3 +- crates/nu-cli/src/commands/rm.rs | 1 + crates/nu-cli/src/shell/filesystem_shell.rs | 36 ++++++++++++++------- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c47dce8a4..e81bfff6c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,7 +62,7 @@ nu-build = { version = "0.12.0", path = "./crates/nu-build" } test-bins = [] default = ["sys", "ps", "textview", "inc", "str"] -stable = ["default", "starship-prompt", "binaryview", "match", "tree", "average", "post", "fetch", "clipboard-cli"] +stable = ["default", "starship-prompt", "binaryview", "match", "tree", "average", "post", "fetch", "clipboard-cli", "trash-support"] # Default textview = ["crossterm", "syntect", "url", "nu_plugin_textview"] @@ -82,6 +82,7 @@ tree = ["nu_plugin_tree"] clipboard-cli = ["nu-cli/clipboard-cli"] starship-prompt = ["nu-cli/starship-prompt"] +trash-support = ["nu-cli/trash-support"] [[bin]] name = "fail" diff --git a/crates/nu-cli/Cargo.toml b/crates/nu-cli/Cargo.toml index cd77b84de..cfeb333d0 100644 --- a/crates/nu-cli/Cargo.toml +++ b/crates/nu-cli/Cargo.toml @@ -79,11 +79,11 @@ term = "0.5.2" termcolor = "1.1.0" textwrap = {version = "0.11.0", features = ["term_size"]} toml = "0.5.6" -trash = "1.0.0" typetag = "0.1.4" umask = "0.1" unicode-xid = "0.2.0" +trash = { version = "1.0.0", optional = true } clipboard = { version = "0.5", optional = true } starship = { version = "0.39.0", optional = true } @@ -101,3 +101,4 @@ nu-build = { version = "0.12.0", path = "../nu-build" } stable = [] starship-prompt = ["starship"] clipboard-cli = ["clipboard"] +trash-support = ["trash"] \ No newline at end of file diff --git a/crates/nu-cli/src/commands/rm.rs b/crates/nu-cli/src/commands/rm.rs index 2c766f037..af9461408 100644 --- a/crates/nu-cli/src/commands/rm.rs +++ b/crates/nu-cli/src/commands/rm.rs @@ -12,6 +12,7 @@ pub struct Remove; pub struct RemoveArgs { pub rest: Vec>, pub recursive: Tagged, + #[allow(unused)] pub trash: Tagged, } diff --git a/crates/nu-cli/src/shell/filesystem_shell.rs b/crates/nu-cli/src/shell/filesystem_shell.rs index 8fde96668..33fa049e2 100644 --- a/crates/nu-cli/src/shell/filesystem_shell.rs +++ b/crates/nu-cli/src/shell/filesystem_shell.rs @@ -946,7 +946,7 @@ impl Shell for FilesystemShell { RemoveArgs { rest: targets, recursive, - trash, + trash: _trash, }: RemoveArgs, name: Tag, path: &str, @@ -1024,16 +1024,30 @@ impl Shell for FilesystemShell { if let Ok(metadata) = f.symlink_metadata() { if metadata.is_file() || metadata.file_type().is_symlink() || recursive.item || is_empty() { - let result = if trash.item { - trash::remove(f) - .map_err(|e| f.to_string_lossy()) - } else if metadata.is_file() { - std::fs::remove_file(f) - .map_err(|e| f.to_string_lossy()) - } else { - std::fs::remove_dir_all(f) - .map_err(|e| f.to_string_lossy()) - }; + let result; + #[cfg(feature = "trash-support")] + { + result = if _trash.item { + trash::remove(f) + .map_err(|e| f.to_string_lossy()) + } else if metadata.is_file() { + std::fs::remove_file(f) + .map_err(|e| f.to_string_lossy()) + } else { + std::fs::remove_dir_all(f) + .map_err(|e| f.to_string_lossy()) + }; + } + #[cfg(not(feature = "trash-support"))] + { + result = if metadata.is_file() { + std::fs::remove_file(f) + .map_err(|e| f.to_string_lossy()) + } else { + std::fs::remove_dir_all(f) + .map_err(|e| f.to_string_lossy()) + }; + } if let Err(e) = result { let msg = format!("Could not delete {:}", e);