From: Gabriel Goller <g.goller@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox v5 1/2] router: cli: added `ask_for_confirmation` helper
Date: Thu, 18 Apr 2024 13:49:52 +0200 [thread overview]
Message-ID: <20240418114957.186561-2-g.goller@proxmox.com> (raw)
In-Reply-To: <20240418114957.186561-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 dcd71a4..0b9d361 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 7df94ad..7f87284 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;
@@ -61,6 +64,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.43.0
_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
next prev parent reply other threads:[~2024-04-18 11:50 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-18 11:49 [pbs-devel] [PATCH proxmox{, -backup} v5 0/2] close #4763: client: added command to forget backup group Gabriel Goller
2024-04-18 11:49 ` Gabriel Goller [this message]
2024-04-24 19:03 ` [pbs-devel] [PATCH proxmox v5 1/2] router: cli: added `ask_for_confirmation` helper Thomas Lamprecht
2024-04-25 8:52 ` Gabriel Goller
2024-04-25 9:42 ` Thomas Lamprecht
2024-04-25 10:37 ` Gabriel Goller
2024-04-25 11:32 ` Thomas Lamprecht
2024-04-18 11:49 ` [pbs-devel] [PATCH proxmox-backup v5 2/2] close #4763: client: add command to forget backup group Gabriel Goller
2024-04-26 12:37 ` [pbs-devel] [PATCH proxmox{, -backup} v5 0/2] close #4763: client: added " 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=20240418114957.186561-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox