Implement an option to show paths made of mkdir. (#1932)

This commit is contained in:
utam0k 2020-06-07 04:13:38 +09:00 committed by GitHub
parent ba6370621f
commit 15e66ae065
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 5 deletions

View file

@ -11,6 +11,8 @@ pub struct Mkdir;
#[derive(Deserialize)]
pub struct MkdirArgs {
pub rest: Vec<Tagged<PathBuf>>,
#[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 {

View file

@ -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<OutputStream, ShellError> {
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(

View file

@ -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");
})
}