func Watcher(c context.Context, task func(string), globs ...string) { files, err := glob.Glob(globs...) if err != nil { c.Error(err) return } // Create some channels f := make(fileNameChan) e := make(errorChan) for matchpair := range files { monitorFile(matchpair.Name, f, e) } go func() { for { select { case fn := <-f: task(fn) case err := <-e: c.Error(err) } } }() }
//Src returns a channel of slurp.Files that match the provided pattern. func Src(c *slurp.C, globs ...string) slurp.Pipe { pipe := make(chan slurp.File) files, err := glob.Glob(globs...) if err != nil { c.Error(err) close(pipe) } cwd, err := os.Getwd() if err != nil { c.Error(err) close(pipe) return pipe } //TODO: Parse globs here, check for invalid globs, split them into "filters". go func() { defer close(pipe) for matchpair := range files { f, err := Read(matchpair.Name) if err != nil { c.Error(err) continue } f.Cwd = cwd f.Dir = glob.Dir(matchpair.Glob) pipe <- *f } }() return pipe }