summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2023-08-18 14:02:43 +0200
committerJustin Wernick <justin@worthe-it.co.za>2023-08-18 14:04:22 +0200
commit5594c1454d9b57418273b4b6fa009529482cc77d (patch)
treee4a37b6bec92eda21cab3f26f2ed5ce054457e07 /src
parentcca27c355823272ccc5927057d5fe228ede0bd41 (diff)
Added a prompt to confirm risky actions like deleting a repo
Fix #3
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs35
1 files changed, 32 insertions, 3 deletions
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<ControlFlow<(), ()>, 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<ControlFlow<(), ()>, ShackleError
Ok(ControlFlow::Continue(()))
}
+fn confirm_risky_action(prompt: String) -> Result<bool, ShackleError> {
+ 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)]