tiempo-rs/src/commands/archive.rs

86 lines
2.6 KiB
Rust
Raw Normal View History

2021-08-09 10:13:51 -05:00
use std::convert::TryFrom;
2021-08-25 14:30:27 -05:00
use std::io::{BufRead, Write};
2021-08-09 10:13:51 -05:00
use clap::ArgMatches;
use chrono::{DateTime, Utc};
use regex::Regex;
2021-08-11 20:25:32 -05:00
use crate::database::Database;
2021-08-09 10:13:51 -05:00
use crate::error::{Error, Result};
2021-08-25 14:30:27 -05:00
use crate::commands::{Command, Facts};
2021-08-09 10:13:51 -05:00
use crate::timeparse::parse_time;
2021-08-11 20:25:32 -05:00
use crate::old::{entries_or_warning, time_or_warning};
2021-08-09 10:13:51 -05:00
use crate::formatters::text;
use crate::regex::parse_regex;
2021-08-11 20:25:32 -05:00
use crate::interactive::ask;
2021-08-25 14:30:27 -05:00
use crate::io::Streams;
2021-08-09 10:13:51 -05:00
#[derive(Default)]
pub struct Args {
start: Option<DateTime<Utc>>,
end: Option<DateTime<Utc>>,
grep: Option<Regex>,
fake: bool,
sheet: Option<String>,
}
impl<'a> TryFrom<&'a ArgMatches<'a>> for Args {
type Error = Error;
fn try_from(matches: &'a ArgMatches) -> Result<Self> {
Ok(Args {
2021-12-13 14:20:56 -06:00
start: matches.value_of("start").map(parse_time).transpose()?,
end: matches.value_of("end").map(parse_time).transpose()?,
2021-08-09 10:13:51 -05:00
grep: matches.value_of("grep").map(parse_regex).transpose()?,
fake: matches.is_present("fake"),
sheet: matches.value_of("sheet").map(|s| s.to_owned()),
})
}
}
pub struct ArchiveCommand {}
impl<'a> Command<'a> for ArchiveCommand {
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-08-09 10:13:51 -05:00
where
D: Database,
2021-08-25 14:30:27 -05:00
I: BufRead,
2021-08-09 10:13:51 -05:00
O: Write,
E: Write,
{
2021-08-11 20:25:32 -05:00
let mut entries = {
2021-08-25 14:30:27 -05:00
let start = args.start.map(|s| time_or_warning(s, &streams.db)).transpose()?.map(|s| s.0);
let end = args.end.map(|e| time_or_warning(e, &streams.db)).transpose()?.map(|e| e.0);
let current_sheet = streams.db.current_sheet()?.unwrap_or_else(|| "default".into());
2021-08-11 20:25:32 -05:00
let sheet = args.sheet.unwrap_or(current_sheet);
2021-08-25 14:30:27 -05:00
streams.db.entries_by_sheet(&sheet, start, end)?
2021-08-11 20:25:32 -05:00
};
if let Some(re) = args.grep {
entries.retain(|e| re.is_match(&e.note.clone().unwrap_or_else(String::new)));
}
if args.fake {
2021-08-25 14:30:27 -05:00
let (entries, _) = entries_or_warning(entries, &streams.db)?;
2021-08-11 20:25:32 -05:00
text::print_formatted(
entries,
2021-08-25 14:30:27 -05:00
&mut streams.out,
facts,
2021-08-11 20:25:32 -05:00
true,
)?;
2021-08-25 14:30:27 -05:00
} else if ask(streams, &format!("Archive {} entries?", entries.len()))? {
2021-08-11 20:35:24 -05:00
for entry in entries {
2021-08-25 14:30:27 -05:00
streams.db.entry_update(entry.id, entry.start, entry.end, entry.note, &format!("_{}", entry.sheet))?;
2021-08-11 20:25:32 -05:00
}
2021-08-11 20:35:24 -05:00
} else {
2021-08-25 14:30:27 -05:00
writeln!(streams.out, "Ok, they're still there")?;
2021-08-11 20:25:32 -05:00
}
Ok(())
2021-08-09 10:13:51 -05:00
}
}