add move, recursive fill, and recursive create procedures

This commit is contained in:
xiuxiu62 2021-10-05 14:08:39 -07:00
parent 8dc3ebd6e2
commit 74d4c501a8
3 changed files with 109 additions and 77 deletions

View File

@ -44,7 +44,7 @@ impl Command for Cp {
let source = path.join(source.as_str()); let source = path.join(source.as_str());
let destination = path.join(destination.as_str()); let destination = path.join(destination.as_str());
let mut sources = let sources =
glob::glob(&source.to_string_lossy()).map_or_else(|_| Vec::new(), Iterator::collect); glob::glob(&source.to_string_lossy()).map_or_else(|_| Vec::new(), Iterator::collect);
if sources.is_empty() { if sources.is_empty() {
return Err(ShellError::FileNotFound(call.positional[0].span)); return Err(ShellError::FileNotFound(call.positional[0].span));
@ -60,10 +60,7 @@ impl Command for Cp {
} }
let any_source_is_dir = sources.iter().any(|f| matches!(f, Ok(f) if f.is_dir())); let any_source_is_dir = sources.iter().any(|f| matches!(f, Ok(f) if f.is_dir()));
let recursive = call let recursive = call.named.iter().any(|p| &p.0 == "recursive");
.named
.iter()
.fold(false, |acc, p| acc || { &p.0 == "recursive" });
if any_source_is_dir && !recursive { if any_source_is_dir && !recursive {
return Err(ShellError::MoveNotPossibleSingle( return Err(ShellError::MoveNotPossibleSingle(
"Directories must be copied using \"--recursive\"".to_string(), "Directories must be copied using \"--recursive\"".to_string(),
@ -71,84 +68,106 @@ impl Command for Cp {
)); ));
} }
// for entry in sources.into_iter().flatten() { for entry in sources.into_iter().flatten() {
// let mut sources = FileStructure::new(); let mut sources = FileStructure::new();
// sources.walk_decorate(&entry)?; sources.walk_decorate(&entry)?;
// if entry.is_file() { if entry.is_file() {
// let sources = sources.paths_applying_with(|(source_file, _depth_level)| { let sources = sources.paths_applying_with(|(source_file, _depth_level)| {
// if destination.is_dir() { if destination.is_dir() {
// let mut dest = canonicalize_with(&destination.item, &path)?; let mut dest = canonicalize_with(&destination, &path)?;
// if let Some(name) = entry.file_name() { if let Some(name) = entry.file_name() {
// dest.push(name); dest.push(name);
// } }
// Ok((source_file, dest)) Ok((source_file, dest))
// } else { } else {
// Ok((source_file, destination.clone())) Ok((source_file, destination.clone()))
// } }
// })?; })?;
// for (src, dst) in sources { for (src, dst) in sources {
// if src.is_file() { if src.is_file() {
// std::fs::copy(src, dst).map_err(|e| { std::fs::copy(&src, dst).map_err(|e| {
// ShellError::labeled_error(e.to_string(), e.to_string(), &name_tag) ShellError::MoveNotPossibleSingle(
// })?; format!(
// } "failed to move containing file \"{}\": {}",
// } src.to_string_lossy(),
// } else if entry.is_dir() { e
// let destination = if !destination.exists() { ),
// destination.clone() call.positional[0].span,
// } else { )
// match entry.file_name() { })?;
// Some(name) => destination.join(name), }
// None => { }
// return Err(ShellError::labeled_error( } else if entry.is_dir() {
// "Copy aborted. Not a valid path", let destination = if !destination.exists() {
// "not a valid path", destination.clone()
// dst.tag, } else {
// )) match entry.file_name() {
// } Some(name) => destination.join(name),
// } None => {
// }; return Err(ShellError::FileNotFoundCustom(
format!("containing \"{:?}\" is not a valid path", entry),
call.positional[0].span,
))
}
}
};
// std::fs::create_dir_all(&destination).map_err(|e| { std::fs::create_dir_all(&destination).map_err(|e| {
// ShellError::labeled_error(e.to_string(), e.to_string(), &dst.tag) ShellError::MoveNotPossibleSingle(
// })?; format!("failed to recursively fill destination: {}", e),
call.positional[1].span,
)
})?;
// let sources = sources.paths_applying_with(|(source_file, depth_level)| { let sources = sources.paths_applying_with(|(source_file, depth_level)| {
// let mut dest = destination.clone(); let mut dest = destination.clone();
// let path = canonicalize_with(&source_file, &path)?; let path = canonicalize_with(&source_file, &path)?;
// let comps: Vec<_> = path let comps: Vec<_> = path
// .components() .components()
// .map(|fragment| fragment.as_os_str()) .map(|fragment| fragment.as_os_str())
// .rev() .rev()
// .take(1 + depth_level) .take(1 + depth_level)
// .collect(); .collect();
// for fragment in comps.into_iter().rev() { for fragment in comps.into_iter().rev() {
// dest.push(fragment); dest.push(fragment);
// } }
// Ok((PathBuf::from(&source_file), dest)) Ok((PathBuf::from(&source_file), dest))
// })?; })?;
// let dst_tag = &dst.tag; for (src, dst) in sources {
// for (src, dst) in sources { if src.is_dir() && !dst.exists() {
// if src.is_dir() && !dst.exists() { std::fs::create_dir_all(&dst).map_err(|e| {
// std::fs::create_dir_all(&dst).map_err(|e| { ShellError::MoveNotPossibleSingle(
// ShellError::labeled_error(e.to_string(), e.to_string(), dst_tag) format!(
// })?; "failed to create containing directory \"{}\": {}",
// } dst.to_string_lossy(),
e
),
call.positional[1].span,
)
})?;
}
// if src.is_file() { if src.is_file() {
// std::fs::copy(&src, &dst).map_err(|e| { std::fs::copy(&src, &dst).map_err(|e| {
// ShellError::labeled_error(e.to_string(), e.to_string(), &name_tag) ShellError::MoveNotPossibleSingle(
// })?; format!(
// } "failed to move containing file \"{}\": {}",
// } src.to_string_lossy(),
// } e
// } ),
call.positional[0].span,
)
})?;
}
}
}
}
Ok(Value::Nothing { span: call.head }) Ok(Value::Nothing { span: call.head })
} }

