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;
|
2021-08-11 20:24:42 -05:00
|
|
|
use crate::interactive::ask;
|
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),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TryFrom<&'a ArgMatches<'a>> for Args {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(args: &'a ArgMatches) -> Result<Args> {
|
|
|
|
Ok(args.value_of("id").map(|v| {
|
|
|
|
Args::Id(v.parse().unwrap())
|
|
|
|
}).unwrap_or_else(|| {
|
|
|
|
Args::Sheet(args.value_of("sheet").unwrap().to_owned())
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct KillCommand;
|
|
|
|
|
|
|
|
impl<'a> Command<'a> for KillCommand {
|
|
|
|
type Args = Args;
|
|
|
|
|
2021-08-25 14:30:27 -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)? {
|
|
|
|
if ask(streams, &format!("are you sure you want to delete entry {}? ({})", entry.id, entry.note.unwrap_or_else(|| "".into())))? {
|
|
|
|
streams.db.delete_entry_by_id(id)?;
|
|
|
|
writeln!(streams.out, "It's dead")?;
|
2021-07-30 17:55:19 -05:00
|
|
|
} else {
|
2021-08-25 14:30:27 -05:00
|
|
|
writeln!(streams.out, "Don't worry, it's still there")?;
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
}
|