check for EOF when asking questions

when invoking Ouch from shell scripts, it is likely that questions will
be left unanswered with EOF, this commit fixes Ouch interpreting EOF as
Yes in Y/N questions
This commit is contained in:
João M. Bezerra 2022-11-23 00:30:35 -03:00
parent f4b029c1fd
commit c737956a4e
2 changed files with 13 additions and 2 deletions

View File

@ -14,7 +14,7 @@ use fs_err as fs;
use super::{strip_cur_dir, to_utf};
use crate::{
accessible::is_running_in_accessible_mode,
error::{Error, Result},
error::{Error, FinalError, Result},
utils::{self, colors},
};
@ -145,7 +145,17 @@ impl<'a> Confirmation<'a> {
io::stdout().flush()?;
let mut answer = String::new();
io::stdin().read_line(&mut answer)?;
let bytes_read = io::stdin().read_line(&mut answer)?;
if bytes_read == 0 {
let error = FinalError::with_title("Unexpected EOF when asking question.")
.detail("When asking the user:")
.detail(format!(" \"{message}\""))
.detail("Expected 'y' or 'n' as answer, but found EOF instead.")
.hint("If using Ouch in scripting, consider using `--yes` and `--no`.");
return Err(error.into());
}
answer.make_ascii_lowercase();
match answer.trim() {

View File

@ -9,6 +9,7 @@ macro_rules! ouch {
($($e:expr),*) => {
$crate::utils::cargo_bin()
$(.arg($e))*
.arg("--yes")
.unwrap();
}
}