Merge pull request #3 from helloimalemur/add_clap

add clap
This commit is contained in:
Koonts 2024-03-29 10:47:15 -04:00 committed by GitHub
commit e299910c4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 99 additions and 161 deletions

View File

@ -7,5 +7,6 @@ edition = "2021"
[dependencies] [dependencies]
Fasching = "0.1.19" Fasching = "0.1.19"
clap = { version = "4.5.4", features = ["derive"] }
#Fasching = {path = "../Fasching/"} #Fasching = {path = "../Fasching/"}
whoami = "1.5.1" whoami = "1.5.1"

View File

@ -1,17 +1,37 @@
# sys-compare # sys-compare
#### work in progress
### Modes
```shell
Usage: sys-compare <COMMAND>
Commands:
create
compare
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
```
### Create Snapshot ### Create Snapshot
## ./sys-compare create [snapshot] [root_dir]
```shell ```shell
cargo run -- create ./test1.snap /home/foxx/Documents/ Usage: sys-compare create --root-dir <ROOT_DIR> --output-path <OUTPUT_PATH>
cargo run -- create ./test2.snap /home/foxx/Documents/
Options:
-r, --root-dir <ROOT_DIR>
-o, --output-path <OUTPUT_PATH>
-h, --help Print help
``` ```
### Compare Snapshots ### Compare Snapshots
## ./sys-compare compare [.snap] [.snap] [created]|[deleted]|[changed]
```shell ```shell
cargo run -- compare ./test1.snap ./test2.snap created Usage: sys-compare compare [OPTIONS] --left <LEFT> --right <RIGHT>
Options:
-l, --left <LEFT>
-r, --right <RIGHT>
-s, --selection <SELECTION>
-h, --help Print help
``` ```
## Development and Collaboration ## Development and Collaboration

View File

