From bd7016edf93cf24fb8ba76a96e3b79870eefbde6 Mon Sep 17 00:00:00 2001 From: mace Date: Mon, 12 Oct 2026 15:27:51 +0200 Subject: [PATCH] Ask before stopping the docker container on down (default yes) --- src/main.rs | 11 +++++++- src/prompt.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/prompt.rs diff --git a/src/main.rs b/src/main.rs index 4d43548..07fb5f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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"; - start_docker_compose(&config.container_service, &config.container_dir, start)?; + 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); diff --git a/src/prompt.rs b/src/prompt.rs new file mode 100644 index 0000000..cfea7f7 --- /dev/null +++ b/src/prompt.rs @@ -0,0 +1,70 @@ +use std::io::{BufRead, Write}; + +use anyhow::Result; + +fn parse_answer(answer: &str) -> Option { + 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 { + 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] "); + } +}