2021-10-07 22:20:23 +00:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
2022-01-04 22:30:34 +00:00
|
|
|
use nu_engine::env::current_dir;
|
2021-10-07 22:20:23 +00:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::ast::Call;
|
2021-10-25 16:58:58 +00:00
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
2021-10-28 04:13:10 +00:00
|
|
|
use nu_protocol::{
|
2022-02-18 14:30:16 +00:00
|
|
|
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
|
|
|
|
SyntaxShape, Value,
|
2021-10-28 04:13:10 +00:00
|
|
|
};
|
2021-10-07 22:20:23 +00:00
|
|
|
|
2021-10-25 04:01:02 +00:00
|
|
|
#[derive(Clone)]
|
2021-10-07 22:20:23 +00:00
|
|
|
pub struct Mkdir;
|
|
|
|
|
|
|
|
impl Command for Mkdir {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"mkdir"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("mkdir")
|
|
|
|
.rest(
|
|
|
|
"rest",
|
|
|
|
SyntaxShape::Filepath,
|
|
|
|
"the name(s) of the path(s) to create",
|
|
|
|
)
|
|
|
|
.switch("show-created-paths", "show the path(s) created.", Some('s'))
|
2021-11-17 04:22:37 +00:00
|
|
|
.category(Category::FileSystem)
|
2021-10-07 22:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Make directories, creates intermediary directories as required."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
2021-10-25 06:31:39 +00:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-07 22:20:23 +00:00
|
|
|
call: &Call,
|
2021-10-25 04:01:02 +00:00
|
|
|
_input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
2022-01-04 22:30:34 +00:00
|
|
|
let path = current_dir(engine_state, stack)?;
|
2021-10-07 22:20:23 +00:00
|
|
|
let mut directories = call
|
2021-10-25 06:31:39 +00:00
|
|
|
.rest::<String>(engine_state, stack, 0)?
|
2021-10-07 22:20:23 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|dir| path.join(dir))
|
|
|
|
.peekable();
|
|
|
|
|
|
|
|
let show_created_paths = call.has_flag("show-created-paths");
|
|
|
|
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() {
|
2022-04-09 02:55:02 +00:00
|
|
|
let span = call
|
|
|
|
.positional_nth(i)
|
|
|
|
.expect("already checked through directories")
|
|
|
|
.span;
|
2021-10-07 22:20:23 +00:00
|
|
|
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),
|
2022-04-09 02:55:02 +00:00
|
|
|
call.positional_nth(i)
|
|
|
|
.expect("already checked through directories")
|
|
|
|
.span,
|
2021-10-07 22:20:23 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if show_created_paths {
|
|
|
|
let val = format!("{:}", dir.to_string_lossy());
|
|
|
|
stream.push_back(Value::String { val, span });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 04:13:10 +00:00
|
|
|
Ok(stream
|
|
|
|
.into_iter()
|
|
|
|
.into_pipeline_data(engine_state.ctrlc.clone()))
|
2021-10-07 22:20:23 +00:00
|
|
|
}
|
2022-02-18 14:30:16 +00:00
|
|
|
|
|
|
|
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 -s foo/bar foo2",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
2021-10-07 22:20:23 +00:00
|
|
|
}
|