Use output-stdout pattern for linter command (#2794)

This commit is contained in:
Charlie Marsh 2023-02-11 22:09:03 -05:00 committed by GitHub
parent 77e65c9ff5
commit 8289ede00f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 35 deletions

View file

@ -1,11 +1,34 @@
use std::io;
use std::io::BufWriter;
use std::io::Write;
use anyhow::Result;
use itertools::Itertools;
use ruff::registry::{Linter, RuleNamespace, UpstreamCategory};
use serde::Serialize;
use strum::IntoEnumIterator;
use ruff::registry::{Linter, RuleNamespace, UpstreamCategory};
use crate::args::HelpFormat;
pub fn linter(format: HelpFormat) {
#[derive(Serialize)]
struct LinterInfo {
prefix: &'static str,
name: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
categories: Option<Vec<LinterCategoryInfo>>,
}
#[derive(Serialize)]
struct LinterCategoryInfo {
prefix: &'static str,
name: &'static str,
}
pub fn linter(format: HelpFormat) -> Result<()> {
let mut stdout = BufWriter::new(io::stdout().lock());
let mut output = String::new();
match format {
HelpFormat::Text => {
for linter in Linter::iter() {
@ -18,11 +41,7 @@ pub fn linter(format: HelpFormat) {
.join("/"),
prefix => prefix.to_string(),
};
#[allow(clippy::print_stdout)]
{
println!("{:>4} {}", prefix, linter.name());
}
output.push_str(&format!("{:>4} {}\n", prefix, linter.name()));
}
}
@ -41,19 +60,14 @@ pub fn linter(format: HelpFormat) {
}),
})
.collect();
#[allow(clippy::print_stdout)]
{
println!("{}", serde_json::to_string_pretty(&linters).unwrap());
}
output.push_str(&serde_json::to_string_pretty(&linters)?);
output.push('\n');
}
HelpFormat::Markdown => {
#[allow(clippy::print_stdout)]
{
println!("| {:>6} | {:<27} |", "Prefix", "Name");
println!("| {:>6} | {:<27} |", "------", "-".repeat(27));
}
output.push_str(&format!("| {:>6} | {:<27} |\n", "Prefix", "Name"));
output.push_str(&format!("| {:>6} | {:<27} |\n", "------", "-".repeat(27)));
for linter in Linter::iter() {
let prefix = match linter.common_prefix() {
"" => linter
@ -64,26 +78,12 @@ pub fn linter(format: HelpFormat) {
.join("/"),
prefix => prefix.to_string(),
};
#[allow(clippy::print_stdout)]
{
println!("| {:>6} | {:<27} |", prefix, linter.name());
}
output.push_str(&format!("| {:>6} | {:<27} |\n", prefix, linter.name()));
}
}
}
}
#[derive(Serialize)]
struct LinterInfo {
prefix: &'static str,
name: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
categories: Option<Vec<LinterCategoryInfo>>,
}
write!(stdout, "{output}")?;
#[derive(Serialize)]
struct LinterCategoryInfo {
prefix: &'static str,
name: &'static str,
Ok(())
}