2019-08-21 12:07:37 +00:00
|
|
|
use crate::commands::command::RunnablePerItemContext;
|
2019-08-07 02:45:38 +00:00
|
|
|
use crate::errors::ShellError;
|
2019-08-09 04:51:21 +00:00
|
|
|
use crate::parser::registry::{CommandRegistry, Signature};
|
2019-08-07 02:45:38 +00:00
|
|
|
use crate::prelude::*;
|
2019-08-21 12:07:37 +00:00
|
|
|
use std::path::PathBuf;
|
2019-08-07 02:45:38 +00:00
|
|
|
|
|
|
|
pub struct Mkdir;
|
|
|
|
|
2019-08-21 12:07:37 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct MkdirArgs {
|
|
|
|
rest: Vec<Tagged<PathBuf>>,
|
|
|
|
}
|
|
|
|
|
2019-08-15 05:02:02 +00:00
|
|
|
impl PerItemCommand for Mkdir {
|
2019-08-09 04:51:21 +00:00
|
|
|
fn run(
|
|
|
|
&self,
|
2019-08-15 05:02:02 +00:00
|
|
|
call_info: &CallInfo,
|
2019-08-21 12:07:37 +00:00
|
|
|
_registry: &CommandRegistry,
|
2019-08-15 05:02:02 +00:00
|
|
|
shell_manager: &ShellManager,
|
2019-08-21 12:07:37 +00:00
|
|
|
_input: Tagged<Value>,
|
2019-08-15 05:02:02 +00:00
|
|
|
) -> Result<VecDeque<ReturnValue>, ShellError> {
|
2019-08-21 12:07:37 +00:00
|
|
|
call_info.process(shell_manager, mkdir)?.run()
|
2019-08-07 02:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"mkdir"
|
|
|
|
}
|
|
|
|
|
2019-08-09 04:51:21 +00:00
|
|
|
fn signature(&self) -> Signature {
|
2019-08-21 12:07:37 +00:00
|
|
|
Signature::build("mkdir").rest()
|
2019-08-07 02:45:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 12:07:37 +00:00
|
|
|
fn mkdir(
|
|
|
|
MkdirArgs { rest: directories }: MkdirArgs,
|
|
|
|
RunnablePerItemContext {
|
|
|
|
name,
|
|
|
|
shell_manager,
|
|
|
|
..
|
|
|
|
}: &RunnablePerItemContext,
|
2019-08-15 05:02:02 +00:00
|
|
|
) -> Result<VecDeque<ReturnValue>, ShellError> {
|
2019-08-21 12:07:37 +00:00
|
|
|
let full_path = PathBuf::from(shell_manager.path());
|
2019-08-09 04:51:21 +00:00
|
|
|
|
2019-08-21 12:07:37 +00:00
|
|
|
if directories.len() == 0 {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"mkdir requires directory paths",
|
|
|
|
"needs parameter",
|
|
|
|
name,
|
|
|
|
));
|
2019-08-07 02:45:38 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 12:07:37 +00:00
|
|
|
for dir in directories.iter() {
|
|
|
|
let create_at = {
|
|
|
|
let mut loc = full_path.clone();
|
|
|
|
loc.push(&dir.item);
|
|
|
|
loc
|
|
|
|
};
|
|
|
|
|
|
|
|
match std::fs::create_dir_all(create_at) {
|
|
|
|
Err(reason) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
reason.to_string(),
|
|
|
|
reason.to_string(),
|
|
|
|
dir.span(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
Ok(_) => {}
|
|
|
|
}
|
2019-08-07 02:45:38 +00:00
|
|
|
}
|
2019-08-21 12:07:37 +00:00
|
|
|
|
|
|
|
Ok(VecDeque::new())
|
2019-08-07 02:45:38 +00:00
|
|
|
}
|