all lists on lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox] router: cli: added `ask_for_confirmation` helper
@ 2023-08-30 12:45 Gabriel Goller
  2023-08-30 12:45 ` [pbs-devel] [PATCH proxmox-backup v2] close #4763: client: added command to forget backup group Gabriel Goller
  2023-09-07 15:26 ` [pbs-devel] [PATCH proxmox] router: cli: added `ask_for_confirmation` helper Thomas Lamprecht
  0 siblings, 2 replies; 5+ messages in thread
From: Gabriel Goller @ 2023-08-30 12:45 UTC (permalink / raw)
  To: pbs-devel

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>
---

Note: A dependency bump is needed.

 proxmox-router/src/cli/mod.rs | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/proxmox-router/src/cli/mod.rs b/proxmox-router/src/cli/mod.rs
index 208df4a..2366b47 100644
--- a/proxmox-router/src/cli/mod.rs
+++ b/proxmox-router/src/cli/mod.rs
@@ -12,11 +12,15 @@
 //! - 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;
 
 mod environment;
+use anyhow::bail;
 pub use environment::*;
 
 mod shellword;
@@ -62,6 +66,19 @@ pub fn init_cli_logger(env_var_name: &str, default_log_level: &str) {
     .init();
 }
 
+pub fn ask_for_confirmation(query: String) -> Result<(), anyhow::Error> {
+    print!("{} [y/N]: ", query);
+    io::stdout().flush()?;
+    let stdin = io::stdin();
+    let mut line = String::new();
+    stdin.read_line(&mut line)?;
+    if line.trim() == "y" {
+        Ok(())
+    } else {
+        bail!("Aborted");
+    }
+}
+
 /// Define a simple CLI command.
 pub struct CliCommand {
     /// The Schema definition.
-- 
2.39.2





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-09-08 11:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-30 12:45 [pbs-devel] [PATCH proxmox] router: cli: added `ask_for_confirmation` helper Gabriel Goller
2023-08-30 12:45 ` [pbs-devel] [PATCH proxmox-backup v2] close #4763: client: added command to forget backup group Gabriel Goller
2023-09-07 15:26 ` [pbs-devel] [PATCH proxmox] router: cli: added `ask_for_confirmation` helper Thomas Lamprecht
2023-09-08 10:39   ` Gabriel Goller
2023-09-08 11:44   ` Gabriel Goller

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.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal