From a94a871e52d4e41003b8817e69b1dc98769757a8 Mon Sep 17 00:00:00 2001 From: Wataru Otsubo Date: Sun, 21 Jul 2024 21:04:46 +0900 Subject: [PATCH] update: organize commands to subcommands and rename bin - existing functions are moved to subcommand `add-master-log` --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 5 +++ src/main.rs | 119 +++++++++++++++++++++++++++++---------------------- tests/cli.rs | 9 ++-- 5 files changed, 81 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d5d999..a410a46 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -637,7 +637,7 @@ dependencies = [ ] [[package]] -name = "psb-qaqc-parse" +name = "psb-qaqc" version = "0.1.0" dependencies = [ "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 9a67c4b..63b6943 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "psb-qaqc-parse" +name = "psb-qaqc" version = "0.1.0" edition = "2021" diff --git a/README.md b/README.md index aa84e5f..617f229 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,11 @@ cargo build cargo run -- -h ``` +Build for JATHub using cross: +```sh +CROSS_CONTAINER_ENGINE=podman cross build --release --target armv7-unknown-linux-musleabihf +``` + ### test ```sh cargo test diff --git a/src/main.rs b/src/main.rs index 8096328..6ed3bb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,26 +10,35 @@ use std::{ use anyhow::{anyhow, Context, Result}; use chrono::{DateTime, Local, Utc}; -use clap::Parser; +use clap::{Parser, Subcommand}; use log::{debug, error, info, trace, warn}; use regex::Regex; use semver::Version; use serde::{Deserialize, Serialize}; 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)] #[command(version, about, long_about = None)] struct Args { - /// Master log file. - master_log: path::PathBuf, - /// Output CSV file. - // #[arg(default_value = get_default_log_path().into_os_string())] - outfile: PathBuf, + #[command(subcommand)] + command: Commands, #[command(flatten)] 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 #[derive(Debug, PartialEq, Eq, Hash, Clone, PartialOrd, Ord)] enum PositionLayer { @@ -444,55 +453,58 @@ fn main() -> Result<()> { " 1".split_whitespace().next().unwrap().parse::() ); - let result = { - let file = File::open(args.master_log.clone())?; - let reader = BufReader::new(file); - MasterLogResult::parse_file( - reader, - args.master_log - .file_name() - .unwrap() - .to_str() - .unwrap() - .to_string(), - )? - }; - debug!("{:?}", result); + match args.command { + Commands::AddMasterLog { + master_log, + outfile, + } => { + let result = { + let file = File::open(master_log.clone())?; + let reader = BufReader::new(file); + MasterLogResult::parse_file( + reader, + master_log + .file_name() + .unwrap() + .to_str() + .unwrap() + .to_string(), + )? + }; + debug!("{:?}", result); - // { - // let file = File::options() - // .read(true) - // .open(args.outfile)?; - // - // let mut rdr = csv::Reader::from_reader(file); - // rdr.records(); - // } + // { + // let file = File::options() + // .read(true) + // .open(args.outfile)?; + // + // let mut rdr = csv::Reader::from_reader(file); + // rdr.records(); + // } - let expanded_results = PsbQaqcResult::from_masterlogresult(result); + let expanded_results = PsbQaqcResult::from_masterlogresult(result); - let mut wtr = match args.outfile.exists() { - true => { - let file = File::options().read(true).append(true).open(args.outfile)?; - csv::WriterBuilder::new() - .has_headers(false) - .from_writer(file) + let mut wtr = match outfile.exists() { + true => { + let file = File::options().read(true).append(true).open(outfile)?; + csv::WriterBuilder::new() + .has_headers(false) + .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(()) } @@ -625,4 +637,9 @@ mod test { ) ); } + + // #[test] + // fn parse_file() { + // let logfile = r""""""; + // } } diff --git a/tests/cli.rs b/tests/cli.rs index 35f77b4..989dfe3 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -15,8 +15,9 @@ mod integrated_test { let test_out = PathBuf::new().join(&test_out_dir).join("out.csv"); // 1st file - let mut cmd = Command::cargo_bin("psb-qaqc-parse")?; + let mut cmd = Command::cargo_bin("psb-qaqc")?; cmd.current_dir("tests") + .arg("add-master-log") .arg("./example_logs/valid/20240720_171418.log") .arg(test_out.as_path()) .assert() @@ -33,8 +34,9 @@ mod integrated_test { } // 2nd file - let mut cmd = Command::cargo_bin("psb-qaqc-parse")?; + let mut cmd = Command::cargo_bin("psb-qaqc")?; cmd.current_dir("tests") + .arg("add-master-log") .arg("./example_logs/valid/20240720_171418.log") .arg(test_out.as_path()) .assert() @@ -48,8 +50,9 @@ mod integrated_test { let test_out_dir = assert_fs::TempDir::new()?; 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") + .arg("add-master-log") .arg("./example_logs/valid/20240720_171419.log") .arg(test_out.as_path()) .assert()