public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox-backup v2] cli: add interactive confirmation for block device wipe
@ 2023-11-29 13:20 Markus Frank
  2023-11-29 14:04 ` [pbs-devel] applied: " Thomas Lamprecht
  0 siblings, 1 reply; 2+ messages in thread
From: Markus Frank @ 2023-11-29 13:20 UTC (permalink / raw)
  To: pbs-devel

If stdin is a TTY, an interactive prompt is added to confirm the deletion
of a block device, ensuring user verification before proceeding.

Signed-off-by: Markus Frank <m.frank@proxmox.com>
---
v2:
* changed stdin_isatty() to std::io::stdin().is_terminal()
* removed unnecessary eprint
* do not loop if answer is not either y/Y or n/N
* moved err inline in bail

 src/bin/proxmox_backup_manager/disk.rs | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/src/bin/proxmox_backup_manager/disk.rs b/src/bin/proxmox_backup_manager/disk.rs
index 7a292098..e0056f6c 100644
--- a/src/bin/proxmox_backup_manager/disk.rs
+++ b/src/bin/proxmox_backup_manager/disk.rs
@@ -3,6 +3,7 @@ use serde_json::Value;
 
 use proxmox_router::{cli::*, ApiHandler, RpcEnvironment};
 use proxmox_schema::api;
+use std::io::{IsTerminal, Write};
 
 use pbs_api_types::{
     ZfsCompressionType, ZfsRaidLevel, BLOCKDEVICE_DISK_AND_PARTITION_NAME_SCHEMA,
@@ -152,6 +153,26 @@ async fn initialize_disk(
 async fn wipe_disk(mut param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
     param["node"] = "localhost".into();
 
+    // If we're on a TTY, query the user
+    if std::io::stdin().is_terminal() {
+        loop {
+            eprintln!("You are about to wipe block device {}.", param["disk"]);
+            eprint!("Are you sure you want to continue? (y/N): ");
+            let _ = std::io::stdout().flush();
+            use std::io::{BufRead, BufReader};
+            let mut line = String::new();
+            match BufReader::new(std::io::stdin()).read_line(&mut line) {
+                Ok(_) => {
+                    match line.trim() {
+                        "y" | "Y" => break,
+                        _ => bail!("Aborting."),
+                    }
+                }
+                Err(err) => bail!("Failed to read line - {err}."),
+            }
+        }
+    }
+
     let info = &api2::node::disks::API_METHOD_WIPE_DISK;
     let result = match info.handler {
         ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
-- 
2.39.2





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

* [pbs-devel] applied: [PATCH proxmox-backup v2] cli: add interactive confirmation for block device wipe
  2023-11-29 13:20 [pbs-devel] [PATCH proxmox-backup v2] cli: add interactive confirmation for block device wipe Markus Frank
@ 2023-11-29 14:04 ` Thomas Lamprecht
  0 siblings, 0 replies; 2+ messages in thread
From: Thomas Lamprecht @ 2023-11-29 14:04 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Markus Frank

Am 29/11/2023 um 14:20 schrieb Markus Frank:
> If stdin is a TTY, an interactive prompt is added to confirm the deletion
> of a block device, ensuring user verification before proceeding.
> 
> Signed-off-by: Markus Frank <m.frank@proxmox.com>
> ---
> v2:
> * changed stdin_isatty() to std::io::stdin().is_terminal()
> * removed unnecessary eprint
> * do not loop if answer is not either y/Y or n/N
> * moved err inline in bail
> 
>  src/bin/proxmox_backup_manager/disk.rs | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
> 
>

applied, but dropped the now useless loop in a follow-up and changed
the output target for the prompt message from stderr to stdout, thanks!




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

end of thread, other threads:[~2023-11-29 14:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-29 13:20 [pbs-devel] [PATCH proxmox-backup v2] cli: add interactive confirmation for block device wipe Markus Frank
2023-11-29 14:04 ` [pbs-devel] applied: " Thomas Lamprecht

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal