journal/journal_cli/src/commands/print_session.rs

61 lines
1.5 KiB
Rust
Raw Normal View History

2024-11-23 21:43:28 +00:00
use journal::{journal::JSONJournal, types::Consumer};
2024-11-20 13:47:34 +00:00
2024-11-23 23:43:25 +00:00
use crate::{
args::JournalLocation, display::print_ingestion_log, formatting::format_session_title,
};
2024-11-19 20:30:24 +00:00
2024-11-20 13:47:34 +00:00
#[derive(Debug, Clone, clap::Args)]
2024-11-21 15:10:34 +00:00
#[group(requires = "journal_location")]
2024-11-23 21:43:28 +00:00
pub struct PrintSessionArgs {
pub session_title: String,
2024-11-21 13:58:29 +00:00
2024-11-23 23:43:25 +00:00
#[command(flatten)]
pub journal_location: JournalLocation,
2024-11-21 13:58:29 +00:00
#[arg(long)]
2024-11-20 19:15:07 +00:00
pub substance_filter: Option<Vec<String>>,
2024-11-21 13:58:29 +00:00
#[arg(long, value_delimiter = ',')]
2024-11-20 19:15:07 +00:00
pub consumer_filter: Option<Vec<String>>,
}
pub fn parse_consumer_filter(consumer_filter: Option<Vec<String>>) -> Option<Vec<Consumer>> {
match consumer_filter {
Some(consumer_filter) => {
let mut consumers: Vec<Consumer> = Vec::new();
for consumer in consumer_filter.into_iter() {
2024-11-22 17:04:18 +00:00
let mut consumer = consumer;
if consumer.is_empty() {
consumer = "default".to_string();
2024-11-20 19:15:07 +00:00
}
2024-11-22 17:07:42 +00:00
consumers.push(Consumer::from(consumer.as_str()));
2024-11-20 19:15:07 +00:00
}
Some(consumers)
}
None => None,
}
2024-11-19 20:30:24 +00:00
}
2024-11-23 23:43:25 +00:00
pub fn print_session(args: &PrintSessionArgs) -> Result<(), Box<dyn std::error::Error>> {
let journal = JSONJournal::load(&args.journal_location.journal_file)?;
2024-11-23 21:43:28 +00:00
let session = journal
.first_session_by_title(&args.session_title)
.expect("could not find session");
2024-11-19 20:30:24 +00:00
2024-11-23 21:43:28 +00:00
//println!("{:#?}", &session);
2024-11-19 20:30:24 +00:00
2024-11-23 21:43:28 +00:00
println!("{}", format_session_title(&session));
2024-11-20 19:15:07 +00:00
let substance_filter = args.substance_filter.clone();
let consumer_filter = parse_consumer_filter(args.consumer_filter.clone());
print_ingestion_log(
&journal,
2024-11-23 21:43:28 +00:00
&session,
2024-11-20 19:15:07 +00:00
substance_filter.as_ref(),
consumer_filter.as_ref(),
);
2024-11-19 20:30:24 +00:00
2024-11-20 13:47:34 +00:00
Ok(())
}