-
Notifications
You must be signed in to change notification settings - Fork 69
/
glob_file_filterer.go
52 lines (40 loc) · 1.28 KB
/
glob_file_filterer.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
package discovery
import (
"context"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/newrelic/newrelic-cli/internal/install/types"
)
// GlobFileFilterer is an implementation of the FileFilterer interface that uses
// glob-based filesystem searches to locate the existence of files.
type GlobFileFilterer struct{}
// NewGlobFileFilterer returns a new instance of GlobFileFilterer.
func NewGlobFileFilterer() *GlobFileFilterer {
f := GlobFileFilterer{}
return &f
}
// Filter uses the patterns provided in the passed recipe to return matches based
// on which files exist in the underlying file system.
func (f *GlobFileFilterer) Filter(ctx context.Context, recipes []types.OpenInstallationRecipe) ([]types.OpenInstallationLogMatch, error) {
fileMatches := []types.OpenInstallationLogMatch{}
for _, r := range recipes {
for _, l := range r.LogMatch {
match, _ := matchLogFilesFromRecipe(l)
if match {
fileMatches = append(fileMatches, l)
}
}
}
return fileMatches, nil
}
func matchLogFilesFromRecipe(matcher types.OpenInstallationLogMatch) (bool, []string) {
matches, err := filepath.Glob(matcher.File)
if err != nil {
log.Errorf("error matching logfiles: %s", err)
return false, nil
}
if len(matches) > 0 {
return true, matches
}
return false, nil
}