nushell/src/commands/mkdir.rs

46 lines
1.2 KiB
Rust
Raw Normal View History

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)?;
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()?)),
_ => {}
}
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
}
}