mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
feat: create "resolve_path" for smart_unpack to deal with rename
This commit is contained in:
parent
63e8549408
commit
8b84038cec
@ -34,6 +34,24 @@ pub fn clear_path(path: &Path, question_policy: QuestionPolicy) -> crate::Result
|
|||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn resolve_path(path: &Path, question_policy: QuestionPolicy) -> crate::Result<Option<PathBuf>> {
|
||||||
|
if path.exists() {
|
||||||
|
match user_wants_to_overwrite(path, question_policy)? {
|
||||||
|
FileConflitOperation::Cancel => return Ok(None),
|
||||||
|
FileConflitOperation::Overwrite => {
|
||||||
|
remove_file_or_dir(path)?;
|
||||||
|
Ok(Some(path.to_path_buf()))
|
||||||
|
},
|
||||||
|
FileConflitOperation::Rename => {
|
||||||
|
let renamed_path = rename_for_available_filename(path);
|
||||||
|
Ok(Some(renamed_path))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Ok(Some(path.to_path_buf()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn remove_file_or_dir(path: &Path) -> crate::Result<()> {
|
pub fn remove_file_or_dir(path: &Path) -> crate::Result<()> {
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
fs::remove_dir_all(path)?;
|
fs::remove_dir_all(path)?;
|
||||||
|
@ -46,16 +46,13 @@ pub enum FileConflitOperation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Check if QuestionPolicy flags were set, otherwise, ask user if they want to overwrite.
|
/// Check if QuestionPolicy flags were set, otherwise, ask user if they want to overwrite.
|
||||||
pub fn user_wants_to_overwrite(path: &Path, question_policy: QuestionPolicy) -> crate::Result<bool> {
|
pub fn user_wants_to_overwrite(path: &Path, question_policy: QuestionPolicy) -> crate::Result<FileConflitOperation> {
|
||||||
|
use FileConflitOperation as Op;
|
||||||
|
|
||||||
match question_policy {
|
match question_policy {
|
||||||
QuestionPolicy::AlwaysYes => Ok(true),
|
QuestionPolicy::AlwaysYes => Ok(Op::Overwrite),
|
||||||
QuestionPolicy::AlwaysNo => Ok(false),
|
QuestionPolicy::AlwaysNo => Ok(Op::Cancel),
|
||||||
QuestionPolicy::Ask => {
|
QuestionPolicy::Ask => ask_file_conflict_operation(path),
|
||||||
let path = path_to_str(strip_cur_dir(path));
|
|
||||||
let path = Some(&*path);
|
|
||||||
let placeholder = Some("FILE");
|
|
||||||
Confirmation::new("Do you want to overwrite 'FILE'?", placeholder).ask(path)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user