From 32a7a9bce44769f049d5305724a78d85caa0b633 Mon Sep 17 00:00:00 2001 From: David Bottiau Date: Wed, 3 Apr 2024 14:11:03 +0200 Subject: [PATCH] Handle multiple file patterns in validate command (#3615) Co-authored-by: Tobie Morgan Hitchcock --- src/cli/validate.rs | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/cli/validate.rs b/src/cli/validate.rs index 70c398ea..3bdbc21d 100644 --- a/src/cli/validate.rs +++ b/src/cli/validate.rs @@ -8,29 +8,35 @@ use surrealdb::sql::parse; pub struct ValidateCommandArguments { #[arg(help = "Glob pattern for the files to validate")] #[arg(default_value = "**/*.surql")] - pattern: String, + patterns: Vec, } pub async fn init(args: ValidateCommandArguments) -> Result<(), Error> { let ValidateCommandArguments { - pattern, + patterns, } = args; - let entries = match glob(&pattern) { - Ok(entries) => entries, - Err(error) => { - eprintln!("Error parsing glob pattern {pattern}: {error}"); + let mut entries = vec![]; - return Err(Error::Io(IoError::new( - ErrorKind::Other, - format!("Error parsing glob pattern {pattern}: {error}"), - ))); - } - }; + 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()); + } 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 parse_result = parse(&file_content); @@ -50,12 +56,8 @@ pub async fn init(args: ValidateCommandArguments) -> Result<(), Error> { } if !has_entries { - eprintln!("No files found for pattern {pattern}"); - - return Err(Error::Io(IoError::new( - ErrorKind::NotFound, - format!("No files found for pattern {pattern}"), - ))); + eprintln!("No files found"); + return Err(Error::Io(IoError::new(ErrorKind::NotFound, "No files found".to_string()))); } Ok(())