more gooder output

This commit is contained in:
Lynne Megido 2021-02-06 13:24:13 +10:00
parent b92ceabfe2
commit 6d49336e6b
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90
2 changed files with 33 additions and 11 deletions

View File

@ -24,6 +24,7 @@ use smartstring::alias::String;
use clap::Clap; use clap::Clap;
use log::{debug, trace, info, warn, error}; use log::{debug, trace, info, warn, error};
use rayon::prelude::*; use rayon::prelude::*;
use std::fmt::{self, Display};
struct Findings { struct Findings {
file: PathBuf, file: PathBuf,
@ -38,6 +39,22 @@ impl Findings {
} }
} }
enum ScanError {
File,
Mime
}
impl Display for ScanError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}",
match self {
Self::File => "Couldn't read file",
Self::Mime => "Couldn't determine mime type"
}
)
}
}
// TODO: test if this actually works on a windows machine // TODO: test if this actually works on a windows machine
#[cfg(windows)] #[cfg(windows)]
fn is_hidden(entry: &DirEntry) -> bool { fn is_hidden(entry: &DirEntry) -> bool {
@ -87,8 +104,8 @@ fn main() {
let mut builder = env_logger::Builder::from_default_env(); let mut builder = env_logger::Builder::from_default_env();
builder builder
// .format(|buf, r| writeln!(buf, "{} - {}", r.level(), r.args())) // .format(|buf, r| writeln!(buf, "{} - {}", r.level(), r.args()))
.format_module_path(false) .format_module_path(false) // don't include module in logs, as it's not necessary
.format_timestamp(None) .format_timestamp(None) // don't include timestamps (unnecessary, and the feature flag is disabled anyway)
.init(); .init();
let db = xdg_mime::SharedMimeInfo::new(); let db = xdg_mime::SharedMimeInfo::new();
@ -103,23 +120,23 @@ fn main() {
trace!("Found {} items to check", entries.len()); trace!("Found {} items to check", entries.len());
let results: Vec<Result<Findings, PathBuf>> = entries let results: Vec<Result<Findings, (ScanError, PathBuf)>> = entries
.par_iter() .par_iter()
.map(|entry: &DirEntry | { .map(|entry: &DirEntry | {
// try to determine mimetype for this entry // try to determine mimetype for this entry
let result = inspectors::mime_type(&db, entry.path()); let result = inspectors::mime_type(&db, entry.path());
if let Err(error) = result { if let Err(_) = result {
// an error occurred while trying to read the file // an error occurred while trying to read the file
error!("{}: {}", entry.path().to_string_lossy(), error); // error!("{}: {}", entry.path().to_string_lossy(), error);
return Err(entry.path().to_path_buf()); return Err((ScanError::File, entry.path().to_path_buf()));
} }
let result = result.unwrap(); let result = result.unwrap();
if result.is_none() { if result.is_none() {
// the file was read successfully, but we were unable to determine its mimetype // the file was read successfully, but we were unable to determine its mimetype
warn!("Couldn't determine mimetype for {}", entry.path().to_string_lossy()); // warn!("Couldn't determine mimetype for {}", entry.path().to_string_lossy());
return Err(entry.path().to_path_buf()); return Err((ScanError::Mime, entry.path().to_path_buf()));
} }
let result = result.unwrap(); let result = result.unwrap();
@ -155,7 +172,7 @@ fn main() {
trace!("{:?} is totally fine", r.file) trace!("{:?} is totally fine", r.file)
} }
} }
Err(f) => warn!("{:#?}: Error 0uo", f) Err(f) => warn!("{:#?}: Error 0uo - {}", f.1, f.0)
} }
} }

View File

@ -12,8 +12,13 @@ pub struct Parameters {
#[clap(short, long)] #[clap(short, long)]
pub scan_hidden: bool, pub scan_hidden: bool,
/// Directories to process /// Output format to use. See "--help formats" for more information.
#[clap(name = "DIRS", default_value = ".", parse(from_os_str))] #[clap(short, long, default_value="script", possible_values = &["script", "text"])]
pub output_format: String,
/// Directory to process
// TODO: right now this can only take a single directory - should this be improved?
#[clap(name = "DIR", default_value = ".", parse(from_os_str))]
// dirs: PathBuf // dirs: PathBuf
pub dirs: PathBuf, pub dirs: PathBuf,
} }