From 83f03d2e44f6e0c6625b02e916832ace3aa50ee8 Mon Sep 17 00:00:00 2001 From: Ambareesh Shyam Sundar <44121747+ambareesh1510@users.noreply.github.com> Date: Mon, 23 Sep 2024 21:30:00 -0400 Subject: [PATCH] Add --latest flag (#67) --- src/cli.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 8e55132..f24423a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,6 @@ use clap::Parser; -use anyhow::bail; +use anyhow::{bail, Context}; use std::fs; use std::path::PathBuf; @@ -11,6 +11,9 @@ use tlparse::{parse_path, ParseConfig}; #[command(propagate_version = true)] pub struct Cli { path: PathBuf, + /// Parse most recent log + #[arg(long)] + latest: bool, /// Output directory, defaults to `tl_out` #[arg(short, default_value = "tl_out")] out: PathBuf, @@ -42,7 +45,30 @@ pub struct Cli { fn main() -> anyhow::Result<()> { let cli = Cli::parse(); - let path = cli.path; + let path = if cli.latest { + let input_path = cli.path; + // Path should be a directory + if !input_path.is_dir() { + bail!( + "Input path {} is not a directory (required when using --latest)", + input_path.display() + ); + } + + let last_modified_file = std::fs::read_dir(&input_path) + .with_context(|| format!("Couldn't access directory {}", input_path.display()))? + .flatten() + .filter(|f| f.metadata().unwrap().is_file()) + .max_by_key(|x| x.metadata().unwrap().modified().unwrap()); + + let Some(last_modified_file) = last_modified_file else { + bail!("No files found in directory {}", input_path.display()); + }; + last_modified_file.path() + } else { + cli.path + }; + let out_path = cli.out; if out_path.exists() {