Skip to content

Commit

Permalink
Add --latest flag (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
ambareesh1510 authored Sep 24, 2024
1 parent 6c6e3c3 commit 83f03d2
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;

use anyhow::bail;
use anyhow::{bail, Context};
use std::fs;
use std::path::PathBuf;

Expand All @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 83f03d2

Please sign in to comment.