1
0

read.go 1002 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package sqlpath
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/kyleconroy/sqlc/internal/migrations"
  8. )
  9. // Return a list of SQL files in the listed paths. Only includes files ending
  10. // in .sql. Omits hidden files, directories, and migrations.
  11. func Glob(paths []string) ([]string, error) {
  12. var files []string
  13. for _, path := range paths {
  14. f, err := os.Stat(path)
  15. if err != nil {
  16. return nil, fmt.Errorf("path %s does not exist", path)
  17. }
  18. if f.IsDir() {
  19. listing, err := os.ReadDir(path)
  20. if err != nil {
  21. return nil, err
  22. }
  23. for _, f := range listing {
  24. files = append(files, filepath.Join(path, f.Name()))
  25. }
  26. } else {
  27. files = append(files, path)
  28. }
  29. }
  30. var sqlFiles []string
  31. for _, file := range files {
  32. if !strings.HasSuffix(file, ".sql") {
  33. continue
  34. }
  35. if strings.HasPrefix(filepath.Base(file), ".") {
  36. continue
  37. }
  38. if migrations.IsDown(filepath.Base(file)) {
  39. continue
  40. }
  41. sqlFiles = append(sqlFiles, file)
  42. }
  43. return sqlFiles, nil
  44. }