Justin Ma 7b95e37bbe
Making coreutils umkdir as the default mkdir (#12007)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
`umkdir` was added in #10785, I think it's time to replace the default
one.

# After Submitting

Remove the old `mkdir` command and making coreutils' `umkdir` as the
default
2024-02-28 06:27:10 -06:00

99 lines
2.9 KiB
Rust

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, PipelineData, ShellError, Signature, SyntaxShape, Type};
use uu_mkdir::mkdir;
#[derive(Clone)]
pub struct UMkdir;
const IS_RECURSIVE: bool = true;
// This is the same default as Rust's std uses:
// https://doc.rust-lang.org/nightly/std/os/unix/fs/trait.DirBuilderExt.html#tymethod.mode
const DEFAULT_MODE: u32 = 0o777;
impl Command for UMkdir {
fn name(&self) -> &str {
"mkdir"
}
fn usage(&self) -> &str {
"Create directories, with intermediary directories if required using uutils/coreutils mkdir."
}
fn search_terms(&self) -> Vec<&str> {
vec!["directory", "folder", "create", "make_dirs", "coreutils"]
}
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 a message for each created directory.",
Some('v'),
)
.category(Category::FileSystem)
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let cwd = current_dir(engine_state, stack)?;
let mut directories = call
.rest::<String>(engine_state, stack, 0)?
.into_iter()
.map(|dir| nu_path::expand_path_with(dir, &cwd))
.peekable();
let is_verbose = call.has_flag(engine_state, stack, "verbose")?;
if directories.peek().is_none() {
return Err(ShellError::MissingParameter {
param_name: "requires directory paths".to_string(),
span: call.head,
});
}
for dir in directories {
if let Err(error) = mkdir(&dir, IS_RECURSIVE, DEFAULT_MODE, is_verbose) {
return Err(ShellError::GenericError {
error: format!("{}", error),
msg: format!("{}", error),
span: None,
help: None,
inner: vec![],
});
}
}
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,
},
]
}
}