From: Gabriel Goller <g.goller@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH v4 proxmox 1/2] router: cli: added `ask_for_confirmation` helper
Date: Thu, 7 Dec 2023 13:51:25 +0100 [thread overview]
Message-ID: <20231207125126.201502-2-g.goller@proxmox.com> (raw)
In-Reply-To: <20231207125126.201502-1-g.goller@proxmox.com>
Added `ask_for_confirmation` helper that outputs a prompt and
lets the user confirm or deny it.
Implemented to close #4763.
Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
---
proxmox-router/Cargo.toml | 1 +
proxmox-router/src/cli/mod.rs | 46 ++++++++++++++++++++++++++++++++++-
2 files changed, 46 insertions(+), 1 deletion(-)
diff --git a/proxmox-router/Cargo.toml b/proxmox-router/Cargo.toml
index c53ccd0..cd9140d 100644
--- a/proxmox-router/Cargo.toml
+++ b/proxmox-router/Cargo.toml
@@ -19,6 +19,7 @@ percent-encoding.workspace = true
serde_json.workspace = true
serde.workspace = true
unicode-width ="0.1.8"
+regex.workspace = true
# cli:
tokio = { workspace = true, features = [], optional = true }
diff --git a/proxmox-router/src/cli/mod.rs b/proxmox-router/src/cli/mod.rs
index 208df4a..2628538 100644
--- a/proxmox-router/src/cli/mod.rs
+++ b/proxmox-router/src/cli/mod.rs
@@ -12,7 +12,10 @@
//! - Ability to create interactive commands (using ``rustyline``)
//! - Supports complex/nested commands
-use std::collections::HashMap;
+use std::{
+ collections::HashMap,
+ io::{self, Write},
+};
use crate::ApiMethod;
@@ -62,6 +65,47 @@ pub fn init_cli_logger(env_var_name: &str, default_log_level: &str) {
.init();
}
+pub enum DefaultAnswer {
+ Yes,
+ No,
+}
+
+/// Prints a prompt to ask for confirmation
+pub fn ask_for_confirmation(query: String, default: DefaultAnswer) -> Result<bool, io::Error> {
+ let yesnoprompt: (char, char) = match default {
+ DefaultAnswer::Yes => ('Y', 'n'),
+ DefaultAnswer::No => ('y', 'N'),
+ };
+ print!("{query} [{}/{}]: ", yesnoprompt.0, yesnoprompt.1);
+
+ io::stdout().flush()?;
+ let stdin = io::stdin();
+ let mut line = String::new();
+ stdin.read_line(&mut line)?;
+
+ use regex::Regex;
+ match default {
+ DefaultAnswer::Yes => {
+ // unwrap is okay, because this regex is correct
+ let no_regex: Regex = Regex::new("^[nN]").unwrap();
+ if no_regex.is_match(line.trim()) {
+ Ok(false)
+ } else {
+ Ok(true)
+ }
+ }
+ DefaultAnswer::No => {
+ // unwrap is okay, because this regex is coorrect
+ let yes_regex: Regex = Regex::new("^[yY]").unwrap();
+ if yes_regex.is_match(line.trim()) {
+ Ok(true)
+ } else {
+ Ok(false)
+ }
+ }
+ }
+}
+
/// Define a simple CLI command.
pub struct CliCommand {
/// The Schema definition.
--
2.39.2
next prev parent reply other threads:[~2023-12-07 12:51 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-07 12:51 [pbs-devel] [PATCH v4 proxmox{, -backup} 0/2] close #4763: client: added command to forget backup group Gabriel Goller
2023-12-07 12:51 ` Gabriel Goller [this message]
2023-12-07 12:51 ` [pbs-devel] [PATCH v4 proxmox-backup 2/2] " Gabriel Goller
2024-04-09 9:02 ` [pbs-devel] [PATCH v4 proxmox{, -backup} 0/2] " Gabriel Goller
2024-04-17 14:15 ` Christian Ebner
2024-04-17 14:26 ` Christian Ebner
2024-04-18 9:13 ` Gabriel Goller
2024-04-18 10:24 ` Christian Ebner
2024-04-18 11:49 ` Gabriel Goller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231207125126.201502-2-g.goller@proxmox.com \
--to=g.goller@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.