Merge pull request 'Ask user whether they want to override already existig files when editing' (#12) from 11-add-confirmation-dialogue-when-a-file-to-edit-already-exist-in-the-working-directory into master
Reviewed-on: https://stupstech.de/dev/Mr_Steppy/multi-ssh/pulls/12
This commit is contained in:
commit
96ac4d792f
68
src/main.rs
68
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";
|
||||
@ -119,6 +119,22 @@ enum OldVersionPolicy {
|
||||
Delete,
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! input {
|
||||
($prompt: tt) => {{
|
||||
print!($prompt);
|
||||
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.trim().to_string()
|
||||
}};
|
||||
() => {
|
||||
input!()
|
||||
};
|
||||
}
|
||||
|
||||
fn main() -> Result<(), String> {
|
||||
let args = Args::parse();
|
||||
|
||||
@ -250,13 +266,7 @@ fn main() -> Result<(), String> {
|
||||
}
|
||||
|
||||
if !no_confirm {
|
||||
log!(logger, "Continue? [Y|n] ");
|
||||
std::io::stdout().flush().expect("failed to flush stdout");
|
||||
let mut buffer = String::new();
|
||||
std::io::stdin()
|
||||
.read_line(&mut buffer)
|
||||
.expect("failed to read stdin");
|
||||
match buffer.to_lowercase().trim() {
|
||||
match input!("Continue? [Y|n] ").to_lowercase().as_str() {
|
||||
"n" | "no" => {
|
||||
log!(logger, "Aborting...");
|
||||
return Ok(());
|
||||
@ -333,19 +343,35 @@ 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!(
|
||||
"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()
|
||||
));
|
||||
'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)
|
||||
{
|
||||
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 {
|
||||
match input!("{duplication_notification}. Do you want to replace it? [N|y] ")
|
||||
.to_lowercase()
|
||||
.as_str()
|
||||
{
|
||||
"y" | "yes" => break 'duplicate_check,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
return Err(format!(
|
||||
"{duplication_notification}. You can override it with --override or -f"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
require_non_empty_servers(&servers)?;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user