2019-08-20 11:20:48 +00:00
|
|
|
use crate::commands::command::RunnablePerItemContext;
|
2019-07-17 19:51:18 +00:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::parser::hir::SyntaxType;
|
2019-08-20 11:20:48 +00:00
|
|
|
use crate::parser::registry::{CommandRegistry, Signature};
|
2019-07-20 02:27:10 +00:00
|
|
|
use crate::prelude::*;
|
2019-08-20 11:20:48 +00:00
|
|
|
use crate::utils::FileStructure;
|
2019-08-02 19:15:07 +00:00
|
|
|
use std::path::PathBuf;
|
2019-07-17 19:51:18 +00:00
|
|
|
|
|
|
|
pub struct Remove;
|
|
|
|
|
2019-08-20 11:20:48 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct RemoveArgs {
|
2019-08-21 12:08:23 +00:00
|
|
|
target: Tagged<PathBuf>,
|
2019-08-20 11:20:48 +00:00
|
|
|
recursive: Tagged<bool>,
|
|
|
|
}
|
|
|
|
|
2019-08-15 05:02:02 +00:00
|
|
|
impl PerItemCommand for Remove {
|
2019-07-17 19:51:18 +00:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"rm"
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:15:07 +00:00
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("rm")
|
|
|
|
.required("path", SyntaxType::Path)
|
|
|
|
.switch("recursive")
|
|
|
|
}
|
2019-07-17 19:51:18 +00:00
|
|
|
|
2019-08-02 19:15:07 +00:00
|
|
|
fn run(
|
|
|
|
&self,
|
2019-08-15 05:02:02 +00:00
|
|
|
call_info: &CallInfo,
|
|
|
|
_registry: &CommandRegistry,
|
|
|
|
shell_manager: &ShellManager,
|
|
|
|
_input: Tagged<Value>,
|
|
|
|
) -> Result<VecDeque<ReturnValue>, ShellError> {
|
2019-08-20 11:20:48 +00:00
|
|
|
call_info.process(shell_manager, rm)?.run()
|
2019-07-17 19:51:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 12:08:23 +00:00
|
|
|
fn rm(
|
|
|
|
RemoveArgs { target, recursive }: RemoveArgs,
|
|
|
|
RunnablePerItemContext { name, .. }: &RunnablePerItemContext,
|
2019-08-15 05:02:02 +00:00
|
|
|
) -> Result<VecDeque<ReturnValue>, ShellError> {
|
2019-08-21 12:08:23 +00:00
|
|
|
let path = target.item.clone();
|
|
|
|
let name_span = name;
|
2019-07-17 19:51:18 +00:00
|
|
|
|
2019-08-21 12:08:23 +00:00
|
|
|
let file = path.to_string_lossy();
|
2019-08-01 21:55:49 +00:00
|
|
|
|
2019-08-20 11:20:48 +00:00
|
|
|
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 12:08:23 +00:00
|
|
|
target.span(),
|
2019-08-20 11:20:48 +00:00
|
|
|
));
|
2019-08-01 21:55:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 11:20:48 +00:00
|
|
|
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 12:08:23 +00:00
|
|
|
target.tag,
|
2019-08-20 11:20:48 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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 12:08:23 +00:00
|
|
|
if source_dir.contains_files() && !recursive.item {
|
2019-08-20 11:20:48 +00:00
|
|
|
return Err(ShellError::labeled_error(
|
2019-08-21 12:08:23 +00:00
|
|
|
format!("{:?} is a directory. Try using \"--recursive\".", file),
|
|
|
|
format!("{:?} is a directory. Try using \"--recursive\".", file),
|
|
|
|
target.span(),
|
2019-08-20 11:20:48 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-01 21:55:49 +00:00
|
|
|
|
|
|
|
for entry in entries {
|
2019-08-01 21:00:08 +00:00
|
|
|
match entry {
|
|
|
|
Ok(path) => {
|
2019-08-20 11:20:48 +00:00
|
|
|
let path_file_name = {
|
2019-08-21 12:08:23 +00:00
|
|
|
match path.file_name() {
|
2019-08-20 11:20:48 +00:00
|
|
|
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,
|
|
|
|
))
|
|
|
|
}
|
2019-08-01 21:00:08 +00:00
|
|
|
}
|
2019-08-20 11:20:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut source_dir: FileStructure = FileStructure::new();
|
|
|
|
|
|
|
|
source_dir.walk_decorate(&path)?;
|
|
|
|
|
2019-08-21 12:08:23 +00:00
|
|
|
if source_dir.contains_more_than_one_file() && !recursive.item {
|
2019-08-20 11:20:48 +00:00
|
|
|
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 12:08:23 +00:00
|
|
|
target.span(),
|
2019-08-20 11:20:48 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if path.is_dir() {
|
|
|
|
std::fs::remove_dir_all(&path)?;
|
2019-08-01 21:00:08 +00:00
|
|
|
} else if path.is_file() {
|
2019-08-20 11:20:48 +00:00
|
|
|
std::fs::remove_file(&path)?;
|
2019-08-01 21:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-20 11:20:48 +00:00
|
|
|
Err(e) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
format!("Remove aborted. {:}", e.to_string()),
|
|
|
|
format!("Remove aborted. {:}", e.to_string()),
|
|
|
|
name_span,
|
|
|
|
))
|
|
|
|
}
|
2019-07-17 19:51:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 05:02:02 +00:00
|
|
|
Ok(VecDeque::new())
|
2019-07-17 19:51:18 +00:00
|
|
|
}
|