Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flag to allow debugging parse failures #3

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ pub struct Log<Format> {
}

impl<Format> Log<Format> {
pub fn parse(yaml: &str) -> Option<Self> {
pub fn parse(yaml: &str, include_failures: bool) -> Option<Self> {
let cleaned = strip_ansi_escapes::strip_str(yaml);

match serde_yaml::from_str(&cleaned) {
Ok(log) => Some(log),
Err(e) => {
eprintln!("Failed to parse YAML: {}", e);
eprintln!("YAML content:\n{}", cleaned);
if include_failures {
eprintln!("Failed to parse YAML: {}", e);
eprintln!("YAML content:\n{}", cleaned);
}
None
}
}
Expand Down Expand Up @@ -113,7 +115,7 @@ mod tests {

let expected_output = format!($expected_output);
assert_eq!(
Log::<$format>::parse(yaml_data).unwrap().to_string(),
Log::<$format>::parse(yaml_data, true).unwrap().to_string(),
expected_output
);
}
Expand Down
27 changes: 23 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ struct Args {

#[clap(subcommand)]
command: Command,

/// whether to include logs that fail to parse
#[clap(short, long, global = true)]
include_failures: bool,
}

#[derive(Parser, Debug)]
Expand All @@ -64,7 +68,7 @@ enum Command {
}

impl Command {
async fn run<F>(self, project: &str, filter: &str) -> anyhow::Result<()>
async fn run<F>(self, project: &str, filter: &str, include_failures: bool) -> anyhow::Result<()>
where
Log<F>: std::fmt::Display + 'static,
{
Expand All @@ -90,6 +94,7 @@ impl Command {
.take()
.ok_or(anyhow!("Failed to capture stdout"))?,
),
include_failures,
)
.await?
}
Expand All @@ -107,6 +112,7 @@ impl Command {
.take()
.ok_or(anyhow!("Failed to capture stdout"))?,
),
include_failures,
));

let mut new_filters;
Expand All @@ -131,6 +137,7 @@ async fn main() {
std::env::set_var("CLOUDSDK_PYTHON_SITEPACKAGES", "1");
let Args {
filter,
include_failures,
pretty,
project,
command,
Expand All @@ -155,10 +162,22 @@ async fn main() {

if let Err(e) = match pretty {
true => match atty::is(Stream::Stdout) {
true => command.run::<Decorated>(&project, &filter).await,
false => command.run::<Pretty>(&project, &filter).await,
true => {
command
.run::<Decorated>(&project, &filter, include_failures)
.await
}
false => {
command
.run::<Pretty>(&project, &filter, include_failures)
.await
}
},
false => command.run::<Raw>(&project, &filter).await,
false => {
command
.run::<Raw>(&project, &filter, include_failures)
.await
}
} {
eprintln!("Error: {}", e);
exit(1);
Expand Down
8 changes: 5 additions & 3 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::log::Log;
pub async fn transpose<F>(
mut writer: impl std::io::Write,
reader: impl AsyncBufRead + Unpin,
include_failures: bool,
) -> std::io::Result<()>
where
Log<F>: std::fmt::Display,
Expand All @@ -18,13 +19,14 @@ where
if line == "---" {
// Parse the collected YAML block if it's non-empty
if !yaml_buffer.trim().is_empty() {
match Log::<F>::parse(&yaml_buffer) {
match Log::<F>::parse(&yaml_buffer, include_failures) {
Some(entry) => {
write!(writer, "{entry}")?;
}
None => {
None if include_failures => {
eprintln!("Failed to parse log entry:\n{}", yaml_buffer);
}
None => {}
}
yaml_buffer.clear();
}
Expand Down Expand Up @@ -64,7 +66,7 @@ mod tests {
let reader = BufReader::new(Cursor::new(log_data));
let mut result = Vec::new();

transpose::<Raw>(&mut result, reader).await.unwrap();
transpose::<Raw>(&mut result, reader, true).await.unwrap();
let result = String::from_utf8(result).unwrap();
assert_eq!(result, "Test message");
}
Expand Down
Loading