-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
261 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use crate::check_user_input; | ||
use crate::commands::FixArgs; | ||
use crate::commands::Format; | ||
use crate::linter; | ||
use sqruff_lib::core::config::FluffConfig; | ||
use std::path::Path; | ||
|
||
pub(crate) fn run_fix( | ||
args: FixArgs, | ||
config: FluffConfig, | ||
ignorer: impl Fn(&Path) -> bool + Send + Sync, | ||
) -> i32 { | ||
let FixArgs { | ||
paths, | ||
force, | ||
format, | ||
} = args; | ||
let mut linter = linter(config, format); | ||
let result = linter.lint_paths(paths, true, &ignorer); | ||
|
||
if result | ||
.paths | ||
.iter() | ||
.map(|path| path.files.iter().all(|file| file.violations.is_empty())) | ||
.all(|v| v) | ||
{ | ||
let count_files = result | ||
.paths | ||
.iter() | ||
.map(|path| path.files.len()) | ||
.sum::<usize>(); | ||
println!("{} files processed, nothing to fix.", count_files); | ||
0 | ||
} else { | ||
if !force { | ||
match check_user_input() { | ||
Some(true) => { | ||
eprintln!("Attempting fixes..."); | ||
} | ||
Some(false) => return 0, | ||
None => { | ||
eprintln!("Invalid input, please enter 'Y' or 'N'"); | ||
eprintln!("Aborting..."); | ||
return 0; | ||
} | ||
} | ||
} | ||
|
||
for linted_dir in result.paths { | ||
for mut file in linted_dir.files { | ||
let path = std::mem::take(&mut file.path); | ||
let write_buff = file.fix_string(); | ||
std::fs::write(path, write_buff).unwrap(); | ||
} | ||
} | ||
|
||
linter.formatter_mut().unwrap().completion_message(); | ||
0 | ||
} | ||
} | ||
|
||
pub(crate) fn run_fix_stdin(config: FluffConfig, format: Format) -> i32 { | ||
let read_in = crate::stdin::read_std_in().unwrap(); | ||
|
||
let linter = linter(config, format); | ||
let result = linter.lint_string(&read_in, None, true); | ||
|
||
// print fixed to std out | ||
let violations = result.get_violations(Some(false)); | ||
println!("{}", result.fix_string()); | ||
|
||
// if all fixable violations are fixable, return 0 else return 1 | ||
if violations.is_empty() { | ||
0 | ||
} else { | ||
1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::commands::{Format, LintArgs}; | ||
use crate::linter; | ||
use sqruff_lib::core::config::FluffConfig; | ||
use std::path::Path; | ||
|
||
pub(crate) fn run_lint( | ||
args: LintArgs, | ||
config: FluffConfig, | ||
ignorer: impl Fn(&Path) -> bool + Send + Sync, | ||
) -> i32 { | ||
let LintArgs { paths, format } = args; | ||
let mut linter = linter(config, format); | ||
let result = linter.lint_paths(paths, false, &ignorer); | ||
let count: usize = result.paths.iter().map(|path| path.files.len()).sum(); | ||
|
||
// TODO this should be cleaned up better | ||
if matches!(format, Format::GithubAnnotationNative) { | ||
for path in result.paths { | ||
for file in path.files { | ||
for violation in file.violations { | ||
let line = format!( | ||
"::error title=sqruff,file={},line={},col={}::{}: {}", | ||
file.path, | ||
violation.line_no, | ||
violation.line_pos, | ||
violation.rule.as_ref().unwrap().code, | ||
violation.description | ||
); | ||
eprintln!("{line}"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
eprintln!("The linter processed {count} file(s)."); | ||
linter.formatter_mut().unwrap().completion_message(); | ||
if linter | ||
.formatter() | ||
.unwrap() | ||
.has_fail | ||
.load(std::sync::atomic::Ordering::SeqCst) | ||
{ | ||
1 | ||
} else { | ||
0 | ||
} | ||
} | ||
|
||
pub(crate) fn run_lint_stdin(config: FluffConfig, format: Format) -> i32 { | ||
let read_in = crate::stdin::read_std_in().unwrap(); | ||
|
||
let mut linter = linter(config, format); | ||
let result = linter.lint_string(&read_in, None, false); | ||
|
||
linter.formatter_mut().unwrap().completion_message(); | ||
|
||
if result.get_violations(None).is_empty() { | ||
0 | ||
} else { | ||
1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::io::Read; | ||
use std::path::PathBuf; | ||
|
||
/// Check if the given input is the flag to use stdin as input. | ||
/// | ||
/// If the input is a single path and that path is `-`, then the input is the flag to use stdin as | ||
/// input. Else, the input is not the flag to use stdin as input. | ||
/// | ||
/// The error message is returned if any of the inputs are `-` and there are other inputs. | ||
pub(crate) fn is_std_in_flag_input(inputs: &[PathBuf]) -> Result<bool, String> { | ||
if inputs.len() == 1 && inputs[0] == PathBuf::from("-") { | ||
Ok(true) | ||
} else if inputs.iter().any(|input| *input == PathBuf::from("-")) { | ||
Err("Cannot mix stdin flag with other inputs".to_string()) | ||
} else { | ||
Ok(false) | ||
} | ||
} | ||
|
||
/// Read the contents of stdin. | ||
pub(crate) fn read_std_in() -> Result<String, String> { | ||
let mut buffer = String::new(); | ||
std::io::stdin() | ||
.read_to_string(&mut buffer) | ||
.map_err(|e| e.to_string())?; | ||
Ok(buffer) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_is_std_in_flag_input() { | ||
let inputs = vec![PathBuf::from("-")]; | ||
assert_eq!(is_std_in_flag_input(&inputs), Ok(true)); | ||
|
||
let inputs = vec![PathBuf::from("file1"), PathBuf::from("-")]; | ||
assert_eq!( | ||
is_std_in_flag_input(&inputs), | ||
Err("Cannot mix stdin flag with other inputs".to_string()) | ||
); | ||
|
||
let inputs = vec![PathBuf::from("file1")]; | ||
assert_eq!(is_std_in_flag_input(&inputs), Ok(false)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
== [<string>] FAIL | ||
L: 1 | P: 20 | LT12 | Files must end with a single trailing newline. | ||
| [layout.end_of_file] | ||
All Finished |
Oops, something went wrong.