Finishing

This commit is contained in:
Chaoscaot
2023-03-08 15:47:51 +01:00
parent 74df144d05
commit 98d797532e
11 changed files with 823 additions and 45 deletions

View File

@ -2,10 +2,18 @@
name = "schemsearch-cli"
version = "0.1.0"
edition = "2021"
license = "AGPL-3.0-or-later"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
schemsearch-lib = { path = "../schemsearch-lib" }
schemsearch-files = { path = "../schemsearch-files" }
schemsearch-sql = { path = "../schemsearch-sql", optional = true }
clap = { version = "4.1.8", features = ["cargo"] }
futures = { version = "0.3", optional = true }
sqlx = { version = "0.6", features = [ "runtime-async-std-native-tls" , "mysql" ], optional = true }
[features]
sql = [ "dep:schemsearch-sql", "dep:futures", "dep:sqlx" ]

View File

@ -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();
}
}
}

View File

@ -0,0 +1,39 @@
use std::path::Path;
#[cfg(feature = "sql")]
use futures::executor::block_on;
use schemsearch_files::Schematic;
#[cfg(feature = "sql")]
use schemsearch_sql::{load_schemdata, SchematicNode};
pub enum SchematicSupplierType<'local> {
PATH(Box<PathSchematicSupplier<'local>>),
#[cfg(feature = "sql")]
SQL(SqlSchematicSupplier),
}
pub struct PathSchematicSupplier<'local> {
pub path: &'local Path,
}
impl PathSchematicSupplier<'_> {
pub fn get_name(&self) -> String {
self.path.file_name().unwrap().to_str().unwrap().to_string()
}
}
#[cfg(feature = "sql")]
pub struct SqlSchematicSupplier {
pub node: SchematicNode,
}
#[cfg(feature = "sql")]
impl SqlSchematicSupplier {
pub fn get_schematic(&self) -> Result<Schematic, String> {
let schemdata = block_on(load_schemdata(self.node.id));
Schematic::load_data(schemdata.as_slice())
}
pub fn get_name(&self) -> String {
format!("{} ({})", self.node.name, self.node.id)
}
}