Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --latest flag #67

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading