本文整理汇总了Golang中gopkg/in/fsnotify/v1.Watcher类的典型用法代码示例。如果您正苦于以下问题:Golang Watcher类的具体用法?Golang Watcher怎么用?Golang Watcher使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Watcher类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: makeWalker
func makeWalker(base string, w *fsnotify.Watcher) filepath.WalkFunc {
return func(path string, _ os.FileInfo, err error) error {
if err != nil {
log.Fatalf("Error walking: %v", err)
}
rel, err := filepath.Rel(base, path)
if err != nil {
log.Fatalf("Failed to get relative path of %s: %v", path, err)
}
// skip a few things that we know don't form part of the spec
if rel == "api/node_modules" ||
rel == "scripts/gen" ||
rel == "scripts/tmp" {
return filepath.SkipDir
}
// log.Printf("Adding watch on %s", path)
if err := w.Add(path); err != nil {
log.Fatalf("Failed to add watch on %s: %v", path, err)
}
return nil
}
}
示例2: addDirToFileWatcher
func (specInfoGatherer *SpecInfoGatherer) addDirToFileWatcher(watcher *fsnotify.Watcher, dir string) {
err := watcher.Add(dir)
if err != nil {
logger.ApiLog.Error("Unable to add directory %v to file watcher: %s", dir, err)
} else {
logger.ApiLog.Info("Watching directory: %s", dir)
}
}
示例3: TestWatchWrite
func TestWatchWrite(t *testing.T) {
sandbox(func(tmpDir string) {
var err error
var watcher *fsnotify.Watcher
if watcher, err = fsnotify.NewWatcher(); err != nil {
t.Fatal(fmt.Errorf("failed to create watcher: %s", err))
}
defer watcher.Close()
result := make(chan interface{})
timeout := time.Millisecond * 200
go func() {
for {
select {
case event := <-watcher.Events:
result <- event.Op
case err := <-watcher.Errors:
result <- err
case <-time.After(timeout):
result <- fmt.Errorf("event did not occur within timeout of %v", timeout)
}
}
}()
// create file before adding to watcher
var f *os.File
if f, err = os.Create(filepath.Join(tmpDir, "a.txt")); err != nil {
t.Fatal(err)
}
defer f.Close()
if err = watcher.Add(tmpDir); err != nil {
t.Fatal(err)
}
// perform write
f.Write([]byte("hello"))
// wait on result
res := <-result
expect := fsnotify.Write
switch res.(type) {
case fsnotify.Op:
if res != fsnotify.Write {
t.Errorf("expect: %v, result: %v", expect, res)
}
case error:
t.Error(res)
default:
t.Fatal("unexpected response")
}
})
}
示例4: addDirToFileWatcher
func (s *SpecInfoGatherer) addDirToFileWatcher(watcher *fsnotify.Watcher, dir string) {
err := watcher.Add(dir)
if err != nil {
logger.APILog.Error("Unable to add directory %v to file watcher: %s", dir, err)
} else {
logger.APILog.Info("Watching directory: %s", dir)
files, _ := ioutil.ReadDir(dir)
logger.APILog.Debug("Found %d files", len(files))
}
}
示例5: makeWalker
func makeWalker(w *fsnotify.Watcher) filepath.WalkFunc {
return func(path string, _ os.FileInfo, err error) error {
if err != nil {
log.Fatalf("Error walking: %v", err)
}
if err := w.Add(path); err != nil {
log.Fatalf("Failed to add watch: %v", err)
}
return nil
}
}
示例6: AddWatch
func AddWatch(w *fsnotify.Watcher) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
w.Add(path)
} else {
if HasMarkdownSuffix(path) {
tocMutex.Lock()
toc = append(toc, path)
tocMutex.Unlock()
log.Println("Found", path)
w.Add(path)
}
}
return nil
}
}
示例7: handleChange
func handleChange(event fsnotify.Event,
db *SymbolsDB,
watcher *fsnotify.Watcher,
files chan string) {
visitorDir := func(path string) {
// add watcher to directory
watcher.Add(path)
}
visitorC := func(path string) {
// add watcher
watcher.Add(path)
// put file in channel
files <- path
}
switch {
case event.Op&(fsnotify.Write|fsnotify.Create|fsnotify.Chmod) != 0:
traversePath(event.Name, visitorDir, visitorC)
case event.Op&(fsnotify.Remove|fsnotify.Rename) != 0:
watcher.Remove(event.Name)
tx := db.BeginTx()
tx.RemoveFileReferences(filepath.Clean(event.Name))
tx.Close()
}
}
示例8: removeWatcherOn
func (s *SpecInfoGatherer) removeWatcherOn(watcher *fsnotify.Watcher, path string) {
logger.APILog.Info("Removing watcher on : %s", path)
watcher.Remove(path)
}
示例9: removeWatcherOn
func (specInfoGatherer *specInfoGatherer) removeWatcherOn(watcher *fsnotify.Watcher, path string) {
logger.ApiLog.Error("Removing watcher on : %s", path)
watcher.Remove(path)
}