Skip to content

Commit

Permalink
feat: add -d and support for "latest" to get-tef
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Sep 16, 2024
1 parent 38281cb commit 05bc728
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ pub struct Serve {

#[derive(Debug, clap::Parser)]
pub struct GetTEF {
/// The file to remove. Can be "latest".
#[arg(index = 1, value_name = "FILE")]
pub jsonl_file: String,
/// Storage directory.
#[arg(short = 'd', long = "dir")]
pub dir: Option<String>,
/// Output file (.json file)
#[arg(short = 'o', long = "out")]
pub o: Option<String>,
Expand Down
42 changes: 41 additions & 1 deletion src/get_tef.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,54 @@
use std::{
fs,
io::{self, stdout, BufReader, BufWriter},
path::PathBuf,
};

use anyhow::Result;

use crate::{cli, utils};

fn get_file_in_dir(file: &str, d: &str) -> Result<String> {
let mut file2 = PathBuf::from(&d);
file2.push(file);

if fs::exists(&file2).ok() == Some(true) {
Ok(file2.to_string_lossy().to_string())
} else {
anyhow::bail!("Tried {file:?} and {file2:?}, neither of which exists");
}
}

fn find_latest_file(d: Option<impl AsRef<str>>) -> Result<String> {
let mut files = crate::list::list_files(d)?;
files.sort();

let f: &PathBuf = files
.last()
.ok_or_else(|| anyhow::anyhow!("No files in directory"))?;
Ok(f.to_string_lossy().to_string())
}

pub fn run(cli: cli::GetTEF) -> Result<()> {
let file_in = fs::File::open(cli.jsonl_file)?;
dbg!(&cli);
let mut file = cli.jsonl_file;

if fs::exists(&file).ok() != Some(true) {
dbg!(&file);
if file == "latest" {
file = find_latest_file(cli.dir.as_ref())?;
} else {
match cli.dir {
None => anyhow::bail!("File {file:?} does not exist"),
Some(d) => {
file = get_file_in_dir(&file, &d)?;
}
}
}
}

log::info!("reading TEF trace from file {file:?}");
let file_in = fs::File::open(file)?;
let mut reader = BufReader::new(file_in);

let out: Box<dyn io::Write> = match cli.o {
Expand Down
47 changes: 30 additions & 17 deletions src/list.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
use std::path::PathBuf;

use anyhow::Result;

use crate::{cli, utils};

pub fn run(cli: cli::List) -> Result<()> {
let files = match cli.dir {
Some(d) => {
let mut res = vec![];
for e in std::fs::read_dir(d)? {
let Ok(e) = e else { continue };
let Ok(ft) = e.file_type() else { continue };
if ft.is_file() {
res.push(e.path())
}
}
res
pub(crate) fn list_files_in_dir(d: &str) -> Result<Vec<PathBuf>> {
let mut res = vec![];
for e in std::fs::read_dir(d)? {
let Ok(e) = e else { continue };
let Ok(ft) = e.file_type() else { continue };
if ft.is_file() {
res.push(e.path())
}
None => {
let xdg = xdg::BaseDirectories::with_prefix(utils::XDG_PREFIX)?;
xdg.list_data_files("")
}
};
}
Ok(res)
}

pub(crate) fn list_files_in_xdg() -> Result<Vec<PathBuf>> {
let xdg = xdg::BaseDirectories::with_prefix(utils::XDG_PREFIX)?;
Ok(xdg.list_data_files(""))
}

pub(crate) fn list_files(dir: Option<impl AsRef<str>>) -> Result<Vec<PathBuf>> {
match dir {
Some(d) => list_files_in_dir(d.as_ref()),
None => list_files_in_xdg(),
}
}

pub fn run(cli: cli::List) -> Result<()> {
let mut files = list_files(cli.dir.as_ref())?;

// deterministic order
files.sort();

for f in files {
let Some(f) = f.as_path().to_str() else {
Expand Down

0 comments on commit 05bc728

Please sign in to comment.