Skip to content

Commit

Permalink
Show some better errors in the CLI, highlighting the failed line
Browse files Browse the repository at this point in the history
Fixes #9
  • Loading branch information
rtyler committed Dec 20, 2020
1 parent e5bb3f5 commit f176c4d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jdp"
version = "0.1.1"
version = "0.2.0"
authors = ["R. Tyler Croy <rtyler@brokenco.de>"]
edition = "2018"
description = "A Rust-native parser for Jenkins Declarative Pipeline"
Expand Down
54 changes: 50 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,58 @@ fn main() {

match opts.command.unwrap() {
Command::Check(checkopts) => {
println!("Checking: {}", checkopts.file.as_path().display());
if let Err(error) = parse_file(&checkopts.file) {
use pest::error::ErrorVariant;
use pest::error::LineColLocation::{Pos, Span};
use std::fs::File;
use std::io::{BufRead, BufReader};

let result = parse_file(&checkopts.file);
let file = File::open(&checkopts.file).expect("Failed to read the file again?");
let lines: Vec<String> = BufReader::new(file).lines().map(|l| l.unwrap()).collect();

if result.is_err() {
println!("Failed to parse!: {:#?}", result);
let filename = checkopts.file.as_path().to_string_lossy();
println!("\n{}", filename);
for _ in 0..filename.len() {
print!("-");
}
println!("");

match error.line_col {
Pos((line, column)) => {
let start_line = std::cmp::max(0, line - 4);

for n in start_line..line {
println!("{}: {}", n, lines[n]);
}
// Just a little spacer for the error
print!(" ");
for _ in 0..column {
print!("-");
}
println!("^");
}
Span(start, end) => {
let start_line = std::cmp::max(0, start.0 - 4);
for n in start_line..start.0 {
println!("{}: {}", n, lines[n]);
}
// Just a little spacer for the error
print!(" ");
for _ in 0..(end.1 - 2) {
print!("-");
}
println!("^");
}
}

match error.variant {
ErrorVariant::CustomError { message } => {
println!("\nFail: {}", message);
}
_ => {
println!("\nFailed to parse: missing required syntax");
}
}
std::process::exit(1);
} else {
println!("Looks valid! Great work!");
Expand Down

0 comments on commit f176c4d

Please sign in to comment.