move main() to the top of main.rs because wynaut

This commit is contained in:
Lynne Megido 2021-02-28 22:20:15 +10:00
parent 67fb03821d
commit e294f56ecf
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90

View file

@ -55,6 +55,87 @@ cfg_if! {
}
}
fn main() {
let args: parameters::Parameters = parameters::Parameters::parse();
let mut builder = env_logger::Builder::from_env(
Env::new().filter_or("RUST_LOG", "INFO")
);
builder
// .format(|buf, r| writeln!(buf, "{} - {}", r.level(), r.args()))
.format_module_path(false) // don't include module in logs, as it's not necessary
.format_timestamp(None) // don't include timestamps (unnecessary, and the feature flag is disabled anyway)
// .target(env_logger::Target::Stdout) // log to stdout rather than stderr
.init();
init_db();
debug!("Iterating directory: {:?}", args.dirs);
let extensions = args.extensions();
debug!("Checking files with extensions: {:?}", extensions);
let entries = scan_directory(&args.dirs, &extensions, args.scan_hidden);
if entries.is_none() {
// no need to log anything for fatal errors - fif will already have printed something obvious like
// "[ERROR] /fake/path: No such file or directory (os error 2)". we can assume that if this has happened, the dir
// given as input doesn't exist or is otherwise unreadable.
exit(exitcode::NOINPUT);
}
let entries = entries.unwrap();
if entries.is_empty() {
warn!("No files matching requested options found.");
exit(exitcode::OK);
}
trace!("Found {} items to check", entries.len());
let results: Vec<_> = scan_from_walkdir(&entries)
.into_iter()
.filter(
|result| result.is_err() || !result.as_ref().unwrap().valid,
// TODO: find a way to trace! the valid files without doing ↓
// || if result.as_ref().unwrap().valid { trace!("{:?} is fine", result.as_ref().unwrap().file); false } else { true }
)
.collect();
for result in &results {
match result {
Ok(r) => {
debug!(
"{:?} should have file extension {}",
r.file,
r.recommended_extension().unwrap_or_else(|| "???".into())
)
}
Err(f) => warn!("{:#?}: Error 0uo - {}", f.1, f.0),
}
}
if results.is_empty() {
info!("All files have valid extensions!");
exit(0);
}
match args.output_format {
OutputFormat::Script => {
let s = Script::new();
if s.write_all(&results, &mut BufWriter::new(stdout().lock())).is_err() {
error!("Failed to write to stdout.");
exit(exitcode::IOERR);
}
}
OutputFormat::Text => todo!(),
}
debug!("Done");
}
cfg_if! {
if #[cfg(windows)] {
fn is_hidden(entry: &DirEntry) -> bool {
@ -212,84 +293,3 @@ fn init_db() {
}
}
}
fn main() {
let args: parameters::Parameters = parameters::Parameters::parse();
let mut builder = env_logger::Builder::from_env(
Env::new().filter_or("RUST_LOG", "INFO")
);
builder
// .format(|buf, r| writeln!(buf, "{} - {}", r.level(), r.args()))
.format_module_path(false) // don't include module in logs, as it's not necessary
.format_timestamp(None) // don't include timestamps (unnecessary, and the feature flag is disabled anyway)
// .target(env_logger::Target::Stdout) // log to stdout rather than stderr
.init();
init_db();
debug!("Iterating directory: {:?}", args.dirs);
let extensions = args.extensions();
debug!("Checking files with extensions: {:?}", extensions);
let entries = scan_directory(&args.dirs, &extensions, args.scan_hidden);
if entries.is_none() {
// no need to log anything for fatal errors - fif will already have printed something obvious like
// "[ERROR] /fake/path: No such file or directory (os error 2)". we can assume that if this has happened, the dir
// given as input doesn't exist or is otherwise unreadable.
exit(exitcode::NOINPUT);
}
let entries = entries.unwrap();
if entries.is_empty() {
warn!("No files matching requested options found.");
exit(exitcode::OK);
}
trace!("Found {} items to check", entries.len());
let results: Vec<_> = scan_from_walkdir(&entries)
.into_iter()
.filter(
|result| result.is_err() || !result.as_ref().unwrap().valid,
// TODO: find a way to trace! the valid files without doing ↓
// || if result.as_ref().unwrap().valid { trace!("{:?} is fine", result.as_ref().unwrap().file); false } else { true }
)
.collect();
for result in &results {
match result {
Ok(r) => {
debug!(
"{:?} should have file extension {}",
r.file,
r.recommended_extension().unwrap_or_else(|| "???".into())
)
}
Err(f) => warn!("{:#?}: Error 0uo - {}", f.1, f.0),
}
}
if results.is_empty() {
info!("All files have valid extensions!");
exit(0);
}
match args.output_format {
OutputFormat::Script => {
let s = Script::new();
if s.write_all(&results, &mut BufWriter::new(stdout().lock())).is_err() {
error!("Failed to write to stdout.");
exit(exitcode::IOERR);
}
}
OutputFormat::Text => todo!(),
}
debug!("Done");
}