View File

@ -8,17 +8,16 @@ pub struct FileStructure {
pub resources: Vec<Resource>, pub resources: Vec<Resource>,
} }
#[allow(dead_code)]
impl FileStructure { impl FileStructure {
pub fn new() -> FileStructure { pub fn new() -> FileStructure {
FileStructure { resources: vec![] } FileStructure { resources: vec![] }
} }
#[allow(dead_code)]
pub fn contains_more_than_one_file(&self) -> bool { pub fn contains_more_than_one_file(&self) -> bool {
self.resources.len() > 1 self.resources.len() > 1
} }
#[allow(dead_code)]
pub fn contains_files(&self) -> bool { pub fn contains_files(&self) -> bool {
!self.resources.is_empty() !self.resources.is_empty()
} }
@ -33,7 +32,7 @@ impl FileStructure {
self.resources self.resources
.iter() .iter()
.map(|f| (PathBuf::from(&f.location), f.at)) .map(|f| (PathBuf::from(&f.location), f.at))
.map(|f| to(f)) .map(to)
.collect() .collect()
} }

View File

@ -83,10 +83,18 @@ pub enum ShellError {
#[diagnostic(code(nu::shell::file_not_found), url(docsrs))] #[diagnostic(code(nu::shell::file_not_found), url(docsrs))]
FileNotFound(#[label("file not found")] Span), FileNotFound(#[label("file not found")] Span),
#[error("File not found")]
#[diagnostic(code(nu::shell::file_not_found), url(docsrs))]
FileNotFoundCustom(String, #[label("{0}")] Span),
#[error("Directory not found")] #[error("Directory not found")]
#[diagnostic(code(nu::shell::directory_not_found), url(docsrs))] #[diagnostic(code(nu::shell::directory_not_found), url(docsrs))]
DirectoryNotFound(#[label("directory not found")] Span), DirectoryNotFound(#[label("directory not found")] Span),
#[error("File not found")]
#[diagnostic(code(nu::shell::file_not_found), url(docsrs))]
DirectoryNotFoundCustom(String, #[label("{0}")] Span),
#[error("Move not possible")] #[error("Move not possible")]
#[diagnostic(code(nu::shell::move_not_possible), url(docsrs))] #[diagnostic(code(nu::shell::move_not_possible), url(docsrs))]
MoveNotPossible { MoveNotPossible {
@ -109,6 +117,12 @@ impl From<std::io::Error> for ShellError {
} }
} }
impl std::convert::From<Box<dyn std::error::Error>> for ShellError {
fn from(input: Box<dyn std::error::Error>) -> ShellError {
ShellError::InternalError(input.to_string())
}
}
impl From<Box<dyn std::error::Error + Send + Sync>> for ShellError { impl From<Box<dyn std::error::Error + Send + Sync>> for ShellError {
fn from(input: Box<dyn std::error::Error + Send + Sync>) -> ShellError { fn from(input: Box<dyn std::error::Error + Send + Sync>) -> ShellError {
ShellError::InternalError(format!("{:?}", input)) ShellError::InternalError(format!("{:?}", input))