2019-08-07 02:45:38 +00:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::parser::hir::SyntaxType;
|
|
|
|
use crate::parser::registry::{CommandConfig, NamedType, PositionalType};
|
|
|
|
use crate::prelude::*;
|
|
|
|
use indexmap::IndexMap;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
pub struct Mkdir;
|
|
|
|
|
|
|
|
impl Command for Mkdir {
|
|
|
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
mkdir(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"mkdir"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn config(&self) -> CommandConfig {
|
|
|
|
let mut named: IndexMap<String, NamedType> = IndexMap::new();
|
2019-08-07 18:40:38 +00:00
|
|
|
named.insert("create-all".to_string(), NamedType::Switch);
|
2019-08-07 02:45:38 +00:00
|
|
|
|
|
|
|
CommandConfig {
|
|
|
|
name: self.name().to_string(),
|
|
|
|
positional: vec![PositionalType::mandatory("file", SyntaxType::Path)],
|
|
|
|
rest_positional: false,
|
|
|
|
named,
|
|
|
|
is_sink: false,
|
|
|
|
is_filter: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mkdir(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
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 18:40:38 +00:00
|
|
|
if !args.has("create-all") {
|
2019-08-07 02:45:38 +00:00
|
|
|
match std::fs::create_dir(full_path) {
|
|
|
|
Err(_) => Err(ShellError::labeled_error(
|
|
|
|
"No such file or directory",
|
|
|
|
"No such file or directory",
|
|
|
|
args.nth(0).unwrap().span(),
|
|
|
|
)),
|
|
|
|
Ok(_) => Ok(OutputStream::empty()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|