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