update: print error when no editor is specified & add-master-log doesn't

append csv file
This commit is contained in:
Wataru Otsubo 2024-09-11 15:19:13 +09:00
parent 4478ec1654
commit 3fdbe5ad4c

View file

@ -4,7 +4,7 @@ use std::{
fmt::Display, fmt::Display,
fs::{self, File}, fs::{self, File},
io::{self, BufRead, BufReader}, io::{self, BufRead, BufReader},
path::{self, Path, PathBuf}, path::{self, PathBuf},
str::FromStr, str::FromStr,
}; };
@ -351,11 +351,12 @@ impl PsbQaqcResult {
// .collect_vec() // .collect_vec()
// } // }
fn write_psbqaqc_csv(result: MasterLogResult, outfile: PathBuf) -> Result<()> { /// If `nonewfile` is `true`, out file is not created when there is already a file.
fn write_psbqaqc_csv(result: MasterLogResult, outfile: PathBuf, nonewfile: bool) -> Result<()> {
let expanded_results = PsbQaqcResult::from_masterlogresult(result); let expanded_results = PsbQaqcResult::from_masterlogresult(result);
let mut wtr = match outfile.exists() { let mut wtr = match (outfile.exists(), nonewfile) {
true => { (true, false) => {
let file = File::options() let file = File::options()
.read(true) .read(true)
.append(true) .append(true)
@ -364,7 +365,7 @@ fn write_psbqaqc_csv(result: MasterLogResult, outfile: PathBuf) -> Result<()> {
.has_headers(false) .has_headers(false)
.from_writer(file) .from_writer(file)
} }
false => { (false, _) => {
println!("Creating new file: {}", outfile.display()); println!("Creating new file: {}", outfile.display());
let file = File::options() let file = File::options()
.create_new(true) .create_new(true)
@ -374,6 +375,10 @@ fn write_psbqaqc_csv(result: MasterLogResult, outfile: PathBuf) -> Result<()> {
.has_headers(true) .has_headers(true)
.from_writer(file) .from_writer(file)
} }
(true, true) => {
error!("Out file already exists but specified not to make new file");
return Err(anyhow!("Out file already exists"));
}
}; };
for result in expanded_results { for result in expanded_results {
wtr.serialize(result)?; wtr.serialize(result)?;
@ -409,7 +414,7 @@ fn main() -> Result<()> {
let outfile = outfile.unwrap_or(get_output_filename(&result)); let outfile = outfile.unwrap_or(get_output_filename(&result));
write_psbqaqc_csv(result, outfile.clone())?; write_psbqaqc_csv(result, outfile.clone(), true)?;
println!("Add comments about errors in the last column of the CSV file"); println!("Add comments about errors in the last column of the CSV file");
println!("Press any key to start editting..."); println!("Press any key to start editting...");
@ -428,14 +433,17 @@ fn main() -> Result<()> {
editor editor
} }
(None, Err(e1), Err(e2)) => { (None, Err(e1), Err(e2)) => {
info!("No VISUAL nor EDITOR found, {}, {}", e1, e2); warn!("No VISUAL nor EDITOR found, {}, {}", e1, e2);
warn!("Using `nano` as a fallback editor");
"nano".to_string() "nano".to_string()
} }
}; };
std::process::Command::new(editor) std::process::Command::new(editor)
.arg(outfile.clone()) .arg(outfile.clone())
.spawn()? .spawn()
.wait()?; .context("Error during spawning editor")?
.wait()
.context("Error while waiting editor")?;
{ {
let f = File::open(outfile.clone())?; let f = File::open(outfile.clone())?;
@ -444,7 +452,7 @@ fn main() -> Result<()> {
} }
println!(); println!();
println!("Copy the CSV above and paste it to the database(Google sheets)"); println!("Copy the CSV above and paste it to the database(Google sheets)");
println!("Choose Data->Split text to columns to format it"); println!("Choose `Data->Split text to columns` to format it");
println!("Press any key when upload finished..."); println!("Press any key when upload finished...");
terminal::enable_raw_mode()?; terminal::enable_raw_mode()?;
@ -483,7 +491,7 @@ fn main() -> Result<()> {
let outfile = outfile.unwrap_or(get_output_filename(&result)); let outfile = outfile.unwrap_or(get_output_filename(&result));
write_psbqaqc_csv(result, outfile)?; write_psbqaqc_csv(result, outfile, false)?;
} }
Commands::CheckDB { csvfile } => { Commands::CheckDB { csvfile } => {
// TODO: more friendly message (like row number) // TODO: more friendly message (like row number)