use std::fs::{File, OpenOptions};
use std::io::{BufReader, Write};
use std::path::Path;
use troupekit_trace::view::{
read_jsonl, render_html_only, render_terminal, SpanTree, TermOptions, ViewLimits,
};
use crate::args::TraceArgs;
pub fn run(call: &TraceArgs, tty: bool) -> Result<String, String> {
let tree = read(&call.file)?;
match &call.html {
Some(out) => {
write_page(out, &render_html_only(&tree, call.only))?;
Ok(format!("wrote {}", out.display()))
}
None => {
let options = TermOptions::default()
.with_color(call.color && tty)
.with_only(call.only);
Ok(render_terminal(&tree, &options))
}
}
}
fn read(path: &Path) -> Result<SpanTree, String> {
let file = File::open(path).map_err(|e| format!("cannot read {}: {e}", path.display()))?;
read_jsonl(BufReader::new(file), ViewLimits::default())
.map_err(|e| format!("{}: {e}", path.display()))
}
fn write_page(out: &Path, page: &str) -> Result<(), String> {
let mut options = OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600);
}
let mut file = options
.open(out)
.map_err(|e| format!("cannot create {}: {e}", out.display()))?;
file.write_all(page.as_bytes())
.map_err(|e| format!("cannot write {}: {e}", out.display()))
}