@ -1,24 +1,26 @@
use crate::syscompare::Comparer;
use Fasching::snapshot::{Snapshot, SnapshotChangeType, SnapshotCompareResult}; use Fasching::snapshot::{Snapshot, SnapshotChangeType, SnapshotCompareResult};
use Fasching::{compare_snapshots, import_snapshot}; use Fasching::{compare_snapshots, import_snapshot};
use crate::options::Arguments;
pub struct CompareMode { pub struct CompareMode {
left: Snapshot, left: Snapshot,
right: Snapshot, right: Snapshot,
args: Vec<String>, selection: Option<String>,
options: Arguments,
result_type: SnapshotChangeType, result_type: SnapshotChangeType,
results: SnapshotCompareResult, results: SnapshotCompareResult,
} }
impl CompareMode { impl CompareMode {
pub fn new(args: Vec<String>, left: String, right: String) -> CompareMode { pub fn new(options: Arguments, left: String, right: String, selection: Option<String>) -> CompareMode {
let left = import_snapshot(left); let left = import_snapshot(left);
let right = import_snapshot(right); let right = import_snapshot(right);
CompareMode { CompareMode {
left, left,
right, right,
args, selection,
options,
result_type: SnapshotChangeType::None, result_type: SnapshotChangeType::None,
results: SnapshotCompareResult { results: SnapshotCompareResult {
created: vec![], created: vec![],
@ -29,11 +31,13 @@ impl CompareMode {
} }
} }
impl Comparer for CompareMode { impl CompareMode {
fn run(&mut self) { pub(crate) fn run(&mut self) {
let selector = match self.args.get(4) { let selector = match &self.selection {
None => "none", None => "none",
Some(r) => r, Some(r) => {
r.as_str()
},
}; };
let results = match compare_snapshots(self.left.clone(), self.right.clone()) { let results = match compare_snapshots(self.left.clone(), self.right.clone()) {
@ -90,11 +94,11 @@ mod tests {
let right_dir = format!("/home/{}/Documents/", user); let right_dir = format!("/home/{}/Documents/", user);
println!("{right}"); println!("{right}");
let mut n1 = CreateMode::new(vec![], left.clone(), left_dir); let mut n1 = CreateMode::new(vec![], left.clone());
n1.run(); n1.run();
let mut n2 = CreateMode::new(vec![], right.clone(), right_dir); let mut n2 = CreateMode::new(vec![], right.clone());
n2.run(); n2.run();
let cm = CompareMode::new(vec![], left.clone(), right.clone()); let cm = CompareMode::new(vec![], left.clone(), right.clone(), );
} }
} }

View File

@ -1,7 +1,5 @@
use crate::print_help;
use crate::syscompare::Comparer;
use std::process::exit; use std::process::exit;
use Fasching::hasher::HashType; use std::sync::{Arc, Mutex};
use Fasching::hasher::HashType::BLAKE3; use Fasching::hasher::HashType::BLAKE3;
use Fasching::snapshot::Snapshot; use Fasching::snapshot::Snapshot;
use Fasching::{create_snapshot, export_snapshot}; use Fasching::{create_snapshot, export_snapshot};
@ -9,31 +7,35 @@ use Fasching::{create_snapshot, export_snapshot};
pub struct CreateMode { pub struct CreateMode {
snapshot_path: String, snapshot_path: String,
root_path: String, root_path: String,
#[allow(unused)]
args: Vec<String>,
snapshot: Snapshot, snapshot: Snapshot,
} }
impl CreateMode { impl CreateMode {
pub fn new(args: Vec<String>, snapshot_path: String, root_path: String) -> CreateMode { pub fn new(snapshot_path: String, root_path: String) -> CreateMode {
if snapshot_path.replace("./", "").is_empty() { if snapshot_path.replace("./", "").is_empty() {
println!("Specify output file name"); println!("Specify output file name");
print_help();
exit(0); exit(0);
} }
let bind = root_path.clone(); let bind = root_path.clone();
let rp = bind.as_str(); let rp = bind.as_str();
CreateMode { CreateMode {
args,
snapshot_path, snapshot_path,
root_path, root_path,
snapshot: create_snapshot(rp, HashType::MD5, vec![]), snapshot: Snapshot {
file_hashes: Arc::new(Mutex::new(Default::default())),
black_list: vec![],
root_path: "".to_string(),
hash_type: BLAKE3,
uuid: "".to_string(),
date_created: 0,
},
} }
} }
} }
impl Comparer for CreateMode { impl CreateMode {
fn run(&mut self) { pub fn run(&mut self) {
let snapshot = create_snapshot(self.root_path.as_str(), BLAKE3, vec![]); let snapshot = create_snapshot(self.root_path.as_str(), BLAKE3, vec![]);
self.snapshot = snapshot.clone(); self.snapshot = snapshot.clone();
if let Ok(e) = snapshot.file_hashes.lock() { if let Ok(e) = snapshot.file_hashes.lock() {

View File

@ -1,44 +1,29 @@
pub mod comparemode; pub mod comparemode;
pub mod createmode; pub mod createmode;
pub mod syscompare; mod options;
use crate::syscompare::SysCompareApp; use clap::{FromArgMatches, Parser};
use crate::syscompare::SysCompareMode::{Compare, Create}; use crate::comparemode::CompareMode;
use std::env::args; use crate::createmode::CreateMode;
use std::process::exit; use crate::options::{Arguments, Commands};
fn main() { fn main() {
let args: Vec<String> = args().collect(); let options = Arguments::parse();
// println!("{:#?}", args); // testing let movable = options.clone();
let app = match args.get(1) { let _app = match options.command {
None => { Commands::Create { root_dir, output_path } => {
print_help(); let mut create =
exit(0); CreateMode::new(output_path, root_dir);
} println!("Creating snapshot..");
Some(mode) => { create.run()
// app mode },
let m = mode.as_str(); Commands::Compare { left, right, selection } => {
let app_mode = match m { println!("Running snapshot comparison..");
"create" => Create, let mut compare = CompareMode::new(movable.clone(), left, right, selection);
"compare" => Compare, compare.run()
_ => {
println!("Invalid MODE argument");
print_help();
exit(0);
}
};
SysCompareApp::new(app_mode, args)
} }
}; };
app.run()
}
pub fn print_help() {
println!("### Create Snapshot\n## ./sys-compare create [snapshot] [root_dir]");
println!("### Compare Snapshots\n## ./sys-compare compare [.snap] [.snap] [created]|[deleted]|[changed]");
} }
// #[cfg(test)] // #[cfg(test)]

26
src/options.rs Normal file
View File

@ -0,0 +1,26 @@
use clap::{Parser, Subcommand};
#[derive(Parser, Clone, Debug)]
pub struct Arguments {
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand, Clone, Debug)]
pub enum Commands {
Create {
#[arg(short, long)]
root_dir: String,
#[arg(short, long)]
output_path: String,
},
Compare {
#[arg(short, long)]
left: String,
#[arg(short, long)]
right: String,
#[arg(short, long)]
selection: Option<String>,
},
}

View File

@ -1,100 +0,0 @@
use crate::comparemode::CompareMode;
use crate::createmode::CreateMode;
use crate::print_help;
use std::collections::HashMap;
use std::process::exit;
use std::sync::{Arc, Mutex};
use Fasching::snapshot::Snapshot;
pub enum SysCompareMode {
Create,
Compare,
}
pub struct SysCompareApp {
mode: SysCompareMode,
args: Vec<String>,
#[allow(unused)]
comparatives: Arc<Mutex<HashMap<String, Snapshot>>>,
}
impl SysCompareApp {
pub fn new(mode: SysCompareMode, args: Vec<String>) -> SysCompareApp {
SysCompareApp {
mode,
args,
comparatives: Arc::new(Mutex::new(HashMap::new())),
}
}
pub fn run(&self) {
println!("running");
match self.mode {
SysCompareMode::Create => {
let snapshot_path = match self.args.get(2) {
None => {
println!("Missing hash dir path as second argument");
print_help();
exit(0);
}
Some(r) => not_empty(r),
};
let root_dir = match self.args.get(3) {
None => {
println!("Missing hash dir path as second argument");
print_help();
exit(0);
}
Some(r) => not_empty(r),
};
let mut create =
CreateMode::new(self.args.clone(), snapshot_path.clone(), root_dir.clone());
create.run()
}
SysCompareMode::Compare => {
let left = match self.args.get(2) {
None => {
println!("Missing hash dir path as second argument");
print_help();
exit(0);
}
Some(r) => not_empty(r),
};
let right = match self.args.get(3) {
None => {
println!("Missing output path as third argument");
print_help();
exit(0);
}
Some(r) => not_empty(r),
};
let mut compare = CompareMode::new(self.args.clone(), left, right);
compare.run()
}
}
}
}
fn not_empty(r: &String) -> String {
if r.replace("./", "").is_empty() {
println!("Specify input file name");
print_help();
exit(0);
} else {
r.to_string()
}
}
impl Default for SysCompareApp {
fn default() -> Self {
SysCompareApp {
mode: SysCompareMode::Create,
args: vec![],
comparatives: Arc::new(Mutex::new(HashMap::new())),
}
}
}
pub trait Comparer {
fn run(&mut self);
}