From 5594c1454d9b57418273b4b6fa009529482cc77d Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Fri, 18 Aug 2023 14:02:43 +0200 Subject: Added a prompt to confirm risky actions like deleting a repo Fix #3 --- src/lib.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 62c050a..fded885 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ pub mod user_info; use comfy_table::Table; use humansize::{format_size, BINARY}; use parser::*; -use rustyline::error::ReadlineError; +use rustyline::{error::ReadlineError, DefaultEditor}; use std::{io, ops::ControlFlow}; use thiserror::Error; @@ -60,8 +60,15 @@ pub fn run_command(user_input: &str) -> Result, ShackleError println!("Successfully created \"{}\"", init_result.path.display()); } Ok(ShackleCommand::Delete(DeleteArgs { directory })) => { - git::delete(&directory)?; - println!("Successfully deleted \"{}\"", directory.display()); + if confirm_risky_action(format!( + "Are you sure you want to delete \"{}\"?", + directory.display() + ))? { + git::delete(&directory)?; + println!("Successfully deleted \"{}\"", directory.display()); + } else { + println!("Action cancelled"); + } } Ok(ShackleCommand::Housekeeping(HousekeepingArgs { directory })) => match directory { Some(directory) => { @@ -92,6 +99,28 @@ pub fn run_command(user_input: &str) -> Result, ShackleError Ok(ControlFlow::Continue(())) } +fn confirm_risky_action(prompt: String) -> Result { + let mut rl = DefaultEditor::new()?; + loop { + let user_input = rl + .readline(&format!("{} (yes/no) ", prompt)) + .map(|user_input| user_input.to_lowercase())?; + + match user_input.trim() { + "" => {} + "yes" => { + return Ok(true); + } + "no" => { + return Ok(false); + } + _ => { + println!("Please answer 'yes' or 'no'."); + } + } + } +} + #[derive(Error, Debug)] pub enum ShackleError { #[error(transparent)] -- cgit v1.2.3