[WIP] add input function and ask user whether he wants to override files

This commit is contained in:
Leonard Steppy 2025-01-08 18:34:59 +01:00
parent effe1418eb
commit 8f9ae82e6a

View File

@ -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::<Result<Vec<_>, _>>()
.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::<Result<Vec<_>, _>>()
.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<S>(prompt: S) -> String where S: Into<String> {
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())