Skip to content

Commit

Permalink
Ask for confirmation on deletion (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
evansmurithi authored Aug 13, 2018
1 parent 6123192 commit a39b8f1
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/cmd/delete.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::{App, Arg, ArgMatches, SubCommand};
use fs;
use std::io::{self, Write};

// Create arguments for `delete` subcommand
pub fn subcommand<'a, 'b>() -> App<'a, 'b> {
Expand All @@ -15,18 +16,30 @@ pub fn subcommand<'a, 'b>() -> App<'a, 'b> {
// Implementation for the `delete` subcommand
pub fn run(args: &ArgMatches) {
let account_name = args.value_of("account").unwrap();
match fs::read() {
Ok(mut accounts) => {
if accounts.get(account_name).is_some() {
accounts.remove(account_name);
match fs::write(&accounts) {
Ok(_) => println!("Account successfully deleted"),
print!("Are you sure you want to delete {} [N/y]? ", account_name);
io::stdout().flush().unwrap();
let mut answer = String::new();
match io::stdin().read_line(&mut answer) {
Ok(_) => {
if answer.trim().to_lowercase() == "y" {
match fs::read() {
Ok(mut accounts) => {
if accounts.get(account_name).is_some() {
accounts.remove(account_name);
match fs::write(&accounts) {
Ok(_) => println!("Account successfully deleted"),
Err(err) => eprintln!("{}", err),
};
} else {
println!("Account does not exist");
}
}
Err(err) => eprintln!("{}", err),
};
} else {
println!("Account does not exist");
println!("Abort.");
}
}
Err(err) => eprintln!("{}", err),
}
Err(_) => eprintln!("Failed to read input"),
};
}

0 comments on commit a39b8f1

Please sign in to comment.