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
46
src/main.rs
46
src/main.rs
@ -19,7 +19,7 @@ use std::io::Write;
|
|||||||
use std::iter::once;
|
use std::iter::once;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::{env, fs};
|
use std::{env, fs, io};
|
||||||
|
|
||||||
const SERVERS_ENV_VAR: &str = "MSSH_SERVERS";
|
const SERVERS_ENV_VAR: &str = "MSSH_SERVERS";
|
||||||
const EDITOR_ENV_VAR: &str = "MSSH_EDITOR";
|
const EDITOR_ENV_VAR: &str = "MSSH_EDITOR";
|
||||||
@ -119,6 +119,22 @@ enum OldVersionPolicy {
|
|||||||
Delete,
|
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> {
|
fn main() -> Result<(), String> {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
@ -250,13 +266,7 @@ fn main() -> Result<(), String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !no_confirm {
|
if !no_confirm {
|
||||||
log!(logger, "Continue? [Y|n] ");
|
match input!("Continue? [Y|n] ").to_lowercase().as_str() {
|
||||||
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() {
|
|
||||||
"n" | "no" => {
|
"n" | "no" => {
|
||||||
log!(logger, "Aborting...");
|
log!(logger, "Aborting...");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@ -333,6 +343,7 @@ fn main() -> Result<(), String> {
|
|||||||
let file_name = file
|
let file_name = file
|
||||||
.file_name()
|
.file_name()
|
||||||
.ok_or("can only edit files, not directories")?;
|
.ok_or("can only edit files, not directories")?;
|
||||||
|
'duplicate_check: {
|
||||||
if !override_existing
|
if !override_existing
|
||||||
&& fs::read_dir(&working_directory)
|
&& fs::read_dir(&working_directory)
|
||||||
.map_err(|e| format!("failed to open working directory: {e}"))?
|
.map_err(|e| format!("failed to open working directory: {e}"))?
|
||||||
@ -341,12 +352,27 @@ fn main() -> Result<(), String> {
|
|||||||
.iter()
|
.iter()
|
||||||
.any(|entry| entry.file_name() == file_name)
|
.any(|entry| entry.file_name() == file_name)
|
||||||
{
|
{
|
||||||
return Err(format!(
|
let duplication_notification = format!(
|
||||||
"A file with the name {} already exists in {}. You can override it with --override or -f",
|
"A file with the name {} already exists in {}",
|
||||||
file_name.to_string_lossy(),
|
file_name.to_string_lossy(),
|
||||||
working_directory.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)?;
|
require_non_empty_servers(&servers)?;
|
||||||
start_ssh_agent(&logger)?;
|
start_ssh_agent(&logger)?;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user