Example/spec: Allow folders to be specified
Some checks are pending
ci/woodpecker/push/woodpecker Pipeline is pending

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-01-11 18:09:28 +01:00
parent 80bd6c281f
commit 4fdc467cf2
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -7,22 +7,57 @@
use std::env::args;
use std::fs::File;
use std::io::{self, Read};
use std::process::exit;
use std::path::{Path, PathBuf};
use scansion_dsl::parse_spec;
fn main() -> io::Result<()> {
let args: Vec<String> = args().collect();
if args.len() != 2 {
println!("Usage: {} <spec-path>", args[0]);
exit(1);
println!("Usage: {} <file-or-dir>", args[0]);
return Err(io::Error::new(
io::ErrorKind::Other,
"Invalid argument count",
));
}
let mut file = File::open(&args[1])?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let path = Path::new(&args[1]);
if !path.exists() {
return Err(io::Error::new(
io::ErrorKind::Other,
"Specified path doesn't exist",
));
}
let files: Vec<PathBuf> = if path.is_file() {
vec![path.to_path_buf()]
} else if path.is_dir() {
path.read_dir()?
.filter_map(|entry| {
let p = entry.unwrap().path();
match p.extension() {
Some(ext) if ext == "scs" => Some(p),
_ => None,
}
})
.collect::<Vec<_>>()
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
"Only files and dirs are supported",
));
};
println!("{:?}", parse_spec(&contents));
for path in files {
let mut file = File::open(path.clone())?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
print!("Path: {path:?}: ");
match parse_spec(&contents) {
Ok(_) => println!("\x1b[32m OK\x1b[0m"),
Err(err) => println!("\x1b[31mERR\x1b[0m\n{err:?}"),
}
}
Ok(())
}