nushell/crates/nu-command/src/filesystem/mv.rs

140 lines
4.6 KiB
Rust
Raw Normal View History

2021-10-04 13:32:08 +02:00
use std::env::current_dir;
use std::path::{Path, PathBuf};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{ShellError, Signature, SyntaxShape, Value};
pub struct Mv;
impl Command for Mv {
fn name(&self) -> &str {
"mv"
}
fn usage(&self) -> &str {
"Move files or directories."
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("mv")
2021-10-04 14:13:47 +02:00
.required(
2021-10-04 13:32:08 +02:00
"source",
SyntaxShape::GlobPattern,
"the location to move files/directories from",
)
2021-10-04 14:13:47 +02:00
.required(
2021-10-04 13:32:08 +02:00
"destination",
2021-10-05 07:08:15 +02:00
SyntaxShape::Filepath,
2021-10-04 13:32:08 +02:00
"the location to move files/directories to",
)
}
fn run(
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
2021-10-12 19:44:23 +02:00
// TODO: handle invalid directory or insufficient permissions when moving
2021-10-04 13:32:08 +02:00
let source: String = call.req(context, 0)?;
let destination: String = call.req(context, 1)?;
let path: PathBuf = current_dir().unwrap();
let source = path.join(source.as_str());
let destination = path.join(destination.as_str());
let mut sources =
glob::glob(&source.to_string_lossy()).map_or_else(|_| Vec::new(), Iterator::collect);
if sources.is_empty() {
2021-10-05 05:43:07 +02:00
return Err(ShellError::FileNotFound(
call.positional.first().unwrap().span,
));
2021-10-04 13:32:08 +02:00
}
if (destination.exists() && !destination.is_dir() && sources.len() > 1)
|| (!destination.exists() && sources.len() > 1)
{
2021-10-05 05:43:07 +02:00
return Err(ShellError::MoveNotPossible {
source_message: "Can't move many files".to_string(),
source_span: call.positional[0].span,
destination_message: "into single file".to_string(),
destination_span: call.positional[1].span,
});
2021-10-04 13:32:08 +02:00
}
let some_if_source_is_destination = sources
.iter()
.find(|f| matches!(f, Ok(f) if destination.starts_with(f)));
if destination.exists() && destination.is_dir() && sources.len() == 1 {
2021-10-05 05:43:07 +02:00
if let Some(Ok(_filename)) = some_if_source_is_destination {
return Err(ShellError::MoveNotPossible {
source_message: "Can't move directory".to_string(),
source_span: call.positional[0].span,
destination_message: "into itself".to_string(),
destination_span: call.positional[1].span,
});
2021-10-04 13:32:08 +02:00
}
}
if let Some(Ok(_filename)) = some_if_source_is_destination {
sources = sources
.into_iter()
.filter(|f| matches!(f, Ok(f) if !destination.starts_with(f)))
.collect();
}
for entry in sources.into_iter().flatten() {
2021-10-05 05:43:07 +02:00
move_file(call, &entry, &destination)?
2021-10-04 13:32:08 +02:00
}
Ok(Value::Nothing { span: call.head })
}
}
2021-10-05 06:40:26 +02:00
fn move_file(call: &Call, from: &Path, to: &Path) -> Result<(), ShellError> {
2021-10-04 13:32:08 +02:00
if to.exists() && from.is_dir() && to.is_file() {
2021-10-05 05:43:07 +02:00
return Err(ShellError::MoveNotPossible {
source_message: "Can't move a directory".to_string(),
source_span: call.positional[0].span,
destination_message: "to a file".to_string(),
destination_span: call.positional[1].span,
});
2021-10-04 13:32:08 +02:00
}
let destination_dir_exists = if to.is_dir() {
true
} else {
to.parent().map(Path::exists).unwrap_or(true)
};
if !destination_dir_exists {
2021-10-05 05:43:07 +02:00
return Err(ShellError::DirectoryNotFound(call.positional[1].span));
2021-10-04 13:32:08 +02:00
}
2021-10-05 06:40:26 +02:00
let mut to = to.to_path_buf();
2021-10-04 13:32:08 +02:00
if to.is_dir() {
let from_file_name = match from.file_name() {
Some(name) => name,
2021-10-05 05:43:07 +02:00
None => return Err(ShellError::DirectoryNotFound(call.positional[1].span)),
2021-10-04 13:32:08 +02:00
};
to.push(from_file_name);
}
2021-10-05 06:40:26 +02:00
move_item(call, from, &to)
2021-10-04 13:32:08 +02:00
}
2021-10-05 05:43:07 +02:00
fn move_item(call: &Call, from: &Path, to: &Path) -> Result<(), ShellError> {
2021-10-04 13:32:08 +02:00
// We first try a rename, which is a quick operation. If that doesn't work, we'll try a copy
// and remove the old file/folder. This is necessary if we're moving across filesystems or devices.
2021-10-05 06:40:26 +02:00
std::fs::rename(&from, &to).map_err(|_| ShellError::MoveNotPossible {
source_message: "failed to move".to_string(),
source_span: call.positional[0].span,
destination_message: "into".to_string(),
destination_span: call.positional[1].span,
2021-10-04 13:32:08 +02:00
})
}