From b09f1f958423dee8c235f2eeb9c148b73936830f Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 24 Oct 2021 13:26:37 +0530 Subject: rework cli, fix is now a flag, implement dry-run mode --- bin/src/main.rs | 73 +++++++++++++++++++++++++++------------------------------ 1 file changed, 34 insertions(+), 39 deletions(-) (limited to 'bin/src/main.rs') diff --git a/bin/src/main.rs b/bin/src/main.rs index 6f0343e..d0f69a0 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -6,55 +6,50 @@ mod traits; use std::io; -use crate::{ - err::{FixErr, StatixErr}, - traits::WriteDiagnostic, -}; +use crate::{err::{StatixErr, FixErr}, traits::WriteDiagnostic}; use clap::Clap; -use config::{FixConfig, LintConfig, Opts, SubCommand}; +use config::{FixConfig, LintConfig, Opts}; use similar::TextDiff; fn _main() -> Result<(), StatixErr> { let opts = Opts::parse(); - match opts.subcmd { - Some(SubCommand::Fix(_)) => { - let fix_config = FixConfig::from_opts(opts)?; - let vfs = fix_config.vfs()?; - for entry in vfs.iter() { - match fix::fix(entry.contents) { - Ok(fix_result) => { - let text_diff = TextDiff::from_lines(entry.contents, &fix_result.src); - let old_file = format!("{}", entry.file_path.display()); - let new_file = format!("{} [fixed]", entry.file_path.display()); - println!( - "{}", - text_diff - .unified_diff() - .context_radius(4) - .header(&old_file, &new_file) - ); - } - Err(e) => eprintln!("{}", FixErr::Parse(entry.file_path.to_path_buf(), e)), + if opts.fix { + let fix_config = FixConfig::from_opts(opts)?; + let vfs = fix_config.vfs()?; + for entry in vfs.iter() { + if let Some(fix_result) = fix::fix(entry.contents) { + if fix_config.diff_only { + let text_diff = TextDiff::from_lines(entry.contents, &fix_result.src); + let old_file = format!("{}", entry.file_path.display()); + let new_file = format!("{} [fixed]", entry.file_path.display()); + println!( + "{}", + text_diff + .unified_diff() + .context_radius(4) + .header(&old_file, &new_file) + ); + } else { + let path = entry.file_path; + std::fs::write(path, &*fix_result.src).map_err(FixErr::InvalidPath)?; } } } - None => { - let lint_config = LintConfig::from_opts(opts)?; - let vfs = lint_config.vfs()?; - let (lints, errors): (Vec<_>, Vec<_>) = - vfs.iter().map(lint::lint).partition(Result::is_ok); - let lint_results = lints.into_iter().map(Result::unwrap); - let errors = errors.into_iter().map(Result::unwrap_err); + } else { + let lint_config = LintConfig::from_opts(opts)?; + let vfs = lint_config.vfs()?; + let (lints, errors): (Vec<_>, Vec<_>) = vfs.iter().map(lint::lint).partition(Result::is_ok); + let lint_results = lints.into_iter().map(Result::unwrap); + let errors = errors.into_iter().map(Result::unwrap_err); - let mut stderr = io::stderr(); - lint_results.for_each(|r| { - stderr.write(&r, &vfs, lint_config.format).unwrap(); - }); - errors.for_each(|e| { - eprintln!("{}", e); - }); - } + let mut stdout = io::stdout(); + lint_results.for_each(|r| { + stdout.write(&r, &vfs, lint_config.format).unwrap(); + }); + errors.for_each(|e| { + eprintln!("{}", e); + }); } Ok(()) } -- cgit v1.2.3