char/dns-ez

cloudflare dns management via plaintext zonefiles

git clone https://git.t4t.associates/char/dns-ez

Charlotte Sommove to anyhow for error handling736d1a4

main
1.0 KiB31 linesraw
1use std::io::{self, IsTerminal, Write};
2
3use crate::reconcile::Action;
4
5/// Ask a yes/no question on stdin; anything but "y"/"yes" is a no.
6pub fn prompt(prompt: &str) -> bool {
7    print!("{prompt}");
8    let _ = io::stdout().flush();
9    let mut input = String::new();
10    let _ = io::stdin().read_line(&mut input);
11    matches!(input.trim().to_ascii_lowercase().as_str(), "y" | "yes")
12}
13
14/// Print an action line, color-coded if stdout is a terminal.
15pub fn print_action(action: &Action, prune: bool) {
16    let (code, text) = match action {
17        Action::Create(_) => (32, action.describe()),
18        Action::Update { .. } => (33, action.describe()),
19        Action::Delete(_) if !prune => (31, format!("{} (skipped: --no-prune)", action.describe())),
20        Action::Delete(_) => (31, action.describe()),
21    };
22    println!("{}", paint(code, &text));
23}
24
25fn paint(code: u8, text: &str) -> String {
26    if io::stdout().is_terminal() {
27        format!("\x1b[{code}m{text}\x1b[0m")
28    } else {
29        text.to_string()
30    }
31}