mirror of
https://github.com/nushell/nushell.git
synced 2025-04-24 05:08:29 +02:00
Appropiate error handling when copying (thanks @jonathandturner)
This commit is contained in:
parent
2da43f4b06
commit
0790a714b0
@ -32,20 +32,18 @@ impl Command for Copycp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn cp(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
pub fn cp(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
let mut source = args.env.lock().unwrap().path().to_path_buf();
|
let mut source = args.env.lock().unwrap().path().to_path_buf();
|
||||||
let mut destination = args.env.lock().unwrap().path().to_path_buf();
|
let mut destination = args.env.lock().unwrap().path().to_path_buf();
|
||||||
|
|
||||||
let mut src = String::new();
|
|
||||||
let mut dst = String::new();
|
let mut dst = String::new();
|
||||||
|
|
||||||
match args
|
match args
|
||||||
.nth(0)
|
.nth(0)
|
||||||
.ok_or_else(|| ShellError::string(&format!("No file or directory specified")))?
|
.ok_or_else(|| ShellError::string(&format!("No file or directory specified")))?
|
||||||
.as_string()?
|
.as_string()?
|
||||||
.as_str() {
|
.as_str()
|
||||||
|
{
|
||||||
file => {
|
file => {
|
||||||
src.push_str(file);
|
|
||||||
source.push(file);
|
source.push(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,12 +52,12 @@ pub fn cp(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|||||||
.nth(1)
|
.nth(1)
|
||||||
.ok_or_else(|| ShellError::string(&format!("No file or directory specified")))?
|
.ok_or_else(|| ShellError::string(&format!("No file or directory specified")))?
|
||||||
.as_string()?
|
.as_string()?
|
||||||
.as_str() {
|
.as_str()
|
||||||
|
{
|
||||||
file => {
|
file => {
|
||||||
dst.push_str(file);
|
dst.push_str(file);
|
||||||
destination.push(file);
|
destination.push(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if destination.is_dir() {
|
if destination.is_dir() {
|
||||||
@ -68,13 +66,15 @@ pub fn cp(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|||||||
let file_name = file_name.to_str().expect("");
|
let file_name = file_name.to_str().expect("");
|
||||||
destination.push(Path::new(file_name));
|
destination.push(Path::new(file_name));
|
||||||
} else if source.is_dir() {
|
} else if source.is_dir() {
|
||||||
return Err(ShellError::string(
|
return Err(ShellError::string(&format!(
|
||||||
&format!("{:?} is a directory (not copied)", src))
|
"{:?} is a directory (not copied)",
|
||||||
);
|
source.to_string_lossy()
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::fs::copy(source, destination).expect("can not copy file");
|
match std::fs::copy(source, destination) {
|
||||||
|
Err(_error) => Err(ShellError::string("can not copy file")),
|
||||||
Ok(OutputStream::empty())
|
Ok(_) => Ok(OutputStream::empty()),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user