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-09 04:51:21 +00:00
|
|
|
impl StaticCommand for Mkdir {
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
mkdir(args, registry)
|
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-09 04:51:21 +00:00
|
|
|
pub fn mkdir(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once(registry)?;
|
|
|
|
|
2019-08-07 18:40:38 +00:00
|
|
|
let mut full_path = PathBuf::from(args.shell_manager.path());
|
2019-08-07 02:45:38 +00:00
|
|
|
|
|
|
|
match &args.nth(0) {
|
|
|
|
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(),
|
|
|
|
args.nth(0).unwrap().span(),
|
|
|
|
)),
|
|
|
|
Ok(_) => Ok(OutputStream::empty()),
|
2019-08-07 02:45:38 +00:00
|
|
|
}
|
|
|
|
}
|