summaryrefslogtreecommitdiff
path: root/predicate.go
diff options
context:
space:
mode:
authorEyal Posener <[email protected]>2017-05-05 23:07:18 +0300
committerEyal Posener <[email protected]>2017-05-05 23:07:18 +0300
commitc8263230e11aa755d3b0b964d4bf7ff296661f5e (patch)
tree257d6dfb9218ad536d3b20956b499f2b74ef271c /predicate.go
parent04d16f6064c7c71068f47b4e7106f15a05b6b326 (diff)
Add additional args
add predition of directories add ability for prediction union
Diffstat (limited to 'predicate.go')
-rw-r--r--predicate.go51
1 files changed, 43 insertions, 8 deletions
diff --git a/predicate.go b/predicate.go
index 5ba544b..1cc6cca 100644
--- a/predicate.go
+++ b/predicate.go
@@ -16,11 +16,20 @@ type Predicate struct {
Predictor func() []Option
}
-func (f *Predicate) predict() []Option {
- if f.Predictor == nil {
+// Or unions two predicate struct, so that the result predicate
+// returns the union of their predication
+func (p Predicate) Or(other Predicate) Predicate {
+ return Predicate{
+ Expects: p.Expects && other.Expects,
+ Predictor: func() []Option { return append(p.predict(), other.predict()...) },
+ }
+}
+
+func (p Predicate) predict() []Option {
+ if p.Predictor == nil {
return nil
}
- return f.Predictor()
+ return p.Predictor()
}
var (
@@ -35,6 +44,28 @@ func PredictFiles(pattern string) Predicate {
}
}
+func PredictDirs(path string) Predicate {
+ return Predicate{
+ Expects: true,
+ Predictor: dirs(path),
+ }
+}
+func dirs(path string) func() []Option {
+ return func() (options []Option) {
+ dirs := []string{}
+ filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
+ if info.IsDir() {
+ dirs = append(dirs, path)
+ }
+ return nil
+ })
+ if !filepath.IsAbs(path) {
+ filesToRel(dirs)
+ }
+ return filesToOptions(dirs)
+ }
+}
+
func glob(pattern string) func() []Option {
return func() []Option {
files, err := filepath.Glob(pattern)
@@ -44,11 +75,7 @@ func glob(pattern string) func() []Option {
if !filepath.IsAbs(pattern) {
filesToRel(files)
}
- options := make([]Option, len(files))
- for i, f := range files {
- options[i] = ArgFileName(f)
- }
- return options
+ return filesToOptions(files)
}
}
func filesToRel(files []string) {
@@ -69,3 +96,11 @@ func filesToRel(files []string) {
}
return
}
+
+func filesToOptions(files []string) []Option {
+ options := make([]Option, len(files))
+ for i, f := range files {
+ options[i] = ArgFileName(f)
+ }
+ return options
+}