nushell/src/commands/mkdir.rs

73 lines
1.7 KiB
Rust
Raw Normal View History

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::*;
use std::path::PathBuf;
2019-08-07 02:45:38 +00:00
pub struct Mkdir;
#[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,
_registry: &CommandRegistry,
2019-08-15 05:02:02 +00:00
shell_manager: &ShellManager,
_input: Tagged<Value>,
2019-08-15 05:02:02 +00:00
) -> Result<VecDeque<ReturnValue>, ShellError> {
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 {
Signature::build("mkdir").rest()
2019-08-07 02:45:38 +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> {
let full_path = PathBuf::from(shell_manager.path());
2019-08-09 04:51:21 +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
}
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
}
Ok(VecDeque::new())
2019-08-07 02:45:38 +00:00
}