2019-08-07 02:45:38 +00:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::parser::hir::SyntaxType;
|
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::*;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
pub struct Mkdir;
|
|
|
|
|
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-09 04:51:21 +00:00
|
|
|
registry: &CommandRegistry,
|
2019-08-15 05:02:02 +00:00
|
|
|
shell_manager: &ShellManager,
|
|
|
|
input: Tagged<Value>,
|
|
|
|
) -> Result<VecDeque<ReturnValue>, ShellError> {
|
|
|
|
mkdir(call_info, registry, shell_manager, input)
|
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 {
|
|
|
|
Signature::build("mkdir").named("file", SyntaxType::Any)
|
2019-08-07 02:45:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 05:02:02 +00:00
|
|
|
pub fn mkdir(
|
|
|
|
call_info: &CallInfo,
|
|
|
|
_registry: &CommandRegistry,
|
|
|
|
shell_manager: &ShellManager,
|
|
|
|
_input: Tagged<Value>,
|
|
|
|
) -> Result<VecDeque<ReturnValue>, ShellError> {
|
|
|
|
let mut full_path = PathBuf::from(shell_manager.path());
|
2019-08-09 04:51:21 +00:00
|
|
|
|
2019-08-15 05:02:02 +00:00
|
|
|
match &call_info.args.nth(0) {
|
2019-08-07 02:45:38 +00:00
|
|
|
Some(Tagged { item: value, .. }) => full_path.push(Path::new(&value.as_string()?)),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2019-08-07 19:38:00 +00:00
|
|
|
match std::fs::create_dir_all(full_path) {
|
|
|
|
Err(reason) => Err(ShellError::labeled_error(
|
|
|
|
reason.to_string(),
|
|
|
|
reason.to_string(),
|
2019-08-15 05:02:02 +00:00
|
|
|
call_info.args.nth(0).unwrap().span(),
|
2019-08-07 19:38:00 +00:00
|
|
|
)),
|
2019-08-15 05:02:02 +00:00
|
|
|
Ok(_) => Ok(VecDeque::new()),
|
2019-08-07 02:45:38 +00:00
|
|
|
}
|
|
|
|
}
|