new: add interactive command to add master log to database

- old add-master-log is now convert-master-log
- add-master-log is now interactive
- add-master-log is not well tested
This commit is contained in:
Wataru Otsubo 2024-09-10 23:01:20 +09:00
parent 7fe5218d39
commit 2dc790f9a8
4 changed files with 254 additions and 6 deletions

View file

@ -1,15 +1,17 @@
use core::str;
use std::{
env,
fmt::Display,
fs::File,
fs::{self, File},
io::{self, BufRead, BufReader},
path::{self, PathBuf},
path::{self, Path, PathBuf},
str::FromStr,
};
use anyhow::{anyhow, Context, Result};
use chrono::{DateTime, Utc};
use clap::{Parser, Subcommand};
use crossterm::{event, terminal};
use log::{debug, error, info, trace, warn};
use masterlog::{get_output_filename, MasterLogResult};
use semver::Version;
@ -31,7 +33,7 @@ struct Args {
#[derive(Subcommand, Debug)]
pub enum Commands {
/// Parse master jathub logfile for PS Board QAQC and write out to CSV.
/// Parse master log and convert to CSV and prompt post processes interactively.
AddMasterLog {
/// Master log file.
master_log: path::PathBuf,
@ -39,6 +41,19 @@ pub enum Commands {
/// Default is `out_<runid>.csv`
// #[arg(default_value = get_default_log_path().into_os_string())]
outfile: Option<PathBuf>,
/// Editor to use.
/// Default is `$VISUAL` or `$EDITOR`.
#[arg(short, long)]
editor: Option<String>,
},
/// Parse master jathub logfile for PS Board QAQC and write out to CSV.
ConvertMasterLog {
/// Master log file.
master_log: path::PathBuf,
/// Output CSV file.
/// Default is `out_<runid>.csv`
// #[arg(default_value = get_default_log_path().into_os_string())]
outfile: Option<PathBuf>,
},
/// Check CSV format
CheckDB {
@ -384,6 +399,81 @@ fn main() -> Result<()> {
Commands::AddMasterLog {
master_log,
outfile,
editor,
} => {
let result = MasterLogResult::parse_file(master_log)?;
debug!("{:?}", result);
// Print boards to retest
result.print_boards_to_retest(io::stdout())?;
let outfile = outfile.unwrap_or(get_output_filename(&result));
write_psbqaqc_csv(result, outfile.clone())?;
println!("Add comments about errors in the last column of the CSV file");
println!("Press any key to start editting...");
terminal::enable_raw_mode()?;
let _ = event::read()?;
terminal::disable_raw_mode()?;
let editor = match (editor, env::var("VISUAL"), env::var("EDITOR")) {
(Some(editor), _, _) => editor,
(None, Ok(editor), _) => {
info!("Use VISUAL");
editor
}
(None, Err(_), Ok(editor)) => {
info!("Use EDITOR");
editor
}
(None, Err(e1), Err(e2)) => {
info!("No VISUAL nor EDITOR found, {}, {}", e1, e2);
"nano".to_string()
}
};
std::process::Command::new(editor)
.arg(outfile.clone())
.spawn()?
.wait()?;
{
let f = File::open(outfile.clone())?;
let rdr = BufReader::new(f);
rdr.lines().for_each(|l| println!("{}", l.unwrap()));
}
println!();
println!("Copy the CSV above and paste it to the database(Google sheets)");
println!("Choose Data->Split text to columns to format it");
println!("Press any key when upload finished...");
terminal::enable_raw_mode()?;
let _ = event::read()?;
terminal::disable_raw_mode()?;
let uploaded_outfile = {
let mut new_file_name = outfile
.clone()
.file_stem()
.context("No file stem for out file")?
.to_owned();
new_file_name.push("_uploaded");
if let Some(ext) = outfile.extension() {
new_file_name.push(".");
new_file_name.push(ext);
};
PathBuf::from("log").join(new_file_name)
};
info!(
"Renaming {} to {}",
outfile.display(),
uploaded_outfile.is_dir()
);
fs::rename(outfile, uploaded_outfile)?;
}
Commands::ConvertMasterLog {
master_log,
outfile,
} => {
let result = MasterLogResult::parse_file(master_log)?;
debug!("{:?}", result);