2024-03-30 04:35:42 +00:00
|
|
|
use anyhow::Error;
|
2024-04-08 22:35:39 +00:00
|
|
|
use filesystem_hashing::snapshot::{Snapshot, SnapshotChangeType, SnapshotCompareResult};
|
|
|
|
use filesystem_hashing::{compare_snapshots, import_snapshot};
|
2024-03-18 22:20:57 +00:00
|
|
|
|
2024-04-14 18:42:17 +00:00
|
|
|
#[derive(Debug)]
|
2024-03-18 22:20:57 +00:00
|
|
|
pub struct CompareMode {
|
|
|
|
left: Snapshot,
|
|
|
|
right: Snapshot,
|
2024-03-29 04:18:38 +00:00
|
|
|
selection: Option<String>,
|
2024-03-29 20:38:58 +00:00
|
|
|
count_only: Option<bool>,
|
2024-04-08 22:38:35 +00:00
|
|
|
#[allow(unused)]
|
2024-03-18 22:35:19 +00:00
|
|
|
result_type: SnapshotChangeType,
|
|
|
|
results: SnapshotCompareResult,
|
2024-03-18 22:20:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CompareMode {
|
2024-04-08 22:38:35 +00:00
|
|
|
pub fn new(
|
|
|
|
left: String,
|
|
|
|
right: String,
|
|
|
|
selection: Option<String>,
|
|
|
|
count_only: Option<bool>,
|
|
|
|
) -> CompareMode {
|
2024-03-30 04:29:29 +00:00
|
|
|
let left = import_snapshot(left).unwrap_or_default();
|
|
|
|
let right = import_snapshot(right).unwrap_or_default();
|
2024-03-18 22:20:57 +00:00
|
|
|
|
2024-03-18 22:28:33 +00:00
|
|
|
CompareMode {
|
2024-03-18 22:31:45 +00:00
|
|
|
left,
|
|
|
|
right,
|
2024-03-29 04:18:38 +00:00
|
|
|
selection,
|
2024-03-29 20:38:58 +00:00
|
|
|
count_only,
|
2024-03-18 22:35:19 +00:00
|
|
|
result_type: SnapshotChangeType::None,
|
2024-03-18 22:28:33 +00:00
|
|
|
results: SnapshotCompareResult {
|
|
|
|
created: vec![],
|
|
|
|
deleted: vec![],
|
|
|
|
changed: vec![],
|
|
|
|
},
|
|
|
|
}
|
2024-03-18 22:20:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 04:21:51 +00:00
|
|
|
impl CompareMode {
|
2024-03-30 04:35:42 +00:00
|
|
|
pub(crate) fn run(&mut self) -> Result<(), Error> {
|
2024-03-29 04:18:38 +00:00
|
|
|
let selector = match &self.selection {
|
2024-03-26 17:14:24 +00:00
|
|
|
None => "none",
|
2024-04-08 22:38:35 +00:00
|
|
|
Some(r) => r.as_str(),
|
2024-03-18 22:47:38 +00:00
|
|
|
};
|
|
|
|
|
2024-03-25 20:51:52 +00:00
|
|
|
let results = match compare_snapshots(self.left.clone(), self.right.clone()) {
|
|
|
|
Some(x) => x,
|
|
|
|
None => panic!("Error Comparing Snapshot"),
|
|
|
|
};
|
2024-03-18 22:35:19 +00:00
|
|
|
self.results = results.1;
|
|
|
|
self.result_type = results.0;
|
2024-03-18 22:47:38 +00:00
|
|
|
|
2024-03-29 20:38:58 +00:00
|
|
|
macro_rules! print_if_not_empty {
|
2024-04-08 22:38:35 +00:00
|
|
|
($ret:expr, $co:expr) => {
|
|
|
|
if let Some(count_only) = $co {
|
2024-03-29 20:38:58 +00:00
|
|
|
if count_only {
|
|
|
|
println!("{}", $ret.len());
|
|
|
|
} else {
|
|
|
|
$ret.iter().for_each(|e| println!("{e}"));
|
|
|
|
println!("Created: {:?}", $ret.len());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$ret.iter().for_each(|e| println!("{e}"));
|
|
|
|
println!("Created: {:?}", $ret.len());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-08 22:38:35 +00:00
|
|
|
match selector {
|
2024-03-18 22:47:38 +00:00
|
|
|
"created" => {
|
2024-03-29 20:38:58 +00:00
|
|
|
print_if_not_empty!(self.results.created, self.count_only);
|
2024-03-26 17:14:24 +00:00
|
|
|
}
|
2024-03-18 22:47:38 +00:00
|
|
|
"deleted" => {
|
2024-03-29 20:38:58 +00:00
|
|
|
print_if_not_empty!(self.results.deleted, self.count_only);
|
2024-03-26 17:14:24 +00:00
|
|
|
}
|
2024-03-18 22:47:38 +00:00
|
|
|
"changed" => {
|
2024-03-29 20:38:58 +00:00
|
|
|
print_if_not_empty!(self.results.changed, self.count_only);
|
2024-03-18 22:47:38 +00:00
|
|
|
}
|
2024-03-18 22:54:57 +00:00
|
|
|
"none" => {
|
2024-03-18 22:47:38 +00:00
|
|
|
println!("Created: {:?}", self.results.created.len());
|
|
|
|
println!("Deleted: {:?}", self.results.deleted.len());
|
|
|
|
println!("Changed: {:?}", self.results.changed.len());
|
2024-03-26 17:14:24 +00:00
|
|
|
}
|
2024-03-18 22:54:57 +00:00
|
|
|
_ => {
|
2024-03-30 04:35:42 +00:00
|
|
|
println!("Created: {:?}", self.results.created.len());
|
|
|
|
println!("Deleted: {:?}", self.results.deleted.len());
|
|
|
|
println!("Changed: {:?}", self.results.changed.len());
|
2024-03-18 22:47:38 +00:00
|
|
|
}
|
2024-04-08 22:38:35 +00:00
|
|
|
};
|
|
|
|
Ok(())
|
2024-03-29 20:38:58 +00:00
|
|
|
}
|
2024-03-18 22:20:57 +00:00
|
|
|
}
|
2024-03-21 21:24:08 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::comparemode::CompareMode;
|
2024-03-21 22:51:45 +00:00
|
|
|
use crate::createmode::CreateMode;
|
2024-04-14 18:42:33 +00:00
|
|
|
use crate::options::{Arguments, Commands};
|
2024-03-26 17:14:24 +00:00
|
|
|
use std::env;
|
|
|
|
use std::fmt::format;
|
2024-03-21 21:24:08 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compare_mode() {
|
2024-03-21 22:50:27 +00:00
|
|
|
let user = whoami::username();
|
2024-03-25 16:36:46 +00:00
|
|
|
println!("{user}");
|
|
|
|
|
2024-03-21 22:50:27 +00:00
|
|
|
let left = format!("/home/{}/test1", user);
|
2024-03-25 17:00:00 +00:00
|
|
|
let left_dir = format!("/home/{}/Documents/", user);
|
2024-03-25 16:36:46 +00:00
|
|
|
println!("{left}");
|
2024-03-21 22:50:27 +00:00
|
|
|
let right = format!("/home/{}/test2", user);
|
2024-03-25 17:00:00 +00:00
|
|
|
let right_dir = format!("/home/{}/Documents/", user);
|
2024-03-25 16:36:46 +00:00
|
|
|
println!("{right}");
|
|
|
|
|
2024-04-14 18:42:17 +00:00
|
|
|
let mut n1 = CreateMode::new(left.clone(), "/etc".to_string());
|
|
|
|
let _ = n1.run();
|
|
|
|
let mut n2 = CreateMode::new(right.clone(), "/etc".to_string());
|
|
|
|
let _ = n2.run();
|
2024-03-21 22:51:45 +00:00
|
|
|
|
2024-04-14 18:42:17 +00:00
|
|
|
let cm = CompareMode::new(left, right, None, None);
|
|
|
|
|
|
|
|
// println!("{:?}", cm);
|
|
|
|
assert!(cm.left.file_hashes.lock().unwrap().len() > 0);
|
|
|
|
assert!(cm.right.file_hashes.lock().unwrap().len() > 0);
|
|
|
|
// assert!(cm.results. > 0);
|
|
|
|
// todo()! finish writing tests
|
2024-03-21 21:24:08 +00:00
|
|
|
}
|
|
|
|
}
|