From 8e4eddc1452cc2955f456fe69497f976be8d4a0a Mon Sep 17 00:00:00 2001 From: sreedev Date: Sun, 1 Jan 2023 23:11:51 -0500 Subject: [PATCH] app restructuring --- src/cli.rs | 19 ------------------- src/database.rs | 2 +- src/main.rs | 6 +++--- src/output.rs | 10 +++++----- src/params.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/scanner.rs | 26 +++----------------------- 6 files changed, 52 insertions(+), 51 deletions(-) delete mode 100644 src/cli.rs create mode 100644 src/params.rs diff --git a/src/cli.rs b/src/cli.rs deleted file mode 100644 index ffe1b33..0000000 --- a/src/cli.rs +++ /dev/null @@ -1,19 +0,0 @@ -use std::path::PathBuf; -use clap::Parser; - -#[derive(Parser, Debug)] -#[command(author, version, about, long_about = None)] -pub struct App { - /// Filetypes to deduplicate (default = all) - #[arg(short, long)] - pub types: Option, - /// Delete files by algorithm (default = oldest) [options = oldest | newest] - #[arg(short, long)] - pub delete: Option, - /// Run Deduplicator on dir different from pwd - #[arg(long)] - pub dir: Option, - /// Don't use cache for indexing files (default = true) - #[arg(long, short)] - pub nocache: bool -} diff --git a/src/database.rs b/src/database.rs index 2ecd934..9dafdbd 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use crate::cli::App; +use crate::params::App; #[derive(Debug, Clone)] pub struct File { diff --git a/src/main.rs b/src/main.rs index 8a9a815..eba7851 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -mod cli; +mod params; mod database; mod output; mod scanner; @@ -8,10 +8,10 @@ use clap::Parser; #[tokio::main] async fn main() -> Result<()> { - let app_args = cli::App::parse(); + let app_args = params::App::parse(); let connection = database::get_connection(&app_args)?; let duplicates = scanner::duplicates(&app_args, &connection)?; - output::print(duplicates); + output::print(duplicates, &app_args); Ok(()) } diff --git a/src/output.rs b/src/output.rs index 3e933bf..7e0a221 100644 --- a/src/output.rs +++ b/src/output.rs @@ -4,10 +4,10 @@ use chrono::DateTime; use colored::Colorize; use humansize::{format_size, DECIMAL}; use std::{collections::HashMap, fs}; +use crate::params::App; -fn format_path(path: &String) -> String { - let stringlen = path.len(); - format!("...{}", String::from(&path[(stringlen - 32)..])) +fn format_path(path: &String, opts: &App) -> String { + format!("...{}", path.replace(&opts.get_directory().unwrap(), "")) } fn file_size(path: &String) -> String { @@ -27,7 +27,7 @@ fn print_divider() { println!("-------------------+-------------------------------------+------------------+----------------------------------+"); } -pub fn print(duplicates: Vec) { +pub fn print(duplicates: Vec, opts: &App) { print_divider(); println!( "| {0: <16} | {1: <35} | {2: <16} | {3: <32} |", @@ -49,7 +49,7 @@ pub fn print(duplicates: Vec) { println!( "| {0: <16} | {1: <35} | {2: <16} | {3: <32} |", file.hash.red(), - format_path(&file.path).yellow(), + format_path(&file.path, opts).yellow(), file_size(&file.path).blue(), modified_time(&file.path).blue() ); diff --git a/src/params.rs b/src/params.rs new file mode 100644 index 0000000..375b4a4 --- /dev/null +++ b/src/params.rs @@ -0,0 +1,40 @@ +use std::path::PathBuf; +use clap::Parser; +use anyhow::Result; +use std::fs; + +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +pub struct App { + /// Filetypes to deduplicate (default = all) + #[arg(short, long)] + pub types: Option, + /// Run Deduplicator on dir different from pwd + #[arg(long)] + pub dir: Option, + /// Don't use cache for indexing files (default = true) + #[arg(long, short)] + pub nocache: bool, +} + +impl App { + pub fn get_directory(&self) -> Result { + let dir_string: String = self + .dir + .clone() + .unwrap_or(std::env::current_dir()?) + .as_os_str() + .to_str() + .unwrap() + .to_string(); + + let dir_pathbuf = PathBuf::from(&dir_string); + let dir = fs::canonicalize(&dir_pathbuf)? + .as_os_str() + .to_str() + .unwrap() + .to_string(); + + Ok(dir) + } +} diff --git a/src/scanner.rs b/src/scanner.rs index d77dfeb..2fcb29d 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -1,5 +1,5 @@ use crate::database; -use crate::{cli::App, database::File}; +use crate::{params::App, database::File}; use anyhow::Result; use glob::glob; use itertools::Itertools; @@ -10,32 +10,12 @@ use fxhash::hash32 as hasher; pub fn duplicates(app_opts: &App, connection: &sqlite::Connection) -> Result> { let scan_results = scan(app_opts, connection)?; - let base_path = get_directory(app_opts)?; + let base_path = app_opts.get_directory()?; index_files(scan_results, connection); database::duplicate_hashes(connection, &base_path) } -fn get_directory(opts: &App) -> Result { - let dir_string: String = opts - .dir - .clone() - .unwrap_or(std::env::current_dir()?) - .as_os_str() - .to_str() - .unwrap() - .to_string(); - - let dir_pathbuf = PathBuf::from(&dir_string); - let dir = fs::canonicalize(&dir_pathbuf)? - .as_os_str() - .to_str() - .unwrap() - .to_string(); - - Ok(dir) -} - fn get_glob_patterns(opts: &App, directory: &String) -> Vec { opts.types .clone() @@ -58,7 +38,7 @@ fn is_indexed_file(path: &String, indexed: &Vec) -> bool { } fn scan(app_opts: &App, connection: &sqlite::Connection) -> Result> { - let directory = get_directory(app_opts)?; + let directory = app_opts.get_directory()?; let glob_patterns: Vec = get_glob_patterns(&app_opts, &directory); let indexed_paths = database::indexed_paths(connection)?; let files: Vec = glob_patterns