2023-03-10 18:27:30 +00:00
|
|
|
package fingerprint
|
2017-09-16 11:44:13 -03:00
|
|
|
|
|
|
|
import (
|
2019-08-21 13:33:12 +09:00
|
|
|
"os"
|
2017-09-16 11:44:13 -03:00
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/mattn/go-zglob"
|
2021-01-07 11:48:33 -03:00
|
|
|
|
|
|
|
"github.com/go-task/task/v3/internal/execext"
|
2022-08-06 18:19:07 -03:00
|
|
|
"github.com/go-task/task/v3/internal/filepathext"
|
2023-11-29 19:38:12 -06:00
|
|
|
"github.com/go-task/task/v3/taskfile"
|
2017-09-16 11:44:13 -03:00
|
|
|
)
|
|
|
|
|
2023-11-29 19:38:12 -06:00
|
|
|
func Globs(dir string, globs []*taskfile.Glob) ([]string, error) {
|
|
|
|
fileMap := make(map[string]bool)
|
2017-09-16 11:44:13 -03:00
|
|
|
for _, g := range globs {
|
2023-11-29 19:38:12 -06:00
|
|
|
matches, err := Glob(dir, g.Glob)
|
2017-11-02 10:25:50 -02:00
|
|
|
if err != nil {
|
2019-08-21 13:33:12 +09:00
|
|
|
continue
|
2017-11-02 10:25:50 -02:00
|
|
|
}
|
2023-11-29 19:38:12 -06:00
|
|
|
for _, match := range matches {
|
|
|
|
fileMap[match] = !g.Negate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
files := make([]string, 0)
|
|
|
|
for file, includePath := range fileMap {
|
|
|
|
if includePath {
|
|
|
|
files = append(files, file)
|
|
|
|
}
|
2019-08-21 13:33:12 +09:00
|
|
|
}
|
|
|
|
sort.Strings(files)
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
2021-05-08 15:02:08 -05:00
|
|
|
func Glob(dir string, g string) ([]string, error) {
|
2019-08-21 13:33:12 +09:00
|
|
|
files := make([]string, 0)
|
2022-08-06 18:19:07 -03:00
|
|
|
g = filepathext.SmartJoin(dir, g)
|
|
|
|
|
2019-08-21 13:33:12 +09:00
|
|
|
g, err := execext.Expand(g)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-06 18:19:07 -03:00
|
|
|
|
2022-08-23 18:25:11 +02:00
|
|
|
fs, err := zglob.GlobFollowSymlinks(g)
|
2019-08-21 13:33:12 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-06 18:19:07 -03:00
|
|
|
|
2019-08-21 13:33:12 +09:00
|
|
|
for _, f := range fs {
|
|
|
|
info, err := os.Stat(f)
|
2017-09-16 11:44:13 -03:00
|
|
|
if err != nil {
|
2019-08-25 16:29:32 -03:00
|
|
|
return nil, err
|
2017-09-16 11:44:13 -03:00
|
|
|
}
|
2019-08-21 13:33:12 +09:00
|
|
|
if info.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
files = append(files, f)
|
2017-09-16 11:44:13 -03:00
|
|
|
}
|
2019-08-21 13:33:12 +09:00
|
|
|
return files, nil
|
2017-09-16 11:44:13 -03:00
|
|
|
}
|