MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / expandFileArgs

Method expandFileArgs

cmd/gosqlx/cmd/validator.go:294–340  ·  view source on GitHub ↗

expandFileArgs expands file arguments (glob patterns, directories) into a list of files

(args []string)

Source from the content-addressed store, hash-verified

292
293// expandFileArgs expands file arguments (glob patterns, directories) into a list of files
294func (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
343func (v *Validator) isDirectory(path string) bool {

Callers 2

ValidateMethod · 0.95

Calls 2

isDirectoryMethod · 0.95
looksLikeSQLFunction · 0.85

Tested by 1