2023-07-10 08:36:35 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use clap::Args;
|
|
|
|
use glob::glob;
|
|
|
|
use std::io::{Error as IoError, ErrorKind};
|
|
|
|
use surrealdb::sql::parse;
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
|
|
|
pub struct ValidateCommandArguments {
|
|
|
|
#[arg(help = "Glob pattern for the files to validate")]
|
|
|
|
#[arg(default_value = "**/*.surql")]
|
2024-04-03 12:11:03 +00:00
|
|
|
patterns: Vec<String>,
|
2023-07-10 08:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn init(args: ValidateCommandArguments) -> Result<(), Error> {
|
|
|
|
let ValidateCommandArguments {
|
2024-04-03 12:11:03 +00:00
|
|
|
patterns,
|
2023-07-10 08:36:35 +00:00
|
|
|
} = args;
|
|
|
|
|
2024-04-03 12:11:03 +00:00
|
|
|
let mut entries = vec![];
|
2023-07-10 08:36:35 +00:00
|
|
|
|
2024-04-03 12:11:03 +00:00
|
|
|
for pattern in patterns {
|
|
|
|
let pattern_entries = match glob(&pattern) {
|
|
|
|
Ok(entries) => entries,
|
|
|
|
Err(error) => {
|
|
|
|
eprintln!("Error parsing glob pattern {pattern}: {error}");
|
|
|
|
|
|
|
|
return Err(Error::Io(IoError::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
format!("Error parsing glob pattern {pattern}: {error}"),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
entries.extend(pattern_entries.flatten());
|
|
|
|
}
|
2023-07-10 08:36:35 +00:00
|
|
|
|
|
|
|
let mut has_entries = false;
|
|
|
|
|
2024-04-03 12:11:03 +00:00
|
|
|
for entry in entries {
|
2023-07-10 08:36:35 +00:00
|
|
|
let file_content = tokio::fs::read_to_string(entry.clone()).await?;
|
|
|
|
let parse_result = parse(&file_content);
|
|
|
|
|
|
|
|
match parse_result {
|
|
|
|
Ok(_) => {
|
|
|
|
println!("{}: OK", entry.display());
|
|
|
|
}
|
|
|
|
Err(error) => {
|
|
|
|
println!("{}: KO", entry.display());
|
|
|
|
eprintln!("{error}");
|
|
|
|
|
|
|
|
return Err(crate::err::Error::from(error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
has_entries = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !has_entries {
|
2024-04-03 12:11:03 +00:00
|
|
|
eprintln!("No files found");
|
|
|
|
return Err(Error::Io(IoError::new(ErrorKind::NotFound, "No files found".to_string())));
|
2023-07-10 08:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|