Make more visible the strategies for figuring out where to copy files.

This commit is contained in:
Andrés N. Robalino 2019-08-07 13:40:38 -05:00
parent e0bacaaf37
commit 50393bdf42
4 changed files with 39 additions and 34 deletions

View File

@ -129,7 +129,7 @@ Nu adheres closely to a set of goals that make up its design philosophy. As feat
| cd path | Change to a new path | | cd path | Change to a new path |
| cp source path | Copy files | | cp source path | Copy files |
| ls (path) | View the contents of the current or given path | | ls (path) | View the contents of the current or given path |
| mkdir path | Make directories, (to create intermediary directories append '--p') | | mkdir path | Make directories, (to create intermediary directories append '--create-all') |
| date (--utc) | Get the current datetime | | date (--utc) | Get the current datetime |
| ps | View current processes | | ps | View current processes |
| sys | View information about the current system | | sys | View information about the current system |

View File

@ -56,7 +56,7 @@ impl FileStructure {
self.root = path.to_path_buf(); self.root = path.to_path_buf();
} }
pub fn translate<F>(&mut self, to: F) -> Vec<(PathBuf, PathBuf)> pub fn paths_applying_with<F>(&mut self, to: F) -> Vec<(PathBuf, PathBuf)>
where where
F: Fn((PathBuf, usize)) -> (PathBuf, PathBuf), F: Fn((PathBuf, usize)) -> (PathBuf, PathBuf),
{ {
@ -140,8 +140,8 @@ pub fn cp(args: CommandArgs) -> Result<OutputStream, ShellError> {
let sources: Vec<_> = sources.unwrap().collect(); let sources: Vec<_> = sources.unwrap().collect();
if sources.len() == 1 { if sources.len() == 1 {
if let Ok(val) = &sources[0] { if let Ok(entry) = &sources[0] {
if val.is_dir() && !args.has("recursive") { if entry.is_dir() && !args.has("recursive") {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"is a directory (not copied). Try using \"--recursive\".", "is a directory (not copied). Try using \"--recursive\".",
"is a directory (not copied). Try using \"--recursive\".", "is a directory (not copied). Try using \"--recursive\".",
@ -151,18 +151,21 @@ pub fn cp(args: CommandArgs) -> Result<OutputStream, ShellError> {
let mut sources: FileStructure = FileStructure::new(); let mut sources: FileStructure = FileStructure::new();
sources.walk_decorate(&val); sources.walk_decorate(&entry);
if val.is_file() { if entry.is_file() {
for (ref src, ref dst) in sources.translate(|(src, _)| {
let strategy = |(source_file, _depth_level)| {
if destination.exists() { if destination.exists() {
let mut dst = dunce::canonicalize(destination.clone()).unwrap(); let mut new_dst = dunce::canonicalize(destination.clone()).unwrap();
dst.push(val.file_name().unwrap()); new_dst.push(entry.file_name().unwrap());
(src, dst) (source_file, new_dst)
} else { } else {
(src, destination.clone()) (source_file, destination.clone())
} }
}) { };
for (ref src, ref dst) in sources.paths_applying_with(strategy) {
if src.is_file() { if src.is_file() {
match std::fs::copy(src, dst) { match std::fs::copy(src, dst) {
Err(e) => { Err(e) => {
@ -178,7 +181,7 @@ pub fn cp(args: CommandArgs) -> Result<OutputStream, ShellError> {
} }
} }
if val.is_dir() { if entry.is_dir() {
if !destination.exists() { if !destination.exists() {
match std::fs::create_dir_all(&destination) { match std::fs::create_dir_all(&destination) {
Err(e) => { Err(e) => {
@ -191,25 +194,27 @@ pub fn cp(args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(o) => o, Ok(o) => o,
}; };
for (ref src, ref dst) in sources.translate(|(src, loc)| { let strategy = |(source_file, depth_level)| {
let mut final_path = destination.clone(); let mut new_dst = destination.clone();
let path = dunce::canonicalize(&src).unwrap(); let path = dunce::canonicalize(&source_file).unwrap();
let mut comps: Vec<_> = path let mut comps: Vec<_> = path
.components() .components()
.map(|fragment| fragment.as_os_str()) .map(|fragment| fragment.as_os_str())
.rev() .rev()
.take(1 + loc) .take(1 + depth_level)
.collect(); .collect();
comps.reverse(); comps.reverse();
for fragment in comps.iter() { for fragment in comps.iter() {
final_path.push(fragment); new_dst.push(fragment);
} }
(PathBuf::from(&src), PathBuf::from(final_path)) (PathBuf::from(&source_file), PathBuf::from(new_dst))
}) { };
for (ref src, ref dst) in sources.paths_applying_with(strategy) {
if src.is_dir() { if src.is_dir() {
if !dst.exists() { if !dst.exists() {
match std::fs::create_dir_all(dst) { match std::fs::create_dir_all(dst) {
@ -239,7 +244,7 @@ pub fn cp(args: CommandArgs) -> Result<OutputStream, ShellError> {
} }
} }
} else { } else {
destination.push(val.file_name().unwrap()); destination.push(entry.file_name().unwrap());
match std::fs::create_dir_all(&destination) { match std::fs::create_dir_all(&destination) {
Err(e) => { Err(e) => {
@ -252,25 +257,27 @@ pub fn cp(args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(o) => o, Ok(o) => o,
}; };
for (ref src, ref dst) in sources.translate(|(src, loc)| { let strategy = |(source_file, depth_level)| {
let mut final_path = dunce::canonicalize(&destination).unwrap(); let mut new_dst = dunce::canonicalize(&destination).unwrap();
let path = dunce::canonicalize(&src).unwrap(); let path = dunce::canonicalize(&source_file).unwrap();
let mut comps: Vec<_> = path let mut comps: Vec<_> = path
.components() .components()
.map(|fragment| fragment.as_os_str()) .map(|fragment| fragment.as_os_str())
.rev() .rev()
.take(1 + loc) .take(1 + depth_level)
.collect(); .collect();
comps.reverse(); comps.reverse();
for fragment in comps.iter() { for fragment in comps.iter() {
final_path.push(fragment); new_dst.push(fragment);
} }
(PathBuf::from(&src), PathBuf::from(final_path)) (PathBuf::from(&source_file), PathBuf::from(new_dst))
}) { };
for (ref src, ref dst) in sources.paths_applying_with(strategy) {
if src.is_dir() { if src.is_dir() {
if !dst.exists() { if !dst.exists() {
match std::fs::create_dir_all(dst) { match std::fs::create_dir_all(dst) {

View File

@ -18,7 +18,7 @@ impl Command for Mkdir {
fn config(&self) -> CommandConfig { fn config(&self) -> CommandConfig {
let mut named: IndexMap<String, NamedType> = IndexMap::new(); let mut named: IndexMap<String, NamedType> = IndexMap::new();
named.insert("p".to_string(), NamedType::Switch); named.insert("create-all".to_string(), NamedType::Switch);
CommandConfig { CommandConfig {
name: self.name().to_string(), name: self.name().to_string(),
@ -32,16 +32,14 @@ impl Command for Mkdir {
} }
pub fn mkdir(args: CommandArgs) -> Result<OutputStream, ShellError> { pub fn mkdir(args: CommandArgs) -> Result<OutputStream, ShellError> {
let env = args.env.lock().unwrap(); let mut full_path = PathBuf::from(args.shell_manager.path());
let path = env.path.to_path_buf();
let mut full_path = PathBuf::from(path);
match &args.nth(0) { match &args.nth(0) {
Some(Tagged { item: value, .. }) => full_path.push(Path::new(&value.as_string()?)), Some(Tagged { item: value, .. }) => full_path.push(Path::new(&value.as_string()?)),
_ => {} _ => {}
} }
if !args.has("p") { if !args.has("create-all") {
match std::fs::create_dir(full_path) { match std::fs::create_dir(full_path) {
Err(_) => Err(ShellError::labeled_error( Err(_) => Err(ShellError::labeled_error(
"No such file or directory", "No such file or directory",

View File

@ -43,7 +43,7 @@ fn creates_intermediary_directories_with_p_flag() {
nu!( nu!(
_output, _output,
cwd(&full_path), cwd(&full_path),
"mkdir some_folder/another/deeper_one --p" "mkdir some_folder/another/deeper_one --create-all"
); );
let mut expected = PathBuf::from(full_path); let mut expected = PathBuf::from(full_path);