Merge pull request #320 from burrbull/fix-mv

fix target_file check in mv
This commit is contained in:
Sagie Gur-Ari 2023-04-22 22:37:24 +03:00 committed by GitHub
commit 6902f9cf45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,7 +2,7 @@ use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use fs_extra::{dir, move_items};
use fsio;
use std::fs;
use fsio::directory::create_parent;
use std::path::Path;
#[cfg(test)]
@ -42,11 +42,23 @@ impl Command for CommandImpl {
} else {
let target_path = Path::new(&arguments[1]);
let source_file = source_path.is_file();
let target_file = target_path.is_file();
let target_file = if target_path.exists() {
target_path.is_file()
} else {
!target_path.ends_with("/")
&& !target_path.ends_with("\\")
&& target_path.extension().is_some()
};
if source_file && target_file {
match fs::rename(&arguments[0], &arguments[1]) {
Ok(_) => CommandResult::Continue(Some("true".to_string())),
match create_parent(&target_path) {
Ok(_) => {
let options = fs_extra::file::CopyOptions::new().overwrite(true);
match fs_extra::file::move_file(source_path, &target_path, &options) {
Ok(_) => CommandResult::Continue(Some("true".to_string())),
Err(error) => CommandResult::Error(error.to_string()),
}
}
Err(error) => CommandResult::Error(error.to_string()),
}
} else {