2021-03-29 15:48:16 +00:00
|
|
|
package enumerator
|
|
|
|
|
|
|
|
import (
|
2021-05-27 12:15:43 +00:00
|
|
|
"io/fs"
|
2021-03-29 15:48:16 +00:00
|
|
|
"os"
|
2021-05-06 08:58:45 +00:00
|
|
|
"path"
|
2021-03-29 15:48:16 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2021-05-06 08:58:45 +00:00
|
|
|
"github.com/bmatcuk/doublestar/v4"
|
2021-03-29 15:48:16 +00:00
|
|
|
)
|
|
|
|
|
2021-05-07 09:43:10 +00:00
|
|
|
func GlobS3(path string) (prefix string, pattern string) {
|
2021-03-29 15:48:16 +00:00
|
|
|
prefix, pattern = splitDirPattern(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func HasMeta(path string) bool {
|
|
|
|
magicChars := `?*[]`
|
|
|
|
return strings.ContainsAny(path, magicChars)
|
|
|
|
}
|
|
|
|
|
2021-06-24 09:01:21 +00:00
|
|
|
// Should split a path :
|
|
|
|
// - prefix : path part that should not contains glob patterns, that is used in S3 query to filter result
|
|
|
|
// - pattern : should contains the glob pattern to be used by doublestar matching library
|
|
|
|
func splitDirPattern(p string) (prefix string, pattern string) {
|
2021-06-15 15:58:15 +00:00
|
|
|
sep := "/"
|
2021-03-29 15:48:16 +00:00
|
|
|
|
2021-06-24 09:01:21 +00:00
|
|
|
splitPath := strings.Split(p, sep)
|
|
|
|
prefixEnded := false
|
|
|
|
for _, s := range splitPath {
|
|
|
|
if HasMeta(s) || prefixEnded {
|
|
|
|
prefixEnded = true
|
|
|
|
pattern = strings.Join([]string{pattern, s}, sep)
|
|
|
|
continue
|
2021-03-29 15:48:16 +00:00
|
|
|
}
|
2021-06-24 09:01:21 +00:00
|
|
|
|
|
|
|
prefix = strings.Join([]string{prefix, s}, sep)
|
|
|
|
|
2021-03-29 15:48:16 +00:00
|
|
|
}
|
2021-06-24 09:01:21 +00:00
|
|
|
return strings.Trim(prefix, sep), strings.Trim(pattern, sep)
|
2021-03-29 15:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Glob(pattern string) ([]string, error) {
|
|
|
|
if !strings.Contains(pattern, "**") {
|
|
|
|
return filepath.Glob(pattern)
|
|
|
|
}
|
|
|
|
|
2021-05-27 12:15:43 +00:00
|
|
|
var files []string
|
|
|
|
|
|
|
|
err := doublestar.GlobWalk(os.DirFS("."), path.Clean(pattern), func(path string, d fs.DirEntry) error {
|
|
|
|
// Ensure paths aren't actually directories
|
|
|
|
// For example when the directory matches the glob pattern like it's a file
|
|
|
|
if !d.IsDir() {
|
|
|
|
files = append(files, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-05-06 08:58:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-03-29 15:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
}
|