From 8f9ae82e6a6635db7e617d03f58321816ea46cde Mon Sep 17 00:00:00 2001 From: Leonard Steppy Date: Wed, 8 Jan 2025 18:34:59 +0100 Subject: [PATCH] [WIP] add input function and ask user whether he wants to override files --- src/main.rs | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index b9918a4..7bb2d88 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ use std::io::Write; use std::iter::once; use std::path::PathBuf; use std::str::FromStr; -use std::{env, fs}; +use std::{env, fs, io}; const SERVERS_ENV_VAR: &str = "MSSH_SERVERS"; const EDITOR_ENV_VAR: &str = "MSSH_EDITOR"; @@ -333,19 +333,29 @@ fn main() -> Result<(), String> { let file_name = file .file_name() .ok_or("can only edit files, not directories")?; - if !override_existing - && fs::read_dir(&working_directory) - .map_err(|e| format!("failed to open working directory: {e}"))? - .collect::, _>>() - .map_err(|e| format!("error while querying working directory contents: {e}"))? - .iter() - .any(|entry| entry.file_name() == file_name) - { - return Err(format!( + 'duplicate_check: { + if !override_existing + && fs::read_dir(&working_directory) + .map_err(|e| format!("failed to open working directory: {e}"))? + .collect::, _>>() + .map_err(|e| format!("error while querying working directory contents: {e}"))? + .iter() + .any(|entry| entry.file_name() == file_name) + { + //TODO ask user whether they want to override, unless silent flag is set + let duplication_notification = format!("A file with the name {} already exists in {}", file_name.to_string_lossy(), working_directory.to_string_lossy()); + + if !args.quiet { + print!("{duplication_notification}. Do you want to replace it? [N|y]"); + + } + + return Err(format!( "A file with the name {} already exists in {}. You can override it with --override or -f", file_name.to_string_lossy(), working_directory.to_string_lossy() )); + } } require_non_empty_servers(&servers)?; @@ -387,6 +397,14 @@ fn main() -> Result<(), String> { Ok(()) } +fn get_user_input(prompt: S) -> String where S: Into { + print!("{}", prompt.into()); + io::stdout().flush().expect("failed to flush stdout"); + let mut buf = String::new(); + io::stdin().read_line(&mut buf).expect("failed to read stdin"); + buf +} + fn require_non_empty_servers(servers: &[Server]) -> Result<(), String> { if servers.is_empty() { Err("You did not provide any servers for this operation. Please see --help".to_string())