update: organize commands to subcommands and rename bin

- existing functions are moved to subcommand `add-master-log`
This commit is contained in:
Wataru Otsubo 2024-07-21 21:04:46 +09:00
parent bdb415e6e6
commit a94a871e52
5 changed files with 81 additions and 56 deletions

2
Cargo.lock generated
View file

@ -637,7 +637,7 @@ dependencies = [
] ]
[[package]] [[package]]
name = "psb-qaqc-parse" name = "psb-qaqc"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",

View file

@ -1,5 +1,5 @@
[package] [package]
name = "psb-qaqc-parse" name = "psb-qaqc"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

View file

@ -9,6 +9,11 @@ cargo build
cargo run -- -h cargo run -- -h
``` ```
Build for JATHub using cross:
```sh
CROSS_CONTAINER_ENGINE=podman cross build --release --target armv7-unknown-linux-musleabihf
```
### test ### test
```sh ```sh
cargo test cargo test

View file

@ -10,26 +10,35 @@ use std::{
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use chrono::{DateTime, Local, Utc}; use chrono::{DateTime, Local, Utc};
use clap::Parser; use clap::{Parser, Subcommand};
use log::{debug, error, info, trace, warn}; use log::{debug, error, info, trace, warn};
use regex::Regex; use regex::Regex;
use semver::Version; use semver::Version;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr}; use serde_with::{serde_as, DisplayFromStr};
/// Parse master jathub logfile for PS Board QAQC and write out to CSV. /// PS Board QAQC shift related commands.
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
struct Args { struct Args {
/// Master log file. #[command(subcommand)]
master_log: path::PathBuf, command: Commands,
/// Output CSV file.
// #[arg(default_value = get_default_log_path().into_os_string())]
outfile: PathBuf,
#[command(flatten)] #[command(flatten)]
verbose: clap_verbosity_flag::Verbosity, verbose: clap_verbosity_flag::Verbosity,
} }
#[derive(Subcommand, Debug)]
pub enum Commands {
/// Parse master jathub logfile for PS Board QAQC and write out to CSV.
AddMasterLog {
/// Master log file.
master_log: path::PathBuf,
/// Output CSV file.
// #[arg(default_value = get_default_log_path().into_os_string())]
outfile: PathBuf,
},
}
/// Layer /// Layer
#[derive(Debug, PartialEq, Eq, Hash, Clone, PartialOrd, Ord)] #[derive(Debug, PartialEq, Eq, Hash, Clone, PartialOrd, Ord)]
enum PositionLayer { enum PositionLayer {
@ -444,55 +453,58 @@ fn main() -> Result<()> {
" 1".split_whitespace().next().unwrap().parse::<u8>() " 1".split_whitespace().next().unwrap().parse::<u8>()
); );
let result = { match args.command {
let file = File::open(args.master_log.clone())?; Commands::AddMasterLog {
let reader = BufReader::new(file); master_log,
MasterLogResult::parse_file( outfile,
reader, } => {
args.master_log let result = {
.file_name() let file = File::open(master_log.clone())?;
.unwrap() let reader = BufReader::new(file);
.to_str() MasterLogResult::parse_file(
.unwrap() reader,
.to_string(), master_log
)? .file_name()
}; .unwrap()
debug!("{:?}", result); .to_str()
.unwrap()
.to_string(),
)?
};
debug!("{:?}", result);
// { // {
// let file = File::options() // let file = File::options()
// .read(true) // .read(true)
// .open(args.outfile)?; // .open(args.outfile)?;
// //
// let mut rdr = csv::Reader::from_reader(file); // let mut rdr = csv::Reader::from_reader(file);
// rdr.records(); // rdr.records();
// } // }
let expanded_results = PsbQaqcResult::from_masterlogresult(result); let expanded_results = PsbQaqcResult::from_masterlogresult(result);
let mut wtr = match args.outfile.exists() { let mut wtr = match outfile.exists() {
true => { true => {
let file = File::options().read(true).append(true).open(args.outfile)?; let file = File::options().read(true).append(true).open(outfile)?;
csv::WriterBuilder::new() csv::WriterBuilder::new()
.has_headers(false) .has_headers(false)
.from_writer(file) .from_writer(file)
}
false => {
println!("Creating new file: {}", outfile.display());
let file = File::options().create_new(true).write(true).open(outfile)?;
csv::WriterBuilder::new()
.has_headers(true)
.from_writer(file)
}
};
for result in expanded_results {
wtr.serialize(result)?;
}
wtr.flush()?;
} }
false => {
println!("Creating new file: {}", args.outfile.display());
let file = File::options()
.create_new(true)
.write(true)
.open(args.outfile)?;
csv::WriterBuilder::new()
.has_headers(true)
.from_writer(file)
}
};
for result in expanded_results {
wtr.serialize(result)?;
} }
wtr.flush()?;
Ok(()) Ok(())
} }
@ -625,4 +637,9 @@ mod test {
) )
); );
} }
// #[test]
// fn parse_file() {
// let logfile = r"""""";
// }
} }

View file

@ -15,8 +15,9 @@ mod integrated_test {
let test_out = PathBuf::new().join(&test_out_dir).join("out.csv"); let test_out = PathBuf::new().join(&test_out_dir).join("out.csv");
// 1st file // 1st file
let mut cmd = Command::cargo_bin("psb-qaqc-parse")?; let mut cmd = Command::cargo_bin("psb-qaqc")?;
cmd.current_dir("tests") cmd.current_dir("tests")
.arg("add-master-log")
.arg("./example_logs/valid/20240720_171418.log") .arg("./example_logs/valid/20240720_171418.log")
.arg(test_out.as_path()) .arg(test_out.as_path())
.assert() .assert()
@ -33,8 +34,9 @@ mod integrated_test {
} }
// 2nd file // 2nd file
let mut cmd = Command::cargo_bin("psb-qaqc-parse")?; let mut cmd = Command::cargo_bin("psb-qaqc")?;
cmd.current_dir("tests") cmd.current_dir("tests")
.arg("add-master-log")
.arg("./example_logs/valid/20240720_171418.log") .arg("./example_logs/valid/20240720_171418.log")
.arg(test_out.as_path()) .arg(test_out.as_path())
.assert() .assert()
@ -48,8 +50,9 @@ mod integrated_test {
let test_out_dir = assert_fs::TempDir::new()?; let test_out_dir = assert_fs::TempDir::new()?;
let test_out = PathBuf::new().join(&test_out_dir).join("out.csv"); let test_out = PathBuf::new().join(&test_out_dir).join("out.csv");
let mut cmd = Command::cargo_bin("psb-qaqc-parse")?; let mut cmd = Command::cargo_bin("psb-qaqc")?;
cmd.current_dir("tests") cmd.current_dir("tests")
.arg("add-master-log")
.arg("./example_logs/valid/20240720_171419.log") .arg("./example_logs/valid/20240720_171419.log")
.arg(test_out.as_path()) .arg(test_out.as_path())
.assert() .assert()