Handle multiple file patterns in validate command (#3615)

Co-authored-by: Tobie Morgan Hitchcock <tobie@surrealdb.com>
This commit is contained in:
David Bottiau 2024-04-03 14:11:03 +02:00 committed by GitHub
parent 6375bd45a6
commit 32a7a9bce4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,15 +8,18 @@ use surrealdb::sql::parse;
pub struct ValidateCommandArguments { pub struct ValidateCommandArguments {
#[arg(help = "Glob pattern for the files to validate")] #[arg(help = "Glob pattern for the files to validate")]
#[arg(default_value = "**/*.surql")] #[arg(default_value = "**/*.surql")]
pattern: String, patterns: Vec<String>,
} }
pub async fn init(args: ValidateCommandArguments) -> Result<(), Error> { pub async fn init(args: ValidateCommandArguments) -> Result<(), Error> {
let ValidateCommandArguments { let ValidateCommandArguments {
pattern, patterns,
} = args; } = args;
let entries = match glob(&pattern) { let mut entries = vec![];
for pattern in patterns {
let pattern_entries = match glob(&pattern) {
Ok(entries) => entries, Ok(entries) => entries,
Err(error) => { Err(error) => {
eprintln!("Error parsing glob pattern {pattern}: {error}"); eprintln!("Error parsing glob pattern {pattern}: {error}");
@ -28,9 +31,12 @@ pub async fn init(args: ValidateCommandArguments) -> Result<(), Error> {
} }
}; };
entries.extend(pattern_entries.flatten());
}
let mut has_entries = false; let mut has_entries = false;
for entry in entries.flatten() { for entry in entries {
let file_content = tokio::fs::read_to_string(entry.clone()).await?; let file_content = tokio::fs::read_to_string(entry.clone()).await?;
let parse_result = parse(&file_content); let parse_result = parse(&file_content);
@ -50,12 +56,8 @@ pub async fn init(args: ValidateCommandArguments) -> Result<(), Error> {
} }
if !has_entries { if !has_entries {
eprintln!("No files found for pattern {pattern}"); eprintln!("No files found");
return Err(Error::Io(IoError::new(ErrorKind::NotFound, "No files found".to_string())));
return Err(Error::Io(IoError::new(
ErrorKind::NotFound,
format!("No files found for pattern {pattern}"),
)));
} }
Ok(()) Ok(())