preset glob non-recursive issue; supporting more syntax
This commit is contained in:
+55
-9
@@ -3,30 +3,76 @@ package utils
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ExpandGlob(patterns []string) ([]string, error) {
|
||||
var result []string
|
||||
seen := make(map[string]struct{})
|
||||
|
||||
for _, p := range patterns {
|
||||
matches, err := filepath.Glob(p)
|
||||
for _, pattern := range patterns {
|
||||
|
||||
if pattern == "*" {
|
||||
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
add(path, seen, &result)
|
||||
return nil
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
isRecursive := strings.Contains(pattern, "**")
|
||||
|
||||
if isRecursive {
|
||||
root := strings.Split(pattern, "**")[0]
|
||||
if root == "" {
|
||||
root = "."
|
||||
}
|
||||
|
||||
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil || info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
matched, _ := filepath.Match(
|
||||
strings.Replace(pattern, "**", "*", 1),
|
||||
path,
|
||||
)
|
||||
|
||||
if matched {
|
||||
add(path, seen, &result)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
matches, err := filepath.Glob(pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, m := range matches {
|
||||
info, err := os.Stat(m)
|
||||
if err != nil {
|
||||
if err != nil || info.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
result = append(result, m)
|
||||
add(m, seen, &result)
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func add(path string, seen map[string]struct{}, result *[]string) {
|
||||
if _, ok := seen[path]; ok {
|
||||
return
|
||||
}
|
||||
seen[path] = struct{}{}
|
||||
*result = append(*result, path)
|
||||
}
|
||||
Reference in New Issue
Block a user