diff options
author | Akshay <[email protected]> | 2021-10-24 08:56:37 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-10-24 08:56:37 +0100 |
commit | b09f1f958423dee8c235f2eeb9c148b73936830f (patch) | |
tree | 0bab5509f6dfcb46af9aa686c313f556c7ae5f0b /bin/src/main.rs | |
parent | 5de0ba055cef7f2dc5451b1eaf0857deb77ae009 (diff) |
rework cli, fix is now a flag, implement dry-run mode
Diffstat (limited to 'bin/src/main.rs')
-rw-r--r-- | bin/src/main.rs | 73 |
1 files changed, 34 insertions, 39 deletions
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; | |||
6 | 6 | ||
7 | use std::io; | 7 | use std::io; |
8 | 8 | ||
9 | use crate::{ | 9 | use crate::{err::{StatixErr, FixErr}, traits::WriteDiagnostic}; |
10 | err::{FixErr, StatixErr}, | ||
11 | traits::WriteDiagnostic, | ||
12 | }; | ||
13 | 10 | ||
14 | use clap::Clap; | 11 | use clap::Clap; |
15 | use config::{FixConfig, LintConfig, Opts, SubCommand}; | 12 | use config::{FixConfig, LintConfig, Opts}; |
16 | use similar::TextDiff; | 13 | use similar::TextDiff; |
17 | 14 | ||
18 | fn _main() -> Result<(), StatixErr> { | 15 | fn _main() -> Result<(), StatixErr> { |
19 | let opts = Opts::parse(); | 16 | let opts = Opts::parse(); |
20 | match opts.subcmd { | 17 | if opts.fix { |
21 | Some(SubCommand::Fix(_)) => { | 18 | let fix_config = FixConfig::from_opts(opts)?; |
22 | let fix_config = FixConfig::from_opts(opts)?; | 19 | let vfs = fix_config.vfs()?; |
23 | let vfs = fix_config.vfs()?; | 20 | for entry in vfs.iter() { |
24 | for entry in vfs.iter() { | 21 | if let Some(fix_result) = fix::fix(entry.contents) { |
25 | match fix::fix(entry.contents) { | 22 | if fix_config.diff_only { |
26 | Ok(fix_result) => { | 23 | let text_diff = TextDiff::from_lines(entry.contents, &fix_result.src); |
27 | let text_diff = TextDiff::from_lines(entry.contents, &fix_result.src); | 24 | let old_file = format!("{}", entry.file_path.display()); |
28 | let old_file = format!("{}", entry.file_path.display()); | 25 | let new_file = format!("{} [fixed]", entry.file_path.display()); |
29 | let new_file = format!("{} [fixed]", entry.file_path.display()); | 26 | println!( |
30 | println!( | 27 | "{}", |
31 | "{}", | 28 | text_diff |
32 | text_diff | 29 | .unified_diff() |
33 | .unified_diff() | 30 | .context_radius(4) |
34 | .context_radius(4) | 31 | .header(&old_file, &new_file) |
35 | .header(&old_file, &new_file) | 32 | ); |
36 | ); | 33 | } else { |
37 | } | 34 | let path = entry.file_path; |
38 | Err(e) => eprintln!("{}", FixErr::Parse(entry.file_path.to_path_buf(), e)), | 35 | std::fs::write(path, &*fix_result.src).map_err(FixErr::InvalidPath)?; |
39 | } | 36 | } |
40 | } | 37 | } |
41 | } | 38 | } |
42 | None => { | 39 | } else { |
43 | let lint_config = LintConfig::from_opts(opts)?; | 40 | let lint_config = LintConfig::from_opts(opts)?; |
44 | let vfs = lint_config.vfs()?; | 41 | let vfs = lint_config.vfs()?; |
45 | let (lints, errors): (Vec<_>, Vec<_>) = | 42 | let (lints, errors): (Vec<_>, Vec<_>) = vfs.iter().map(lint::lint).partition(Result::is_ok); |
46 | vfs.iter().map(lint::lint).partition(Result::is_ok); | 43 | let lint_results = lints.into_iter().map(Result::unwrap); |
47 | let lint_results = lints.into_iter().map(Result::unwrap); | 44 | let errors = errors.into_iter().map(Result::unwrap_err); |
48 | let errors = errors.into_iter().map(Result::unwrap_err); | ||
49 | 45 | ||
50 | let mut stderr = io::stderr(); | 46 | let mut stdout = io::stdout(); |
51 | lint_results.for_each(|r| { | 47 | lint_results.for_each(|r| { |
52 | stderr.write(&r, &vfs, lint_config.format).unwrap(); | 48 | stdout.write(&r, &vfs, lint_config.format).unwrap(); |
53 | }); | 49 | }); |
54 | errors.for_each(|e| { | 50 | errors.for_each(|e| { |
55 | eprintln!("{}", e); | 51 | eprintln!("{}", e); |
56 | }); | 52 | }); |
57 | } | ||
58 | } | 53 | } |
59 | Ok(()) | 54 | Ok(()) |
60 | } | 55 | } |