2021-07-30 17:55:19 -05:00
|
|
|
use std::convert::TryFrom;
|
2021-08-25 14:30:27 -05:00
|
|
|
use std::io::{BufRead, Write};
|
2021-07-30 17:55:19 -05:00
|
|
|
|
|
|
|
use clap::ArgMatches;
|
|
|
|
|
|
|
|
use crate::error::{Error, Result};
|
|
|
|
use crate::database::Database;
|
2022-05-07 22:37:05 -05:00
|
|
|
use crate::interactive::{ask, confirm_deletion};
|
2021-08-25 14:30:27 -05:00
|
|
|
use crate::io::Streams;
|
2021-07-30 17:55:19 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
use super::{Command, Facts};
|
2021-07-30 17:55:19 -05:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Args {
|
|
|
|
Id(u64),
|
|
|
|
Sheet(String),
|
2022-05-07 22:37:05 -05:00
|
|
|
Last,
|
2021-07-30 17:55:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TryFrom<&'a ArgMatches<'a>> for Args {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(args: &'a ArgMatches) -> Result<Args> {
|
2022-05-07 22:37:05 -05:00
|
|
|
Ok(if let Some(id) = args.value_of("id") {
|
|
|
|
Args::Id(id.parse().unwrap())
|
|
|
|
} else if args.is_present("last") {
|
|
|
|
Args::Last
|
|
|
|
} else if let Some(sheet) = args.value_of("sheet") {
|
|
|
|
Args::Sheet(sheet.to_owned())
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
})
|
2021-07-30 17:55:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct KillCommand;
|
|
|
|
|
|
|
|
impl<'a> Command<'a> for KillCommand {
|
|
|
|
type Args = Args;
|
|
|
|
|
2022-05-07 22:37:05 -05:00
|
|
|
fn handle<D, I, O, E>(args: Args, streams: &mut Streams<D, I, O, E>, facts: &Facts) -> Result<()>
|
2021-07-30 17:55:19 -05:00
|
|
|
where
|
|
|
|
D: Database,
|
2021-08-25 14:30:27 -05:00
|
|
|
I: BufRead,
|
2021-07-30 17:55:19 -05:00
|
|
|
O: Write,
|
|
|
|
E: Write,
|
|
|
|
{
|
|
|
|
match args {
|
|
|
|
Args::Id(id) => {
|
2021-08-25 14:30:27 -05:00
|
|
|
if let Some(entry) = streams.db.entry_by_id(id)? {
|
2022-05-07 22:37:05 -05:00
|
|
|
confirm_deletion(streams, entry, facts.now)?;
|
2021-07-30 17:55:19 -05:00
|
|
|
} else {
|
2021-08-25 14:30:27 -05:00
|
|
|
writeln!(streams.out, "There's no entry with id {}. Someone found it before we did.", id)?;
|
2021-07-30 17:55:19 -05:00
|
|
|
}
|
2022-05-07 22:37:05 -05:00
|
|
|
}
|
|
|
|
Args::Last => {
|
|
|
|
let current_sheet = streams.db.current_sheet()?;
|
|
|
|
if let Some(entry) = streams.db.last_entry_of_sheet(¤t_sheet)? {
|
|
|
|
confirm_deletion(streams, entry, facts.now)?;
|
|
|
|
} else {
|
|
|
|
writeln!(streams.out, "Nothing to delete")?;
|
|
|
|
}
|
|
|
|
}
|
2021-07-30 17:55:19 -05:00
|
|
|
Args::Sheet(sheet) => {
|
2021-08-25 14:30:27 -05:00
|
|
|
let n = streams.db.entries_by_sheet(&sheet, None, None)?.len();
|
2021-07-30 17:55:19 -05:00
|
|
|
|
2021-08-25 14:30:27 -05:00
|
|
|
if ask(streams, &format!("are you sure you want to delete {} entries on sheet \"{}\"?", n, sheet))? {
|
|
|
|
streams.db.delete_entries_in_sheet(&sheet)?;
|
|
|
|
writeln!(streams.out, "They're gone")?;
|
2021-07-30 17:55:19 -05:00
|
|
|
} else {
|
2021-08-25 14:30:27 -05:00
|
|
|
writeln!(streams.out, "Don't worry, they're still there")?;
|
2021-07-30 17:55:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|