Return a list of SQL files in the listed paths. Only includes files ending in .sql. Omits hidden files, directories, and down migrations. If a path contains *, ?, [, or ], treat the path as a pattern and expand it filepath.Glob.
(patterns []string)
| 17 | // If a path contains *, ?, [, or ], treat the path as a pattern and expand it |
| 18 | // filepath.Glob. |
| 19 | func Glob(patterns []string) ([]string, error) { |
| 20 | var files, paths []string |
| 21 | for _, pattern := range patterns { |
| 22 | if strings.ContainsAny(pattern, "*?[]") { |
| 23 | matches, err := filepath.Glob(pattern) |
| 24 | if err != nil { |
| 25 | return nil, err |
| 26 | } |
| 27 | // if len(matches) == 0 { |
| 28 | // slog.Warn("zero files matched", "pattern", pattern) |
| 29 | // } |
| 30 | paths = append(paths, matches...) |
| 31 | } else { |
| 32 | paths = append(paths, pattern) |
| 33 | } |
| 34 | } |
| 35 | for _, path := range paths { |
| 36 | f, err := os.Stat(path) |
| 37 | if err != nil { |
| 38 | return nil, fmt.Errorf("path error: %w", err) |
| 39 | } |
| 40 | if f.IsDir() { |
| 41 | listing, err := os.ReadDir(path) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | for _, f := range listing { |
| 46 | files = append(files, filepath.Join(path, f.Name())) |
| 47 | } |
| 48 | } else { |
| 49 | files = append(files, filepath.Clean(path)) |
| 50 | } |
| 51 | } |
| 52 | var sqlFiles []string |
| 53 | for _, file := range files { |
| 54 | if !strings.HasSuffix(file, ".sql") { |
| 55 | continue |
| 56 | } |
| 57 | if strings.HasPrefix(filepath.Base(file), ".") { |
| 58 | continue |
| 59 | } |
| 60 | if migrations.IsDown(filepath.Base(file)) { |
| 61 | continue |
| 62 | } |
| 63 | sqlFiles = append(sqlFiles, file) |
| 64 | } |
| 65 | return sqlFiles, nil |
| 66 | } |