当前位置: 首页>>代码示例>>Golang>>正文


Golang v1.Watcher类代码示例

本文整理汇总了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
	}
}
开发者ID:Ralith,项目名称:matrix-doc,代码行数:25,代码来源:main.go

示例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)
	}
}
开发者ID:christophermoura,项目名称:gauge,代码行数:8,代码来源:specDetails.go

示例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")
		}
	})

}
开发者ID:pfeilbr,项目名称:go-examples,代码行数:57,代码来源:basic_test.go

示例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))
	}
}
开发者ID:0-T-0,项目名称:gauge,代码行数:10,代码来源:specDetails.go

示例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
	}
}
开发者ID:frankgerhardt,项目名称:matrix-doc,代码行数:11,代码来源:main.go

示例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
	}
}
开发者ID:abhijitmamarde,项目名称:livemd,代码行数:19,代码来源:livemd.go

示例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()
	}
}
开发者ID:Sandy4321,项目名称:navc,代码行数:26,代码来源:main.go

示例8: removeWatcherOn

func (s *SpecInfoGatherer) removeWatcherOn(watcher *fsnotify.Watcher, path string) {
	logger.APILog.Info("Removing watcher on : %s", path)
	watcher.Remove(path)
}
开发者ID:0-T-0,项目名称:gauge,代码行数:4,代码来源:specDetails.go

示例9: removeWatcherOn

func (specInfoGatherer *specInfoGatherer) removeWatcherOn(watcher *fsnotify.Watcher, path string) {
	logger.ApiLog.Error("Removing watcher on : %s", path)
	watcher.Remove(path)
}
开发者ID:Jayasagar,项目名称:gauge,代码行数:4,代码来源:specDetails.go


注:本文中的gopkg/in/fsnotify/v1.Watcher类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。