app restructuring

This commit is contained in:
sreedev
2023-01-01 23:11:51 -05:00
parent ecbedadd26
commit 8e4eddc145
6 changed files with 52 additions and 51 deletions

View File

@@ -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<String>,
/// Delete files by algorithm (default = oldest) [options = oldest | newest]
#[arg(short, long)]
pub delete: Option<String>,
/// Run Deduplicator on dir different from pwd
#[arg(long)]
pub dir: Option<PathBuf>,
/// Don't use cache for indexing files (default = true)
#[arg(long, short)]
pub nocache: bool
}

View File

@@ -1,5 +1,5 @@
use anyhow::Result;
use crate::cli::App;
use crate::params::App;
#[derive(Debug, Clone)]
pub struct File {

View File

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

View File

@@ -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<File>) {
pub fn print(duplicates: Vec<File>, opts: &App) {
print_divider();
println!(
"| {0: <16} | {1: <35} | {2: <16} | {3: <32} |",
@@ -49,7 +49,7 @@ pub fn print(duplicates: Vec<File>) {
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()
);

40
src/params.rs Normal file
View File

@@ -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<String>,
/// Run Deduplicator on dir different from pwd
#[arg(long)]
pub dir: Option<PathBuf>,
/// Don't use cache for indexing files (default = true)
#[arg(long, short)]
pub nocache: bool,
}
impl App {
pub fn get_directory(&self) -> Result<String> {
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)
}
}

View File

@@ -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<Vec<File>> {
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<String> {
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<PathBuf> {
opts.types
.clone()
@@ -58,7 +38,7 @@ fn is_indexed_file(path: &String, indexed: &Vec<File>) -> bool {
}
fn scan(app_opts: &App, connection: &sqlite::Connection) -> Result<Vec<String>> {
let directory = get_directory(app_opts)?;
let directory = app_opts.get_directory()?;
let glob_patterns: Vec<PathBuf> = get_glob_patterns(&app_opts, &directory);
let indexed_paths = database::indexed_paths(connection)?;
let files: Vec<String> = glob_patterns