mirror of
https://github.com/Chaoscaot/schemsearch.git
synced 2025-11-05 05:54:02 +01:00
Finishing
This commit is contained in:
@ -1,12 +1,22 @@
|
||||
mod types;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{BufWriter, StdoutLock, Write};
|
||||
use clap::{command, Arg, ArgAction, Command};
|
||||
use schemsearch_files::Schematic;
|
||||
use std::path::Path;
|
||||
use clap::error::ErrorKind;
|
||||
#[cfg(feature = "sql")]
|
||||
use futures::executor::block_on;
|
||||
use schemsearch_lib::{search, SearchBehavior};
|
||||
#[cfg(feature = "sql")]
|
||||
use schemsearch_sql::load_all_schematics;
|
||||
use crate::types::{PathSchematicSupplier, SchematicSupplierType};
|
||||
#[cfg(feature = "sql")]
|
||||
use crate::types::SqlSchematicSupplier;
|
||||
|
||||
fn main() {
|
||||
#[allow(unused_mut)]
|
||||
let mut cmd = command!("schemsearch")
|
||||
.arg(
|
||||
Arg::new("pattern")
|
||||
@ -17,7 +27,6 @@ fn main() {
|
||||
.arg(
|
||||
Arg::new("schematic")
|
||||
.help("The schematics to search in")
|
||||
.required(true)
|
||||
.action(ArgAction::Append),
|
||||
)
|
||||
.arg(
|
||||
@ -83,6 +92,16 @@ fn main() {
|
||||
.about("Searches for a pattern in a schematic")
|
||||
.bin_name("schemsearch");
|
||||
|
||||
#[cfg(feature = "sql")]
|
||||
let mut cmd = cmd
|
||||
.arg(
|
||||
Arg::new("sql")
|
||||
.help("The sql file to write to")
|
||||
.short('s')
|
||||
.long("sql")
|
||||
.action(ArgAction::SetTrue),
|
||||
);
|
||||
|
||||
let matches = cmd.get_matches_mut();
|
||||
|
||||
if matches.contains_id("help") {
|
||||
@ -105,7 +124,21 @@ fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
let schematics = matches.get_many::<String>("schematic").expect("Couldn't get schematics");
|
||||
let mut schematics: Vec<SchematicSupplierType> = match matches.get_many::<String>("schematic") {
|
||||
None => vec![],
|
||||
Some(x) => x.map(|x| SchematicSupplierType::PATH(Box::new(PathSchematicSupplier{path: Path::new(x) }))).collect()
|
||||
};
|
||||
|
||||
#[cfg(feature = "sql")]
|
||||
if matches.get_flag("sql") {
|
||||
for schem in block_on(load_all_schematics()) {
|
||||
schematics.push(SchematicSupplierType::SQL(SqlSchematicSupplier{
|
||||
node: schem
|
||||
}))
|
||||
};
|
||||
} else if schematics.is_empty() {
|
||||
cmd.error(ErrorKind::MissingRequiredArgument, "No schematics specified").exit();
|
||||
}
|
||||
|
||||
let mut output_std = false;
|
||||
let mut output_std_csv = false;
|
||||
@ -145,43 +178,55 @@ fn main() {
|
||||
file_out = Some(BufWriter::new(file.unwrap()));
|
||||
}
|
||||
|
||||
for schem_path in schematics {
|
||||
let path = Path::new(schem_path);
|
||||
if path.is_dir() {
|
||||
match path.read_dir() {
|
||||
Ok(x) => {
|
||||
for path in x {
|
||||
match path {
|
||||
Ok(x) => {
|
||||
if x.path().extension().unwrap_or_default() == "schem" {
|
||||
search_schempath(&mut cmd, search_behavior, &pattern, &mut output_std, &mut output_std_csv, &mut output_file_csv, &mut output_file, &mut lock, &mut file_out, &x.path());
|
||||
for schem in schematics {
|
||||
match schem {
|
||||
SchematicSupplierType::PATH(schem) => {
|
||||
let path = schem.path;
|
||||
if path.is_dir() {
|
||||
match path.read_dir() {
|
||||
Ok(x) => {
|
||||
for path in x {
|
||||
match path {
|
||||
Ok(x) => {
|
||||
if x.path().extension().unwrap_or_default() == "schem" {
|
||||
let schematic = load_schem(&mut cmd, &x.path());
|
||||
search_schempath(search_behavior, &pattern, &mut output_std, &mut output_std_csv, &mut output_file_csv, &mut output_file, &mut lock, &mut file_out, schematic, x.path().file_name().unwrap().to_str().unwrap().to_string());
|
||||
}
|
||||
}
|
||||
Err(e) => cmd.error(ErrorKind::Io, format!("Error while reading dir: {}", e.to_string())).exit()
|
||||
}
|
||||
}
|
||||
Err(e) => cmd.error(ErrorKind::Io, format!("Error while reading dir: {}", e.to_string())).exit()
|
||||
}
|
||||
Err(e) => cmd.error(ErrorKind::Io, format!("Expected to be a dir: {}", e.to_string())).exit()
|
||||
}
|
||||
} else {
|
||||
let schematic = load_schem(&mut cmd, &path);
|
||||
search_schempath(search_behavior, &pattern, &mut output_std, &mut output_std_csv, &mut output_file_csv, &mut output_file, &mut lock, &mut file_out, schematic, schem.get_name());
|
||||
}
|
||||
Err(e) => cmd.error(ErrorKind::Io, format!("Expected to be a dir: {}", e.to_string())).exit()
|
||||
}
|
||||
} else {
|
||||
search_schempath(&mut cmd, search_behavior, &pattern, &mut output_std, &mut output_std_csv, &mut output_file_csv, &mut output_file, &mut lock, &mut file_out, path)
|
||||
#[cfg(feature = "sql")]
|
||||
SchematicSupplierType::SQL(schem) => {
|
||||
search_schempath(search_behavior, &pattern, &mut output_std, &mut output_std_csv, &mut output_file_csv, &mut output_file, &mut lock, &mut file_out, schem.get_schematic().unwrap(), schem.get_name());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn search_schempath(cmd: &mut Command, search_behavior: SearchBehavior, pattern: &Schematic, output_std: &mut bool, output_std_csv: &mut bool, output_file_csv: &mut bool, output_file: &mut bool, stdout: &mut StdoutLock, file_out: &mut Option<BufWriter<File>>, schem_path: &Path) {
|
||||
let schematic = match Schematic::load(schem_path) {
|
||||
fn load_schem(cmd: &mut Command, schem_path: &Path) -> Schematic {
|
||||
match Schematic::load(schem_path) {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
cmd.error(ErrorKind::Io, format!("Error while loading Schematic ({}): {}", schem_path.file_name().unwrap().to_str().unwrap(), e.to_string())).exit();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn search_schempath(search_behavior: SearchBehavior, pattern: &Schematic, output_std: &mut bool, output_std_csv: &mut bool, output_file_csv: &mut bool, output_file: &mut bool, stdout: &mut StdoutLock, file_out: &mut Option<BufWriter<File>>, schematic: Schematic, schem_name: String) {
|
||||
if *output_std {
|
||||
writeln!(stdout, "Searching in schematic: {}", schem_path.file_name().unwrap().to_str().unwrap()).unwrap();
|
||||
writeln!(stdout, "Searching in schematic: {}", schem_name).unwrap();
|
||||
}
|
||||
if *output_file {
|
||||
writeln!(file_out.as_mut().unwrap(), "Searching in schematic: {}", schem_path.file_name().unwrap().to_str().unwrap()).unwrap();
|
||||
writeln!(file_out.as_mut().unwrap(), "Searching in schematic: {}", schem_name).unwrap();
|
||||
}
|
||||
|
||||
let matches = search(schematic, pattern, search_behavior);
|
||||
@ -191,13 +236,13 @@ fn search_schempath(cmd: &mut Command, search_behavior: SearchBehavior, pattern:
|
||||
writeln!(stdout, "Found match at x: {}, y: {}, z: {}, % = {}", x.0, x.1, x.2, x.3).unwrap();
|
||||
}
|
||||
if *output_std_csv {
|
||||
writeln!(stdout, "{},{},{},{},{}", schem_path.file_name().unwrap().to_str().unwrap(), x.0, x.1, x.2, x.3).unwrap();
|
||||
writeln!(stdout, "{},{},{},{},{}", schem_name, x.0, x.1, x.2, x.3).unwrap();
|
||||
}
|
||||
if *output_file {
|
||||
writeln!(file_out.as_mut().unwrap(), "Found match at x: {}, y: {}, z: {}, % = {}", x.0, x.1, x.2, x.3).unwrap();
|
||||
}
|
||||
if *output_file_csv {
|
||||
writeln!(file_out.as_mut().unwrap(), "{},{},{},{},{}", schem_path.file_name().unwrap().to_str().unwrap(), x.0, x.1, x.2, x.3).unwrap();
|
||||
writeln!(file_out.as_mut().unwrap(), "{},{},{},{},{}", schem_name, x.0, x.1, x.2, x.3).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user