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 1 commit
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: 27 additions & 3 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 @@ -10,7 +10,11 @@ use tlparse::{parse_path, ParseConfig};
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
pub struct Cli {
path: PathBuf,
#[arg(required_unless_present = "latest")]
path: Option<PathBuf>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor request: instead of making path optional, could you just keep it required (so no default to current directory; this is rarely what you want anyway since TORCH_TRACE makes you specify an output directory for logs)

/// 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 +46,27 @@ 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.unwrap_or(PathBuf::from("."));
// Path should be a directory
if !input_path.is_dir() {
bail!("Input path {} is not a directory", 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.unwrap()
};

let out_path = cli.out;

if out_path.exists() {
Expand Down
Loading