expandFileArgs expands file arguments (glob patterns, directories) into a list of files
(args []string)
| 292 | |
| 293 | // expandFileArgs expands file arguments (glob patterns, directories) into a list of files |
| 294 | func (v *Validator) expandFileArgs(args []string) ([]string, error) { |
| 295 | var files []string |
| 296 | |
| 297 | for _, arg := range args { |
| 298 | // Check if it's a directory first |
| 299 | if v.Opts.Recursive && v.isDirectory(arg) { |
| 300 | // Recursive directory processing |
| 301 | pattern := v.Opts.Pattern |
| 302 | if pattern == "" { |
| 303 | pattern = "*.sql" |
| 304 | } |
| 305 | |
| 306 | err := filepath.Walk(arg, func(path string, info os.FileInfo, err error) error { |
| 307 | if err != nil { |
| 308 | return err |
| 309 | } |
| 310 | |
| 311 | if !info.IsDir() { |
| 312 | matched, err := filepath.Match(pattern, filepath.Base(path)) |
| 313 | if err != nil { |
| 314 | return err |
| 315 | } |
| 316 | if matched { |
| 317 | files = append(files, path) |
| 318 | } |
| 319 | } |
| 320 | return nil |
| 321 | }) |
| 322 | if err != nil { |
| 323 | return nil, err |
| 324 | } |
| 325 | } else if !looksLikeSQL(arg) && (strings.Contains(arg, "*") || strings.Contains(arg, "?") || strings.Contains(arg, "[")) { |
| 326 | // Only treat as glob pattern if it doesn't look like SQL |
| 327 | // This prevents "SELECT * FROM" from being treated as a glob pattern |
| 328 | matches, err := filepath.Glob(arg) |
| 329 | if err != nil { |
| 330 | return nil, err |
| 331 | } |
| 332 | files = append(files, matches...) |
| 333 | } else { |
| 334 | // Regular file or direct SQL input |
| 335 | files = append(files, arg) |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | return files, nil |
| 340 | } |
| 341 | |
| 342 | // isDirectory checks if the given path is a directory |
| 343 | func (v *Validator) isDirectory(path string) bool { |