[WIP] add input function and ask user whether he wants to override files
This commit is contained in:
parent
effe1418eb
commit
8f9ae82e6a
38
src/main.rs
38
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";
|
||||||
@ -333,19 +333,29 @@ 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")?;
|
||||||
if !override_existing
|
'duplicate_check: {
|
||||||
&& fs::read_dir(&working_directory)
|
if !override_existing
|
||||||
.map_err(|e| format!("failed to open working directory: {e}"))?
|
&& fs::read_dir(&working_directory)
|
||||||
.collect::<Result<Vec<_>, _>>()
|
.map_err(|e| format!("failed to open working directory: {e}"))?
|
||||||
.map_err(|e| format!("error while querying working directory contents: {e}"))?
|
.collect::<Result<Vec<_>, _>>()
|
||||||
.iter()
|
.map_err(|e| format!("error while querying working directory contents: {e}"))?
|
||||||
.any(|entry| entry.file_name() == file_name)
|
.iter()
|
||||||
{
|
.any(|entry| entry.file_name() == file_name)
|
||||||
return Err(format!(
|
{
|
||||||
|
//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",
|
"A file with the name {} already exists in {}. You can override it with --override or -f",
|
||||||
file_name.to_string_lossy(),
|
file_name.to_string_lossy(),
|
||||||
working_directory.to_string_lossy()
|
working_directory.to_string_lossy()
|
||||||
));
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
require_non_empty_servers(&servers)?;
|
require_non_empty_servers(&servers)?;
|
||||||
@ -387,6 +397,14 @@ fn main() -> Result<(), String> {
|
|||||||
Ok(())
|
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> {
|
fn require_non_empty_servers(servers: &[Server]) -> Result<(), String> {
|
||||||
if servers.is_empty() {
|
if servers.is_empty() {
|
||||||
Err("You did not provide any servers for this operation. Please see --help".to_string())
|
Err("You did not provide any servers for this operation. Please see --help".to_string())
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user