Add size to filecount input (#4778)

This commit is contained in:
Samuel-BF
2018-12-13 21:25:49 +01:00
committed by Daniel Nelson
parent 0485119716
commit 8906e2796b
7 changed files with 219 additions and 131 deletions

View File

@@ -1,7 +1,6 @@
package globpath
import (
"fmt"
"os"
"path/filepath"
"strings"
@@ -9,12 +8,10 @@ import (
"github.com/gobwas/glob"
)
var sepStr = fmt.Sprintf("%v", string(os.PathSeparator))
type GlobPath struct {
path string
hasMeta bool
hasSuperMeta bool
HasSuperMeta bool
rootGlob string
g glob.Glob
}
@@ -22,13 +19,13 @@ type GlobPath struct {
func Compile(path string) (*GlobPath, error) {
out := GlobPath{
hasMeta: hasMeta(path),
hasSuperMeta: hasSuperMeta(path),
HasSuperMeta: hasSuperMeta(path),
path: path,
}
// if there are no glob meta characters in the path, don't bother compiling
// a glob object
if !out.hasMeta || !out.hasSuperMeta {
if !out.hasMeta || !out.HasSuperMeta {
return &out, nil
}
@@ -43,6 +40,7 @@ func Compile(path string) (*GlobPath, error) {
return &out, nil
}
// Match returns all files matching the expression
func (g *GlobPath) Match() map[string]os.FileInfo {
out := make(map[string]os.FileInfo)
if !g.hasMeta {
@@ -52,7 +50,7 @@ func (g *GlobPath) Match() map[string]os.FileInfo {
}
return out
}
if !g.hasSuperMeta {
if !g.HasSuperMeta {
files, _ := filepath.Glob(g.path)
for _, file := range files {
info, err := os.Stat(file)
@@ -79,6 +77,32 @@ func (g *GlobPath) Match() map[string]os.FileInfo {
return out
}
// MatchString test a string against the glob
func (g *GlobPath) MatchString(path string) bool {
if !g.HasSuperMeta {
res, _ := filepath.Match(g.path, path)
return res
}
return g.g.Match(path)
}
// GetRoots returns a list of files and directories which should be optimal
// prefixes of matching files when you have a super-meta in your expression :
// - any directory under these roots may contain a matching file
// - no file outside of these roots can match the pattern
// Note that it returns both files and directories.
func (g *GlobPath) GetRoots() []string {
if !g.hasMeta {
return []string{g.path}
}
if !g.HasSuperMeta {
matches, _ := filepath.Glob(g.path)
return matches
}
roots, _ := filepath.Glob(g.rootGlob)
return roots
}
// hasMeta reports whether path contains any magic glob characters.
func hasMeta(path string) bool {
return strings.IndexAny(path, "*?[") >= 0