2016-04-23 17:42:28 +00:00
|
|
|
package globpath
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gobwas/glob"
|
2018-12-18 22:23:25 +00:00
|
|
|
"github.com/karrick/godirwalk"
|
2016-04-23 17:42:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GlobPath struct {
|
2016-09-28 13:55:29 +00:00
|
|
|
path string
|
|
|
|
hasMeta bool
|
2018-12-13 20:25:49 +00:00
|
|
|
HasSuperMeta bool
|
2018-10-12 21:48:11 +00:00
|
|
|
rootGlob string
|
2016-09-28 13:55:29 +00:00
|
|
|
g glob.Glob
|
2016-04-23 17:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Compile(path string) (*GlobPath, error) {
|
|
|
|
out := GlobPath{
|
2016-09-28 13:55:29 +00:00
|
|
|
hasMeta: hasMeta(path),
|
2018-12-13 20:25:49 +00:00
|
|
|
HasSuperMeta: hasSuperMeta(path),
|
2019-07-08 21:46:00 +00:00
|
|
|
path: filepath.FromSlash(path),
|
2016-04-23 17:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if there are no glob meta characters in the path, don't bother compiling
|
2018-10-12 21:48:11 +00:00
|
|
|
// a glob object
|
2018-12-13 20:25:49 +00:00
|
|
|
if !out.hasMeta || !out.HasSuperMeta {
|
2016-04-23 17:42:28 +00:00
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2018-10-12 21:48:11 +00:00
|
|
|
// find the root elements of the object path, the entry point for recursion
|
|
|
|
// when you have a super-meta in your path (which are :
|
|
|
|
// glob(/your/expression/until/first/star/of/super-meta))
|
|
|
|
out.rootGlob = path[:strings.Index(path, "**")+1]
|
2016-04-23 17:42:28 +00:00
|
|
|
var err error
|
|
|
|
if out.g, err = glob.Compile(path, os.PathSeparator); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2019-07-08 21:46:00 +00:00
|
|
|
// Match returns all files matching the expression.
|
|
|
|
// If it's a static path, returns path.
|
|
|
|
// All returned path will have the host platform separator.
|
2018-12-18 22:23:25 +00:00
|
|
|
func (g *GlobPath) Match() []string {
|
2016-04-23 17:42:28 +00:00
|
|
|
if !g.hasMeta {
|
2018-12-18 22:23:25 +00:00
|
|
|
return []string{g.path}
|
2016-04-23 17:42:28 +00:00
|
|
|
}
|
2018-12-13 20:25:49 +00:00
|
|
|
if !g.HasSuperMeta {
|
2016-09-28 13:55:29 +00:00
|
|
|
files, _ := filepath.Glob(g.path)
|
2018-12-18 22:23:25 +00:00
|
|
|
return files
|
2016-09-28 13:55:29 +00:00
|
|
|
}
|
2018-10-12 21:48:11 +00:00
|
|
|
roots, err := filepath.Glob(g.rootGlob)
|
|
|
|
if err != nil {
|
2018-12-18 22:23:25 +00:00
|
|
|
return []string{}
|
2018-10-12 21:48:11 +00:00
|
|
|
}
|
2018-12-18 22:23:25 +00:00
|
|
|
out := []string{}
|
|
|
|
walkfn := func(path string, _ *godirwalk.Dirent) error {
|
2018-10-12 21:48:11 +00:00
|
|
|
if g.g.Match(path) {
|
2018-12-18 22:23:25 +00:00
|
|
|
out = append(out, path)
|
2016-04-23 17:42:28 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
2018-10-12 21:48:11 +00:00
|
|
|
for _, root := range roots {
|
2018-12-18 22:23:25 +00:00
|
|
|
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,
|
|
|
|
})
|
2016-04-23 17:42:28 +00:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-07-08 21:46:00 +00:00
|
|
|
// MatchString tests the path string against the glob. The path should contain
|
|
|
|
// the host platform separator.
|
2018-12-13 20:25:49 +00:00
|
|
|
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.
|
2019-07-08 21:46:00 +00:00
|
|
|
// All returned path will have the host platform separator.
|
2018-12-13 20:25:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-04-23 17:42:28 +00:00
|
|
|
// hasMeta reports whether path contains any magic glob characters.
|
|
|
|
func hasMeta(path string) bool {
|
|
|
|
return strings.IndexAny(path, "*?[") >= 0
|
|
|
|
}
|
2016-09-28 13:55:29 +00:00
|
|
|
|
|
|
|
// hasSuperMeta reports whether path contains any super magic glob characters (**).
|
|
|
|
func hasSuperMeta(path string) bool {
|
|
|
|
return strings.Index(path, "**") >= 0
|
|
|
|
}
|