2016-04-23 17:42:28 +00:00
|
|
|
package globpath
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gobwas/glob"
|
|
|
|
)
|
|
|
|
|
|
|
|
var sepStr = fmt.Sprintf("%v", string(os.PathSeparator))
|
|
|
|
|
|
|
|
type GlobPath struct {
|
2016-09-28 13:55:29 +00:00
|
|
|
path string
|
|
|
|
hasMeta bool
|
|
|
|
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),
|
|
|
|
hasSuperMeta: hasSuperMeta(path),
|
|
|
|
path: 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
|
2016-09-28 13:55:29 +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
|
|
|
|
}
|
|
|
|
|
2016-04-24 20:37:44 +00:00
|
|
|
func (g *GlobPath) Match() map[string]os.FileInfo {
|
2018-10-12 21:48:11 +00:00
|
|
|
out := make(map[string]os.FileInfo)
|
2016-04-23 17:42:28 +00:00
|
|
|
if !g.hasMeta {
|
2016-04-24 20:37:44 +00:00
|
|
|
info, err := os.Stat(g.path)
|
2017-07-21 21:28:14 +00:00
|
|
|
if err == nil {
|
2016-04-24 20:37:44 +00:00
|
|
|
out[g.path] = info
|
|
|
|
}
|
|
|
|
return out
|
2016-04-23 17:42:28 +00:00
|
|
|
}
|
2016-09-28 13:55:29 +00:00
|
|
|
if !g.hasSuperMeta {
|
|
|
|
files, _ := filepath.Glob(g.path)
|
|
|
|
for _, file := range files {
|
|
|
|
info, err := os.Stat(file)
|
2017-07-21 21:28:14 +00:00
|
|
|
if err == nil {
|
2016-09-28 13:55:29 +00:00
|
|
|
out[file] = info
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
2018-10-12 21:48:11 +00:00
|
|
|
roots, err := filepath.Glob(g.rootGlob)
|
|
|
|
if err != nil {
|
|
|
|
return out
|
|
|
|
}
|
2016-04-24 20:37:44 +00:00
|
|
|
walkfn := func(path string, info os.FileInfo, _ error) error {
|
2018-10-12 21:48:11 +00:00
|
|
|
if g.g.Match(path) {
|
|
|
|
out[path] = info
|
2016-04-23 17:42:28 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
2018-10-12 21:48:11 +00:00
|
|
|
for _, root := range roots {
|
|
|
|
filepath.Walk(root, walkfn)
|
2016-04-23 17:42:28 +00:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|