1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// SPDX-License-Identifier: Apache-2.0
//! Shared interactive-confirmation helpers for consequential CLI writes.
//!
//! Originally lived privately in [`crate::cli::security_cmd`] for the
//! `yolo` / `secure` master switches (#507). Promoted to a shared module for
//! #852 so the same governance — *show the consequence, then require explicit
//! approval* — guards every state-mutating write that can clobber existing
//! state (knowledge overwrites, behavior-changing `config set`), not just the
//! security toggles.
//!
//! Invariant: with no TTY and no `--yes` we **refuse** rather than silently
//! apply. An automated/unattended run must never weaken state without an
//! explicit opt-in flag.
use std::io::Write;
const BOLD: &str = "\x1b[1m";
const YELLOW: &str = "\x1b[33m";
const RST: &str = "\x1b[0m";
/// True if the user passed an explicit approval flag (`-y`, `--yes`,
/// `--force`, `-f`). These bypass the interactive prompt for scripts/CI.
pub(crate) fn wants_yes(args: &[String]) -> bool {
args.iter()
.any(|a| matches!(a.as_str(), "-y" | "--yes" | "--force" | "-f"))
}
/// Confirm a consequential change.
///
/// - `assume_yes` short-circuits to `true` (for `--yes` / scripted use).
/// - On a TTY we prompt `[y/N]`: `y`/`yes` is a yes, `n`/`no` or an empty
/// line (Enter) a no. Any other answer is no answer: it is named and the
/// question is asked again, so a mistyped key never counts as a no
/// without a word (rc4-63). After [`MAX_ATTEMPTS`] unclear answers, or
/// when input ends, the answer is no and the person is told so.
/// - With **no** TTY and **no** `--yes` we print a refusal hint and return
/// `false`, so the caller leaves state untouched.
pub(crate) fn confirm(prompt: &str, assume_yes: bool) -> bool {
if assume_yes {
return true;
}
if !lean_ctx::core::host_env::stdin_is_terminal() {
eprintln!(
"{YELLOW}Refusing to apply a consequential change non-interactively.{RST} Re-run with {BOLD}--yes{RST} to confirm."
);
return false;
}
for _ in 0..MAX_ATTEMPTS {
print!("{prompt} [y/N] ");
let _ = std::io::stdout().flush();
let mut input = String::new();
match std::io::stdin().read_line(&mut input) {
Ok(0) | Err(_) => {
println!();
println!("No answer, so that is a no.");
return false;
}
Ok(_) => {}
}
match answer(&input) {
Some(yes) => return yes,
None => println!(
"\"{}\" is not an answer. Type y for yes or n for no.",
shown(&input)
),
}
}
println!("No clear answer, so that is a no.");
false
}
/// How often an unclear answer is asked again before it counts as a no.
const MAX_ATTEMPTS: usize = 3;
/// A typed line as an answer: `Some(true)` for y/yes, `Some(false)` for
/// n/no or Enter alone, `None` for anything else.
fn answer(input: &str) -> Option<bool> {
match input.trim().to_ascii_lowercase().as_str() {
"y" | "yes" => Some(true),
"" | "n" | "no" => Some(false),
_ => None,
}
}
/// The unclear answer as it is shown back: printable, at most 20 chars.
fn shown(input: &str) -> String {
input
.trim()
.chars()
.filter(|c| !c.is_control())
.take(20)
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wants_yes_detects_flags() {
assert!(wants_yes(&["--yes".to_string()]));
assert!(wants_yes(&["-y".to_string()]));
assert!(wants_yes(&["--force".to_string()]));
assert!(wants_yes(&["-f".to_string()]));
assert!(!wants_yes(&["open".to_string()]));
assert!(!wants_yes(&[]));
}
#[test]
fn only_yes_and_no_are_answers() {
assert_eq!(answer("y\n"), Some(true));
assert_eq!(answer(" YES "), Some(true));
assert_eq!(answer("n\n"), Some(false));
assert_eq!(answer("No"), Some(false));
assert_eq!(answer("\n"), Some(false));
// rc4-63: a stray key is no answer, so the question is asked again.
assert_eq!(answer("a\n"), None);
assert_eq!(answer("ja"), None);
assert_eq!(answer("yy"), None);
}
#[test]
fn an_unclear_answer_is_shown_back_printable_and_short() {
assert_eq!(shown("a\n"), "a");
assert_eq!(shown("\x1b[Aab"), "[Aab");
assert_eq!(shown(&"x".repeat(50)).len(), 20);
}
#[test]
fn confirm_assume_yes_short_circuits() {
assert!(confirm("anything", true));
}
}