globpath refactor into pkg separate from filestat
This commit is contained in:
@@ -5,17 +5,12 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gobwas/glob"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/internal/globpath"
|
||||
"github.com/influxdata/telegraf/plugins/inputs"
|
||||
)
|
||||
|
||||
var sepStr = fmt.Sprintf("%v", string(os.PathSeparator))
|
||||
|
||||
const sampleConfig = `
|
||||
## Files to gather stats about.
|
||||
## These accept standard unix glob matching rules, but with the addition of
|
||||
@@ -29,16 +24,13 @@ type FileStat struct {
|
||||
Md5 bool
|
||||
Files []string
|
||||
|
||||
// maps full file paths to glob obj
|
||||
globs map[string]glob.Glob
|
||||
// maps full file paths to their root dir
|
||||
roots map[string]string
|
||||
// maps full file paths to globmatch obj
|
||||
globs map[string]*globpath.GlobPath
|
||||
}
|
||||
|
||||
func NewFileStat() *FileStat {
|
||||
return &FileStat{
|
||||
globs: make(map[string]glob.Glob),
|
||||
roots: make(map[string]string),
|
||||
globs: make(map[string]*globpath.GlobPath),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,27 +48,14 @@ func (f *FileStat) Gather(acc telegraf.Accumulator) error {
|
||||
// Get the compiled glob object for this filepath
|
||||
g, ok := f.globs[filepath]
|
||||
if !ok {
|
||||
if g, err = glob.Compile(filepath, os.PathSeparator); err != nil {
|
||||
if g, err = globpath.Compile(filepath); err != nil {
|
||||
errS += err.Error() + " "
|
||||
continue
|
||||
}
|
||||
f.globs[filepath] = g
|
||||
}
|
||||
// Get the root directory for this filepath
|
||||
root, ok := f.roots[filepath]
|
||||
if !ok {
|
||||
root = findRootDir(filepath)
|
||||
f.roots[filepath] = root
|
||||
}
|
||||
|
||||
var matches []string
|
||||
// Do not walk file tree if we don't have to.
|
||||
if !hasMeta(filepath) {
|
||||
matches = []string{filepath}
|
||||
} else {
|
||||
matches = walkFilePath(f.roots[filepath], f.globs[filepath])
|
||||
}
|
||||
for _, file := range matches {
|
||||
for _, file := range g.Match() {
|
||||
tags := map[string]string{
|
||||
"file": file,
|
||||
}
|
||||
@@ -118,20 +97,6 @@ func (f *FileStat) Gather(acc telegraf.Accumulator) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// walk the filepath from the given root and return a list of files that match
|
||||
// the given glob.
|
||||
func walkFilePath(root string, g glob.Glob) []string {
|
||||
matchedFiles := []string{}
|
||||
walkfn := func(path string, _ os.FileInfo, _ error) error {
|
||||
if g.Match(path) {
|
||||
matchedFiles = append(matchedFiles, path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
filepath.Walk(root, walkfn)
|
||||
return matchedFiles
|
||||
}
|
||||
|
||||
// Read given file and calculate an md5 hash.
|
||||
func getMd5(file string) (string, error) {
|
||||
of, err := os.Open(file)
|
||||
@@ -149,35 +114,6 @@ func getMd5(file string) (string, error) {
|
||||
return fmt.Sprintf("%x", hash.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// find the root dir of the given path (could include globs).
|
||||
// ie:
|
||||
// /var/log/telegraf.conf -> /var/log/
|
||||
// /home/** -> /home/
|
||||
// /home/*/** -> /home/
|
||||
// /lib/share/*/*/**.txt -> /lib/share/
|
||||
func findRootDir(path string) string {
|
||||
pathItems := strings.Split(path, sepStr)
|
||||
outpath := sepStr
|
||||
for i, item := range pathItems {
|
||||
if i == len(pathItems)-1 {
|
||||
break
|
||||
}
|
||||
if item == "" {
|
||||
continue
|
||||
}
|
||||
if hasMeta(item) {
|
||||
break
|
||||
}
|
||||
outpath += item + sepStr
|
||||
}
|
||||
return outpath
|
||||
}
|
||||
|
||||
// hasMeta reports whether path contains any magic glob characters.
|
||||
func hasMeta(path string) bool {
|
||||
return strings.IndexAny(path, "*?[") >= 0
|
||||
}
|
||||
|
||||
func init() {
|
||||
inputs.Add("filestat", func() telegraf.Input {
|
||||
return NewFileStat()
|
||||
|
||||
@@ -164,23 +164,6 @@ func TestGatherSuperAsterisk(t *testing.T) {
|
||||
acc.AssertContainsTaggedFields(t, "filestat", fields3, tags3)
|
||||
}
|
||||
|
||||
func TestFindRootDir(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
output string
|
||||
}{
|
||||
{"/var/log/telegraf.conf", "/var/log/"},
|
||||
{"/home/**", "/home/"},
|
||||
{"/home/*/**", "/home/"},
|
||||
{"/lib/share/*/*/**.txt", "/lib/share/"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
actual := findRootDir(test.input)
|
||||
assert.Equal(t, test.output, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetMd5(t *testing.T) {
|
||||
dir := getTestdataDir()
|
||||
md5, err := getMd5(dir + "test.conf")
|
||||
|
||||
Reference in New Issue
Block a user