Use godirwalk in globpath (#5145)
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gobwas/glob"
|
||||
"github.com/karrick/godirwalk"
|
||||
)
|
||||
|
||||
type GlobPath struct {
|
||||
@@ -41,38 +42,42 @@ func Compile(path string) (*GlobPath, error) {
|
||||
}
|
||||
|
||||
// Match returns all files matching the expression
|
||||
func (g *GlobPath) Match() map[string]os.FileInfo {
|
||||
out := make(map[string]os.FileInfo)
|
||||
// If it's a static path, returns path
|
||||
func (g *GlobPath) Match() []string {
|
||||
if !g.hasMeta {
|
||||
info, err := os.Stat(g.path)
|
||||
if err == nil {
|
||||
out[g.path] = info
|
||||
}
|
||||
return out
|
||||
return []string{g.path}
|
||||
}
|
||||
if !g.HasSuperMeta {
|
||||
files, _ := filepath.Glob(g.path)
|
||||
for _, file := range files {
|
||||
info, err := os.Stat(file)
|
||||
if err == nil {
|
||||
out[file] = info
|
||||
}
|
||||
}
|
||||
return out
|
||||
return files
|
||||
}
|
||||
roots, err := filepath.Glob(g.rootGlob)
|
||||
if err != nil {
|
||||
return out
|
||||
return []string{}
|
||||
}
|
||||
walkfn := func(path string, info os.FileInfo, _ error) error {
|
||||
out := []string{}
|
||||
walkfn := func(path string, _ *godirwalk.Dirent) error {
|
||||
if g.g.Match(path) {
|
||||
out[path] = info
|
||||
out = append(out, path)
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
for _, root := range roots {
|
||||
filepath.Walk(root, walkfn)
|
||||
fileinfo, err := os.Stat(root)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if !fileinfo.IsDir() {
|
||||
if g.MatchString(root) {
|
||||
out = append(out, root)
|
||||
}
|
||||
continue
|
||||
}
|
||||
godirwalk.Walk(root, &godirwalk.Options{
|
||||
Callback: walkfn,
|
||||
Unsorted: true,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package globpath
|
||||
|
||||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -34,7 +33,7 @@ func TestCompileAndMatch(t *testing.T) {
|
||||
matches = g3.Match()
|
||||
require.Len(t, matches, 1)
|
||||
matches = g4.Match()
|
||||
require.Len(t, matches, 0)
|
||||
require.Len(t, matches, 1)
|
||||
matches = g5.Match()
|
||||
require.Len(t, matches, 0)
|
||||
}
|
||||
@@ -75,10 +74,10 @@ func getTestdataDir() string {
|
||||
func TestMatch_ErrPermission(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected map[string]os.FileInfo
|
||||
expected []string
|
||||
}{
|
||||
{"/root/foo", map[string]os.FileInfo{}},
|
||||
{"/root/f*", map[string]os.FileInfo{}},
|
||||
{"/root/foo", []string{"/root/foo"}},
|
||||
{"/root/f*", []string(nil)},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
Reference in New Issue
Block a user