nushell/crates/nu-command/src/filesystem/mkdir.rs
Artemiy 99aea0c71c
Filesystem commands print --verbose to stderr (#8014)
# Description

Makes `mkdir`, `cp`, `mv` and `rm` return nothing and print info to
stderr:

![image](https://user-images.githubusercontent.com/17511668/217859228-feffa4bc-c22d-45d3-b330-1903f5a4d938.png)
See https://github.com/nushell/nushell/pull/7925#issuecomment-1420861638
and
[discord](https://discord.com/channels/601130461678272522/615329862395101194/1072523941865857055).

# User-Facing Changes

`mkdir`, `cp`, `mv` and `rm` will return nothing and print info to
stderr with `--verbose` flag.

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-09 10:29:34 -08:00

108 lines
3.2 KiB
Rust

use std::collections::VecDeque;
use nu_engine::env::current_dir;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
SyntaxShape, Type, Value,
};
#[derive(Clone)]
pub struct Mkdir;
impl Command for Mkdir {
fn name(&self) -> &str {
"mkdir"
}
fn signature(&self) -> Signature {
Signature::build("mkdir")
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
.rest(
"rest",
SyntaxShape::Directory,
"the name(s) of the path(s) to create",
)
.switch("verbose", "print created path(s).", Some('v'))
.category(Category::FileSystem)
}
fn usage(&self) -> &str {
"Make directories, creates intermediary directories as required."
}
fn search_terms(&self) -> Vec<&str> {
vec!["directory", "folder", "create", "make_dirs"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let path = current_dir(engine_state, stack)?;
let mut directories = call
.rest::<String>(engine_state, stack, 0)?
.into_iter()
.map(|dir| path.join(dir))
.peekable();
let is_verbose = call.has_flag("verbose");
let mut stream: VecDeque<Value> = VecDeque::new();
if directories.peek().is_none() {
return Err(ShellError::MissingParameter(
"requires directory paths".to_string(),
call.head,
));
}
for (i, dir) in directories.enumerate() {
let span = call
.positional_nth(i)
.expect("already checked through directories")
.span;
let dir_res = std::fs::create_dir_all(&dir);
if let Err(reason) = dir_res {
return Err(ShellError::CreateNotPossible(
format!("failed to create directory: {reason}"),
call.positional_nth(i)
.expect("already checked through directories")
.span,
));
}
if is_verbose {
let val = format!("{:}", dir.to_string_lossy());
stream.push_back(Value::String { val, span });
}
}
stream
.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone())
.print_not_formatted(engine_state, false, true)?;
Ok(PipelineData::empty())
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Make a directory named foo",
example: "mkdir foo",
result: None,
},
Example {
description: "Make multiple directories and show the paths created",
example: "mkdir -v foo/bar foo2",
result: None,
},
]
}
}