-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathfinder.go
53 lines (45 loc) · 1.5 KB
/
finder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package finder // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/matcher/internal/finder"
import (
"errors"
"fmt"
"slices"
"github.com/bmatcuk/doublestar/v4"
"golang.org/x/exp/maps"
)
func Validate(globs []string) error {
for _, glob := range globs {
_, err := doublestar.PathMatch(glob, "matchstring")
if err != nil {
return fmt.Errorf("parse glob: %w", err)
}
}
return nil
}
// FindFiles gets a list of paths given an array of glob patterns to include and exclude
func FindFiles(includes []string, excludes []string) ([]string, error) {
var errs error
allSet := make(map[string]struct{}, len(includes))
for _, include := range includes {
matches, err := doublestar.FilepathGlob(include, doublestar.WithFilesOnly(), doublestar.WithFailOnIOErrors())
if err != nil {
errs = errors.Join(errs, fmt.Errorf("find files with '%s' pattern: %w", include, err))
// the same pattern could cause an IO error due to one file or directory,
// but also could still find files without `doublestar.WithFailOnIOErrors()`.
matches, _ = doublestar.FilepathGlob(include, doublestar.WithFilesOnly())
}
INCLUDE:
for _, match := range matches {
for _, exclude := range excludes {
if itMatches, _ := doublestar.PathMatch(exclude, match); itMatches {
continue INCLUDE
}
}
allSet[match] = struct{}{}
}
}
keys := maps.Keys(allSet)
slices.Sort(keys)
return keys, errs
}