nushell/src/commands/rm.rs

137 lines
4.2 KiB
Rust
Raw Normal View History

use crate::commands::command::RunnablePerItemContext;
2019-07-17 21:51:18 +02:00
use crate::errors::ShellError;
use crate::parser::hir::SyntaxType;
use crate::parser::registry::{CommandRegistry, Signature};
use crate::prelude::*;
use crate::utils::FileStructure;
2019-08-02 21:15:07 +02:00
use std::path::PathBuf;
2019-07-17 21:51:18 +02:00
pub struct Remove;
#[derive(Deserialize)]
pub struct RemoveArgs {
2019-08-21 14:08:23 +02:00
target: Tagged<PathBuf>,
recursive: Tagged<bool>,
}
2019-08-15 07:02:02 +02:00
impl PerItemCommand for Remove {
2019-07-17 21:51:18 +02:00
fn name(&self) -> &str {
"rm"
}
2019-08-02 21:15:07 +02:00
fn signature(&self) -> Signature {
Signature::build("rm")
.required("path", SyntaxType::Path)
.switch("recursive")
}
2019-07-17 21:51:18 +02:00
2019-08-02 21:15:07 +02:00
fn run(
&self,
2019-08-15 07:02:02 +02:00
call_info: &CallInfo,
_registry: &CommandRegistry,
shell_manager: &ShellManager,
_input: Tagged<Value>,
) -> Result<VecDeque<ReturnValue>, ShellError> {
call_info.process(shell_manager, rm)?.run()
2019-07-17 21:51:18 +02:00
}
}
2019-08-21 14:08:23 +02:00
fn rm(
RemoveArgs { target, recursive }: RemoveArgs,
RunnablePerItemContext { name, .. }: &RunnablePerItemContext,
2019-08-15 07:02:02 +02:00
) -> Result<VecDeque<ReturnValue>, ShellError> {
2019-08-21 14:08:23 +02:00
let path = target.item.clone();
let name_span = name;
2019-07-17 21:51:18 +02:00
2019-08-21 14:08:23 +02:00
let file = path.to_string_lossy();
if file == "." || file == ".." {
return Err(ShellError::labeled_error(
"Remove aborted. \".\" or \"..\" may not be removed.",
"Remove aborted. \".\" or \"..\" may not be removed.",
2019-08-21 14:08:23 +02:00
target.span(),
));
}
let entries: Vec<_> = match glob::glob(&path.to_string_lossy()) {
Ok(files) => files.collect(),
Err(_) => {
return Err(ShellError::labeled_error(
"Invalid pattern.",
"Invalid pattern.",
2019-08-21 14:08:23 +02:00
target.tag,
))
}
};
if entries.len() == 1 {
if let Ok(entry) = &entries[0] {
if entry.is_dir() {
let mut source_dir: FileStructure = FileStructure::new();
source_dir.walk_decorate(&entry)?;
2019-08-21 14:08:23 +02:00
if source_dir.contains_files() && !recursive.item {
return Err(ShellError::labeled_error(
2019-08-21 14:08:23 +02:00
format!("{:?} is a directory. Try using \"--recursive\".", file),
format!("{:?} is a directory. Try using \"--recursive\".", file),
target.span(),
));
}
}
}
}
for entry in entries {
match entry {
Ok(path) => {
let path_file_name = {
2019-08-21 14:08:23 +02:00
match path.file_name() {
Some(name) => PathBuf::from(name),
None => {
return Err(ShellError::labeled_error(
"Remove aborted. Not a valid path",
"Remove aborted. Not a valid path",
name_span,
))
}
}
};
let mut source_dir: FileStructure = FileStructure::new();
source_dir.walk_decorate(&path)?;
2019-08-21 14:08:23 +02:00
if source_dir.contains_more_than_one_file() && !recursive.item {
return Err(ShellError::labeled_error(
format!(
"Directory {:?} found somewhere inside. Try using \"--recursive\".",
path_file_name
),
format!(
"Directory {:?} found somewhere inside. Try using \"--recursive\".",
path_file_name
),
2019-08-21 14:08:23 +02:00
target.span(),
));
}
if path.is_dir() {
std::fs::remove_dir_all(&path)?;
} else if path.is_file() {
std::fs::remove_file(&path)?;
}
}
Err(e) => {
return Err(ShellError::labeled_error(
format!("Remove aborted. {:}", e.to_string()),
format!("Remove aborted. {:}", e.to_string()),
name_span,
))
}
2019-07-17 21:51:18 +02:00
}
}
2019-08-15 07:02:02 +02:00
Ok(VecDeque::new())
2019-07-17 21:51:18 +02:00
}