From 15e66ae0656a0e5b45b6d3eb4c017f46ea336b20 Mon Sep 17 00:00:00 2001 From: utam0k Date: Sun, 7 Jun 2020 04:13:38 +0900 Subject: [PATCH] Implement an option to show paths made of mkdir. (#1932) --- crates/nu-cli/src/commands/mkdir.rs | 6 +++++- crates/nu-cli/src/shell/filesystem_shell.rs | 14 +++++++++--- crates/nu-cli/tests/commands/mkdir.rs | 24 ++++++++++++++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/crates/nu-cli/src/commands/mkdir.rs b/crates/nu-cli/src/commands/mkdir.rs index 54debf250c..34f2e0c026 100644 --- a/crates/nu-cli/src/commands/mkdir.rs +++ b/crates/nu-cli/src/commands/mkdir.rs @@ -11,6 +11,8 @@ pub struct Mkdir; #[derive(Deserialize)] pub struct MkdirArgs { pub rest: Vec>, + #[serde(rename = "show-created-paths")] + pub show_created_paths: bool, } #[async_trait] @@ -20,7 +22,9 @@ impl WholeStreamCommand for Mkdir { } fn signature(&self) -> Signature { - Signature::build("mkdir").rest(SyntaxShape::Path, "the name(s) of the path(s) to create") + Signature::build("mkdir") + .rest(SyntaxShape::Path, "the name(s) of the path(s) to create") + .switch("show-created-paths", "show the path(s) created.", Some('s')) } fn usage(&self) -> &str { diff --git a/crates/nu-cli/src/shell/filesystem_shell.rs b/crates/nu-cli/src/shell/filesystem_shell.rs index a423a00a3c..141fd00cee 100644 --- a/crates/nu-cli/src/shell/filesystem_shell.rs +++ b/crates/nu-cli/src/shell/filesystem_shell.rs @@ -396,11 +396,15 @@ impl Shell for FilesystemShell { fn mkdir( &self, - MkdirArgs { rest: directories }: MkdirArgs, + MkdirArgs { + rest: directories, + show_created_paths, + }: MkdirArgs, name: Tag, path: &str, ) -> Result { let path = Path::new(path); + let mut stream = VecDeque::new(); if directories.is_empty() { return Err(ShellError::labeled_error( @@ -413,7 +417,7 @@ impl Shell for FilesystemShell { for dir in directories.iter() { let create_at = path.join(&dir.item); - let dir_res = std::fs::create_dir_all(create_at); + let dir_res = std::fs::create_dir_all(&create_at); if let Err(reason) = dir_res { return Err(ShellError::labeled_error( reason.to_string(), @@ -421,9 +425,13 @@ impl Shell for FilesystemShell { dir.tag(), )); } + if show_created_paths { + let val = format!("{:}", create_at.to_string_lossy()).into(); + stream.push_back(Ok(ReturnSuccess::Value(val))); + } } - Ok(OutputStream::empty()) + Ok(stream.into()) } fn mv( diff --git a/crates/nu-cli/tests/commands/mkdir.rs b/crates/nu-cli/tests/commands/mkdir.rs index e6d545c4a4..b14376f250 100644 --- a/crates/nu-cli/tests/commands/mkdir.rs +++ b/crates/nu-cli/tests/commands/mkdir.rs @@ -1,6 +1,6 @@ use nu_test_support::fs::files_exist_at; -use nu_test_support::nu; use nu_test_support::playground::Playground; +use nu_test_support::{nu, pipeline}; use std::path::Path; #[test] @@ -61,3 +61,25 @@ fn create_directory_two_parents_up_using_multiple_dots() { assert!(expected.exists()); }) } + +#[test] +fn show_created_paths() { + Playground::setup("mkdir_test_2", |dirs, _| { + let actual = nu!( + cwd: dirs.test(), + pipeline( + r#" + mkdir -s dir_1 dir_2 dir_3 + | count + | echo $it + "# + )); + + assert!(files_exist_at( + vec![Path::new("dir_1"), Path::new("dir_2"), Path::new("dir_3")], + dirs.test() + )); + + assert_eq!(actual.out, "3"); + }) +}