From 454b0e4b78b5b8ed16c8360e21659f706d6e9579 Mon Sep 17 00:00:00 2001 From: nick Date: Thu, 10 Oct 2024 12:58:39 -0400 Subject: [PATCH] added autopsy option --- src/args.rs | 5 +++++ src/main.rs | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/args.rs b/src/args.rs index 139bf60..25181c1 100644 --- a/src/args.rs +++ b/src/args.rs @@ -2,6 +2,11 @@ use clap::Parser; #[derive(Parser)] pub struct Args { + /// open, print, and remove the log file + /// (if it exists) + #[arg(short, long)] + pub autopsy: bool, + /// root of the new filesystem #[arg( default_value_t = String::from(".") diff --git a/src/main.rs b/src/main.rs index 5523c6d..c172589 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,9 +14,9 @@ use terminal::Terminal; use pane::prelude::*; use std::fs::{remove_file, OpenOptions}; -use std::io::{Error, ErrorKind}; +use std::io::{read_to_string, Error, ErrorKind}; -use anyhow::{Result as AnyResult, Error as AnyError}; +use anyhow::{Context, Error as AnyError, Result as AnyResult}; use clap::Parser; use crossterm::event::{read, Event, KeyEvent}; use gag::Redirect; @@ -27,6 +27,19 @@ const LOG_EXISTS_MSG: &str = "the log file exists, implying dirbuider didn't exi fn main() -> AnyResult<()> { let args = Args::parse(); + if args.autopsy { + let f = OpenOptions::new() + .read(true) + .open(LOG_FILE) + .map_err(Into::::into) + .context(LOG_FILE)?; + let s = read_to_string(f)?; + eprint!("{s}"); + remove_file(LOG_FILE)?; + + return Ok(()); + } + let target = OpenOptions::new() .write(true) .create_new(true)