mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 11:35:45 +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)
|
||||
}
|
||||
|
||||
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<()> {
|
||||
if path.is_dir() {
|
||||
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.
|
||||
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 {
|
||||
QuestionPolicy::AlwaysYes => Ok(true),
|
||||
QuestionPolicy::AlwaysNo => Ok(false),
|
||||
QuestionPolicy::Ask => {
|
||||
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)
|
||||
}
|
||||
QuestionPolicy::AlwaysYes => Ok(Op::Overwrite),
|
||||
QuestionPolicy::AlwaysNo => Ok(Op::Cancel),
|
||||
QuestionPolicy::Ask => ask_file_conflict_operation(path),
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user