Closed kbirk closed 6 years ago
Add a filter function may be a better way. w.Add(path,filter)
func filter(fileName string) bool
Is there a good way to go about this with the CLI?
Hey guys!
For now I would think that the easiest way to do this would be to use the filepath.Glob
method that the standard library provides for adding and ignoring files :)
(If it seems reasonable to shortcut this and allow globs directly in the add and ignore methods I can consider that too)
For example:
package main
import (
"fmt"
"log"
"path/filepath"
"github.com/radovskyb/watcher"
)
func main() {
w := watcher.New()
paths, err := filepath.Glob("**/*.go")
if err != nil {
log.Fatalln(err)
}
for _, path := range paths {
err := w.Add(path)
if err != nil {
log.Fatalln(err)
}
}
fmt.Println("before ignore:")
for path := range w.WatchedFiles() {
fmt.Println(path)
}
err = w.Ignore(paths...)
if err != nil {
log.Fatalln(err)
}
fmt.Println("\nafter ignore:")
for path := range w.WatchedFiles() {
fmt.Println(path)
}
// ...
}
However, one thing to note is that it would decrease performance if the plan was to have the globs for ignoring files updated in between polling to see if any other files added match the glob (if the polling duration is a short one), since it would have to re-glob to see if there were any other files that need to be ignored. If the plan is simply to just ignore what's already there though, then filepath.Glob
should be sufficient like within the above example.
(Just btw, if I recall correctly filepath.Glob
isn't recursive with it's matching, so if it isn't sufficient for you guys, there are probably some good globbing libraries out there where you can just replace the part in my example where I used filepath.Glob
to fetch paths.)
I hope that makes sense!
@skinnyjames It might actually make sense for me to consider that too if I end up adding it to the regular add and ignore methods.
Did using filepath.Glob work out for you @kbirk? :)
@skinnyjames Was the CLI question something that you might find useful? Feel free to submit any pull requests if you want by the way, but otherwise I might be able to chuck it in the CLI sometime soonish.
Yes it did work, thank you :). Eventually I needed the globs to be run between polling to check for file additions so I ended up writing my own solution down the line. You can close this issue if you like.
Please do add this to the CLI. I'd love to not use fswatch.
Would it be possible for the watcher API to support Globs as arguments to the
Add
andIgnore
methods? This would make watching specific directories for only certain file types much easier:Or is there a way to achieve this with the current interface?