Ask before stopping the docker container on down (default yes)
This commit is contained in:
1 parent
e97205c4bf
commit
bd7016edf9
2 files changed
+79
No files matched your search
@@ -14,6 +14,7 @@ mod config;
|
||||
mod container;
|
||||
mod env;
|
||||
mod git;
|
||||
mod prompt;
|
||||
|
||||
/// Line-count summary by default, since a full diff can contain secrets
|
||||
/// (e.g. connection strings); `-v`/`--verbose` opts into the diff.
|
||||
@@ -69,7 +70,15 @@ fn main() -> Result<()> {
|
||||
let config: DevToolsConf = load_config(&args)?;
|
||||
let start: bool = args.action == "up";
|
||||
|
||||
let should_toggle_container = start
|
||||
|| prompt::confirm(
|
||||
"Stop the docker container?",
|
||||
&mut std::io::stdin().lock(),
|
||||
&mut std::io::stdout(),
|
||||
)?;
|
||||
if should_toggle_container {
|
||||
start_docker_compose(&config.container_service, &config.container_dir, start)?;
|
||||
}
|
||||
|
||||
if start {
|
||||
print_file_change(&set_local_db(start, &config)?, args.verbose);
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
use std::io::{BufRead, Write};
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
fn parse_answer(answer: &str) -> Option<bool> {
|
||||
match answer.trim().to_lowercase().as_str() {
|
||||
"" | "y" | "yes" => Some(true),
|
||||
"n" | "no" => Some(false),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Asks a yes/no question defaulting to yes; re-asks on unrecognized input.
|
||||
/// A closed input (EOF) is treated as accepting the default.
|
||||
pub fn confirm(question: &str, input: &mut impl BufRead, output: &mut impl Write) -> Result<bool> {
|
||||
loop {
|
||||
write!(output, "{} [Y/n] ", question)?;
|
||||
output.flush()?;
|
||||
|
||||
let mut answer = String::new();
|
||||
if input.read_line(&mut answer)? == 0 {
|
||||
return Ok(true);
|
||||
}
|
||||
if let Some(is_confirmed) = parse_answer(&answer) {
|
||||
return Ok(is_confirmed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn confirm_with_input(input: &str) -> (bool, String) {
|
||||
let mut output = Vec::new();
|
||||
let is_confirmed = confirm("Stop?", &mut input.as_bytes(), &mut output).unwrap();
|
||||
(is_confirmed, String::from_utf8(output).unwrap())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirm_defaults_to_yes_on_empty_answer() {
|
||||
assert_eq!(confirm_with_input("\n"), (true, "Stop? [Y/n] ".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirm_defaults_to_yes_on_eof() {
|
||||
assert!(confirm_with_input("").0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirm_accepts_yes_variants() {
|
||||
for answer in ["y\n", "Y\n", "yes\n", " YES \n"] {
|
||||
assert!(confirm_with_input(answer).0, "answer: {answer:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirm_accepts_no_variants() {
|
||||
for answer in ["n\n", "N\n", "no\n", " No \n"] {
|
||||
assert!(!confirm_with_input(answer).0, "answer: {answer:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirm_reasks_on_invalid_answer() {
|
||||
let (is_confirmed, output) = confirm_with_input("maybe\nn\n");
|
||||
assert!(!is_confirmed);
|
||||
assert_eq!(output, "Stop? [Y/n] Stop? [Y/n] ");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user