Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add: glob expanding maintaining backward compartibility
  • Loading branch information
Valeriy Selitskiy committed Nov 7, 2023
commit 66edc84a50de25191c7215fef7688a26d075cedf
37 changes: 36 additions & 1 deletion internal/sql/sqlpath/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
// Return a list of SQL files in the listed paths. Only includes files ending
// in .sql. Omits hidden files, directories, and migrations.
func Glob(paths []string) ([]string, error) {
paths, err := expandGlobs(paths)
if err != nil {
return nil, err
}
var files []string
for _, path := range paths {
f, err := os.Stat(path)
Expand All @@ -30,7 +34,7 @@ func Glob(paths []string) ([]string, error) {
files = append(files, path)
}
}
var sqlFiles []string
var sqlFiles []string //nolint:prealloc // can be empty
for _, file := range files {
if !strings.HasSuffix(file, ".sql") {
continue
Expand All @@ -45,3 +49,34 @@ func Glob(paths []string) ([]string, error) {
}
return sqlFiles, nil
}

func expandGlobs(paths []string) ([]string, error) {
expandedPatterns := make([]string, 0, len(paths))
for _, pattern := range paths {
expansion, err := filepath.Glob(pattern)
if err != nil {
return nil, fmt.Errorf("failed to expand pattern %q: %w", pattern, err)
}
if len(expansion) == 0 {
fi, err := os.Lstat(pattern)
if err != nil {
return nil, fmt.Errorf("failed to stat path %q: %w", pattern, err)
}
if fi == nil {
return nil, fmt.Errorf("failed to stat path %q: %w", pattern, os.ErrNotExist)
}
var isFilepath bool
for _, mask := range []os.FileMode{os.ModeDir, os.ModeSymlink, os.FileMode(0x400)} {
if fi.Mode()&mask == 0 {
isFilepath = true
break
}
}
if !isFilepath {
continue
}
}
expandedPatterns = append(expandedPatterns, expansion...)
}
return expandedPatterns, nil
}
Empty file.