1
0

read.go 1019 B

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