diff options
author | Nora <nora.widdecke@tu-bs.de> | 2019-02-22 21:38:58 +0100 |
---|---|---|
committer | Nora <nora.widdecke@tu-bs.de> | 2019-02-22 21:38:58 +0100 |
commit | fda005c16988545b6e4c3d52a5eba096651ee91b (patch) | |
tree | 3fe73a563e7c487bc719a39995374c362cda4a55 /src/actions | |
parent | bfb6d7159c4b419bc6098af37df6894bd31d6e8a (diff) |
undo: ask before overriding
Diffstat (limited to 'src/actions')
-rw-r--r-- | src/actions/undo.rs | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/src/actions/undo.rs b/src/actions/undo.rs index d5b0415..460e391 100644 --- a/src/actions/undo.rs +++ b/src/actions/undo.rs @@ -4,6 +4,7 @@ use crate::input; use crate::utils::fileutil; use crate::utils::misc; use crate::KhResult; +use crate::utils::stdioutils; use std::fs; use std::path::{Path, PathBuf}; @@ -27,6 +28,8 @@ pub fn do_undo(_args: &[&str]) -> KhResult<()> { let source_dir = dirs.pop().unwrap().to_path_buf(); let backup_id = source_dir.strip_prefix(backupdir)?; + info!("Restoring {:?}", backup_id); + let files: Vec<PathBuf> = WalkDir::new(source_dir.clone()) .into_iter() .flatten() @@ -41,20 +44,28 @@ pub fn do_undo(_args: &[&str]) -> KhResult<()> { let mut target_path = caldir.clone(); target_path.push(path_in_cal); - if target_path.exists() { - //ask + if target_path.exists() && !ask_overwrite(&target_path) { + info!("ignoring {}", target_path.display()); } else { fs::copy(file_path.clone(), target_path.clone())?; + info!("Restore {} to {}", file_path.display(), target_path.display()); } - println!("{}", target_path.display()); - println!("{}", file_path.display()); } - println!("{:?}", backup_id); Ok(()) } +fn ask_overwrite(path: &Path) -> bool { + println!("File exists:\n{}", path.display()); + println!("Overwrite? y/n:"); + + match stdioutils::read_single_char_from_stdin().unwrap() { + 'y' => true, + _ => false + } +} + #[cfg(test)] mod integration { use super::*; |