2019-07-22 04:23:02 +02:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::parser::hir::SyntaxType;
|
|
|
|
use crate::parser::registry::{CommandConfig, NamedType, PositionalType};
|
|
|
|
use crate::prelude::*;
|
|
|
|
use indexmap::IndexMap;
|
2019-08-07 19:49:11 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-07-22 04:23:02 +02:00
|
|
|
|
|
|
|
pub struct Copycp;
|
|
|
|
|
|
|
|
impl Command for Copycp {
|
|
|
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
cp(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"cp"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn config(&self) -> CommandConfig {
|
|
|
|
let mut named: IndexMap<String, NamedType> = IndexMap::new();
|
|
|
|
named.insert("recursive".to_string(), NamedType::Switch);
|
|
|
|
|
|
|
|
CommandConfig {
|
|
|
|
name: self.name().to_string(),
|
|
|
|
positional: vec![PositionalType::mandatory("file", SyntaxType::Path)],
|
|
|
|
rest_positional: false,
|
|
|
|
named,
|
|
|
|
is_sink: false,
|
|
|
|
is_filter: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
|
|
|
|
pub struct Res {
|
|
|
|
pub loc: PathBuf,
|
|
|
|
pub at: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Res {}
|
|
|
|
|
|
|
|
pub struct FileStructure {
|
|
|
|
root: PathBuf,
|
|
|
|
resources: Vec<Res>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileStructure {
|
|
|
|
pub fn new() -> FileStructure {
|
|
|
|
FileStructure {
|
|
|
|
root: PathBuf::new(),
|
|
|
|
resources: Vec::<Res>::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_root(&mut self, path: &Path) {
|
|
|
|
self.root = path.to_path_buf();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn translate<F>(&mut self, to: F) -> Vec<(PathBuf, PathBuf)>
|
|
|
|
where
|
|
|
|
F: Fn((PathBuf, usize)) -> (PathBuf, PathBuf),
|
|
|
|
{
|
|
|
|
self.resources
|
|
|
|
.iter()
|
|
|
|
.map(|f| (PathBuf::from(&f.loc), f.at))
|
|
|
|
.map(|f| to(f))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn walk_decorate(&mut self, start_path: &Path) {
|
|
|
|
self.set_root(&dunce::canonicalize(start_path).unwrap());
|
|
|
|
self.resources = Vec::<Res>::new();
|
|
|
|
self.build(start_path, 0);
|
|
|
|
self.resources.sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build(&mut self, src: &'a Path, lvl: usize) {
|
|
|
|
let source = dunce::canonicalize(src).unwrap();
|
|
|
|
|
|
|
|
if source.is_dir() {
|
|
|
|
for entry in std::fs::read_dir(&source).unwrap() {
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
let path = entry.path();
|
|
|
|
|
|
|
|
if path.is_dir() {
|
|
|
|
self.build(&path, lvl + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.resources.push(Res {
|
|
|
|
loc: path.to_path_buf(),
|
|
|
|
at: lvl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.resources.push(Res {
|
|
|
|
loc: source,
|
|
|
|
at: lvl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 04:23:02 +02:00
|
|
|
pub fn cp(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2019-08-07 19:49:11 +02:00
|
|
|
let mut source = PathBuf::from(args.shell_manager.path());
|
|
|
|
let mut destination = PathBuf::from(args.shell_manager.path());
|
2019-08-07 16:45:50 +02:00
|
|
|
let name_span = args.call_info.name_span;
|
2019-07-22 04:23:02 +02:00
|
|
|
|
|
|
|
match args
|
|
|
|
.nth(0)
|
|
|
|
.ok_or_else(|| ShellError::string(&format!("No file or directory specified")))?
|
|
|
|
.as_string()?
|
2019-07-23 07:51:22 +02:00
|
|
|
.as_str()
|
|
|
|
{
|
|
|
|
file => {
|
2019-07-22 04:23:02 +02:00
|
|
|
source.push(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match args
|
|
|
|
.nth(1)
|
|
|
|
.ok_or_else(|| ShellError::string(&format!("No file or directory specified")))?
|
|
|
|
.as_string()?
|
2019-07-23 07:51:22 +02:00
|
|
|
.as_str()
|
|
|
|
{
|
|
|
|
file => {
|
2019-07-22 04:23:02 +02:00
|
|
|
destination.push(file);
|
2019-07-23 07:51:22 +02:00
|
|
|
}
|
2019-07-22 04:23:02 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
let sources = glob::glob(&source.to_string_lossy());
|
2019-08-06 09:05:47 +02:00
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
if sources.is_err() {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Invalid pattern.",
|
|
|
|
"Invalid pattern.",
|
|
|
|
args.nth(0).unwrap().span(),
|
|
|
|
));
|
2019-08-06 09:05:47 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
let sources: Vec<_> = sources.unwrap().collect();
|
2019-08-06 09:05:47 +02:00
|
|
|
|
|
|
|
if sources.len() == 1 {
|
2019-08-07 16:45:50 +02:00
|
|
|
if let Ok(val) = &sources[0] {
|
|
|
|
if val.is_dir() && !args.has("recursive") {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"is a directory (not copied). Try using \"--recursive\".",
|
|
|
|
"is a directory (not copied). Try using \"--recursive\".",
|
|
|
|
args.nth(0).unwrap().span(),
|
|
|
|
));
|
|
|
|
}
|
2019-08-06 09:05:47 +02:00
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
let mut sources: FileStructure = FileStructure::new();
|
|
|
|
|
|
|
|
sources.walk_decorate(&val);
|
|
|
|
|
|
|
|
if val.is_file() {
|
|
|
|
for (ref src, ref dst) in sources.translate(|(src, _)| {
|
|
|
|
if destination.exists() {
|
|
|
|
let mut dst = dunce::canonicalize(destination.clone()).unwrap();
|
|
|
|
dst.push(val.file_name().unwrap());
|
|
|
|
(src, dst)
|
2019-08-06 09:05:47 +02:00
|
|
|
} else {
|
2019-08-07 16:45:50 +02:00
|
|
|
(src, destination.clone())
|
|
|
|
}
|
|
|
|
}) {
|
|
|
|
if src.is_file() {
|
|
|
|
match std::fs::copy(src, dst) {
|
|
|
|
Err(e) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
e.to_string(),
|
|
|
|
e.to_string(),
|
|
|
|
name_span,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(o) => o,
|
|
|
|
};
|
2019-08-06 09:05:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
if val.is_dir() {
|
|
|
|
if !destination.exists() {
|
|
|
|
match std::fs::create_dir_all(&destination) {
|
|
|
|
Err(e) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
e.to_string(),
|
|
|
|
e.to_string(),
|
|
|
|
name_span,
|
|
|
|
));
|
2019-08-06 09:05:47 +02:00
|
|
|
}
|
2019-08-07 16:45:50 +02:00
|
|
|
Ok(o) => o,
|
|
|
|
};
|
2019-08-06 09:05:47 +02:00
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
for (ref src, ref dst) in sources.translate(|(src, loc)| {
|
|
|
|
let mut final_path = destination.clone();
|
|
|
|
let path = dunce::canonicalize(&src).unwrap();
|
2019-08-06 09:05:47 +02:00
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
let mut comps: Vec<_> = path
|
|
|
|
.components()
|
|
|
|
.map(|fragment| fragment.as_os_str())
|
|
|
|
.rev()
|
|
|
|
.take(1 + loc)
|
|
|
|
.collect();
|
2019-08-06 09:05:47 +02:00
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
comps.reverse();
|
|
|
|
|
|
|
|
for fragment in comps.iter() {
|
|
|
|
final_path.push(fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
(PathBuf::from(&src), PathBuf::from(final_path))
|
|
|
|
}) {
|
|
|
|
if src.is_dir() {
|
|
|
|
if !dst.exists() {
|
|
|
|
match std::fs::create_dir_all(dst) {
|
|
|
|
Err(e) => {
|
2019-08-06 09:05:47 +02:00
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
e.to_string(),
|
|
|
|
e.to_string(),
|
2019-08-07 16:45:50 +02:00
|
|
|
name_span,
|
2019-08-06 09:05:47 +02:00
|
|
|
));
|
|
|
|
}
|
2019-08-07 16:45:50 +02:00
|
|
|
Ok(o) => o,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if src.is_file() {
|
|
|
|
match std::fs::copy(src, dst) {
|
|
|
|
Err(e) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
e.to_string(),
|
|
|
|
e.to_string(),
|
|
|
|
name_span,
|
|
|
|
));
|
2019-08-06 09:05:47 +02:00
|
|
|
}
|
|
|
|
Ok(o) => o,
|
|
|
|
};
|
2019-08-07 16:45:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
destination.push(val.file_name().unwrap());
|
2019-08-06 09:05:47 +02:00
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
match std::fs::create_dir_all(&destination) {
|
|
|
|
Err(e) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
e.to_string(),
|
|
|
|
e.to_string(),
|
|
|
|
name_span,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(o) => o,
|
|
|
|
};
|
2019-08-06 09:05:47 +02:00
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
for (ref src, ref dst) in sources.translate(|(src, loc)| {
|
|
|
|
let mut final_path = dunce::canonicalize(&destination).unwrap();
|
|
|
|
let path = dunce::canonicalize(&src).unwrap();
|
2019-08-06 09:05:47 +02:00
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
let mut comps: Vec<_> = path
|
|
|
|
.components()
|
|
|
|
.map(|fragment| fragment.as_os_str())
|
|
|
|
.rev()
|
|
|
|
.take(1 + loc)
|
|
|
|
.collect();
|
2019-08-06 09:05:47 +02:00
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
comps.reverse();
|
2019-08-06 09:05:47 +02:00
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
for fragment in comps.iter() {
|
|
|
|
final_path.push(fragment);
|
|
|
|
}
|
2019-08-06 09:05:47 +02:00
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
(PathBuf::from(&src), PathBuf::from(final_path))
|
|
|
|
}) {
|
|
|
|
if src.is_dir() {
|
|
|
|
if !dst.exists() {
|
|
|
|
match std::fs::create_dir_all(dst) {
|
|
|
|
Err(e) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
e.to_string(),
|
|
|
|
e.to_string(),
|
|
|
|
name_span,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(o) => o,
|
|
|
|
};
|
2019-08-06 09:05:47 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 16:45:50 +02:00
|
|
|
|
|
|
|
if src.is_file() {
|
|
|
|
match std::fs::copy(src, dst) {
|
|
|
|
Err(e) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
e.to_string(),
|
|
|
|
e.to_string(),
|
|
|
|
name_span,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(o) => o,
|
|
|
|
};
|
|
|
|
}
|
2019-08-06 09:05:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 16:45:50 +02:00
|
|
|
} else {
|
|
|
|
if destination.exists() {
|
|
|
|
if !sources.iter().all(|x| (x.as_ref().unwrap()).is_file()) && !args.has("recursive") {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Copy aborted (directories found). Try using \"--recursive\".",
|
|
|
|
"Copy aborted (directories found). Try using \"--recursive\".",
|
|
|
|
args.nth(0).unwrap().span(),
|
|
|
|
));
|
2019-08-06 09:05:47 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
for entry in sources {
|
|
|
|
if let Ok(entry) = entry {
|
|
|
|
let mut to = PathBuf::from(&destination);
|
|
|
|
to.push(&entry.file_name().unwrap());
|
2019-08-06 09:05:47 +02:00
|
|
|
|
2019-08-07 16:45:50 +02:00
|
|
|
match std::fs::copy(&entry, &to) {
|
|
|
|
Err(e) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
e.to_string(),
|
|
|
|
e.to_string(),
|
|
|
|
name_span,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(o) => o,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
format!(
|
|
|
|
"Copy aborted. (Does {:?} exist?)",
|
|
|
|
&destination.file_name().unwrap()
|
|
|
|
),
|
|
|
|
format!(
|
|
|
|
"Copy aborted. (Does {:?} exist?)",
|
|
|
|
&destination.file_name().unwrap()
|
|
|
|
),
|
|
|
|
args.nth(1).unwrap().span(),
|
|
|
|
));
|
2019-07-22 04:23:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 09:05:47 +02:00
|
|
|
Ok(OutputStream::empty())
|
2019-07-23 07:51:22 +02:00
|
|
|
}
|
2019-08-07 16:45:50 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::{FileStructure, Res};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
fn fixtures() -> PathBuf {
|
|
|
|
let mut sdx = PathBuf::new();
|
|
|
|
sdx.push("tests");
|
|
|
|
sdx.push("fixtures");
|
|
|
|
sdx.push("formats");
|
|
|
|
dunce::canonicalize(sdx).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn prepares_and_decorates_source_files_for_copying() {
|
|
|
|
let mut res = FileStructure::new();
|
|
|
|
res.walk_decorate(fixtures().as_path());
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
res.resources,
|
|
|
|
vec![
|
|
|
|
Res {
|
|
|
|
loc: fixtures().join("appveyor.yml"),
|
|
|
|
at: 0
|
|
|
|
},
|
|
|
|
Res {
|
|
|
|
loc: fixtures().join("caco3_plastics.csv"),
|
|
|
|
at: 0
|
|
|
|
},
|
|
|
|
Res {
|
|
|
|
loc: fixtures().join("cargo_sample.toml"),
|
|
|
|
at: 0
|
|
|
|
},
|
|
|
|
Res {
|
|
|
|
loc: fixtures().join("jonathan.xml"),
|
|
|
|
at: 0
|
|
|
|
},
|
|
|
|
Res {
|
|
|
|
loc: fixtures().join("sample.ini"),
|
|
|
|
at: 0
|
|
|
|
},
|
|
|
|
Res {
|
|
|
|
loc: fixtures().join("sgml_description.json"),
|
|
|
|
at: 0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|