• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang fsnotify.Watcher类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/howeyc/fsnotify.Watcher的典型用法代码示例。如果您正苦于以下问题:Golang Watcher类的具体用法?Golang Watcher怎么用?Golang Watcher使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Watcher类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: watchDir

func watchDir(w *fsnotify.Watcher, dir string, reload func()) error {
	if err := w.Watch(dir); err != nil {
		return err
	}

	// And also watch subdirectories
	if d, err := os.Open(dir); err != nil {
		return err
	} else {
		defer d.Close()

		if subDirs, err := d.Readdir(-1); err != nil {
			return err
		} else {
			for _, f := range subDirs {
				if !f.IsDir() {
					continue
				}
				if err := watchDir(w, path.Join(dir, f.Name()), reload); err != nil {
					return err
				}
			}
		}
	}
	return nil
}
开发者ID:tetleyt,项目名称:hastie,代码行数:26,代码来源:http.go


示例2: refreshIndex

func refreshIndex(dir string, watcher *fsnotify.Watcher, sync chan *SyncEvent) {
	entries, err := ioutil.ReadDir(dir)

	if err != nil {
		log.Println(err)
		return
	}

	_, inIndex := index[dir]

	if !inIndex {
		watcher.Watch(dir)
	}

	index[dir] = true

	for _, e := range entries {
		name := e.Name()

		if e.IsDir() && !strings.HasPrefix(name, ".") {
			refreshIndex(filepath.Join(dir, name), watcher, sync)

			// Schedule the file for syncing if the directory was not found
			// within the index
		} else if !inIndex {
			sync <- &SyncEvent{Name: filepath.Join(dir, name), Type: SYNC_PUT}
		}
	}
}
开发者ID:CHH,项目名称:livesyncd,代码行数:29,代码来源:livesyncd.go


示例3: _watch

func _watch(watcher *fsnotify.Watcher, sig chan<- *fsnotify.FileEvent, match func(*fsnotify.FileEvent) bool) {
	for cont := true; cont; {
		select {
		case ev := <-watcher.Event:
			Debug(2, "event:", ev)
			if ev.IsCreate() {
				info, err := os.Stat(ev.Name)
				if err != nil {
					Debug(2, "stat error: ", err)
					continue
				}
				if info.IsDir() {
					Debug(1, "watching: ", ev.Name)
					err := watcher.Watch(ev.Name)
					if err != nil {
						Error("watch error: ", err)
					}
				}
			}

			if match(ev) {
				sig <- ev
			}
		case err := <-watcher.Error:
			Print("watcher error:", err)
		}
	}
	close(sig)
}
开发者ID:bmatsuo,项目名称:go-views,代码行数:29,代码来源:watch.go


示例4: walkAndWatch

// Walk a path and watch non-hidden or build directories
func walkAndWatch(path string, w *fsnotify.Watcher) (watched uint) {
	f := func(path string, fi os.FileInfo, err error) error {
		if fi.IsDir() {
			// Skip hidden directories
			if fi.Name()[0] == '.' {
				return filepath.SkipDir
			}

			// Skip *build* directories
			if strings.Contains(fi.Name(), "build") {
				return filepath.SkipDir
			}

			// Skip *static* directories
			if strings.Contains(fi.Name(), "static") {
				return filepath.SkipDir
			}

			// Watch this path
			err = w.Watch(path)
			watched++
			if err != nil {
				log.Fatalf("Error trying to watch %s:\n%v\n%d paths watched", path, err, watched)
			}
		}
		return nil
	}

	err := filepath.Walk(path, f)
	if err != nil && err != filepath.SkipDir {
		log.Fatal("Error walking tree: %v\n", err)
	}
	return
}
开发者ID:schmichael,项目名称:gosphinxbuild,代码行数:35,代码来源:gosphinxbuild.go


示例5: MonitorFile

func MonitorFile(filename string, out chan []string,
	watcher *fsnotify.Watcher) {
	size := GetFileSize(filename)
	go func() {
		for {
			select {
			case ev := <-watcher.Event:
				if ev.IsModify() {
					NewSize := GetFileSize(ev.Name)
					if NewSize <= size {
						MonitorFile(ev.Name, out, watcher)
						return
					}
					content := ReadNBytes(ev.Name, size,
						NewSize-1)
					size = NewSize
					out <- ByteArrayToMultiLines(content)
				}
			case err := <-watcher.Error:
				log.Println("error:", err)
			}
		}
	}()
	err := watcher.Watch(filename)
	if err != nil {
		log.Fatal(err)
	}
}
开发者ID:spin6lock,项目名称:gotail,代码行数:28,代码来源:main.go


示例6: watchDeep

// WatchDeep watches a directory and all
// of its subdirectories.  If the path is not
// a directory then watchDeep is a no-op.
func watchDeep(w *fsnotify.Watcher, path string) {
	info, err := os.Stat(path)
	if os.IsNotExist(err) {
		// This file disapeared on us, fine.
		return
	}
	if err != nil {
		die(err)
	}
	if !info.IsDir() {
		return
	}

	if err := w.Watch(path); err != nil {
		die(err)
	}

	f, err := os.Open(path)
	if err != nil {
		die(err)
	}
	ents, err := f.Readdirnames(-1)
	if err != nil {
		die(err)
	}
	f.Close()

	for _, e := range ents {
		watchDeep(w, filepath.Join(path, e))
	}
}
开发者ID:vivounicorn,项目名称:eaburns,代码行数:34,代码来源:main.go


示例7: watch

func watch(root string, watcher *fsnotify.Watcher, names chan<- string, done chan<- error) {
	if err := filepath.Walk(root, walker(watcher)); err != nil {
		// TODO: handle this somehow?
		infoPrintf(-1, "Error while walking path %s: %s\n", root, err)
	}

	for {
		select {
		case e := <-watcher.Event:
			path := strings.TrimPrefix(e.Name, "./")
			names <- path
			if e.IsCreate() {
				if err := filepath.Walk(path, walker(watcher)); err != nil {
					// TODO: handle this somehow?
					infoPrintf(-1, "Error while walking path %s: %s\n", path, err)
				}
			}
			if e.IsDelete() {
				watcher.RemoveWatch(path)
			}
		case err := <-watcher.Error:
			done <- err
			return
		}
	}
}
开发者ID:rliebling,项目名称:reflex,代码行数:26,代码来源:workers.go


示例8: processSubdir

func processSubdir(watcher *fsnotify.Watcher, ev *fsnotify.FileEvent) {
	if ev.IsModify() {
		return
	}
	if ev.IsDelete() {
		log.Println("remove watch", ev.Name)
		// FIXME: what to do with err?
		watcher.RemoveWatch(ev.Name)
		return
	}
	// FIXME: Lstat or Stat?
	// TODO: what to do with err? can we safely ignore?
	mode, err := os.Lstat(ev.Name)
	if err != nil {
		log.Println("error processing subdir:", err.Error())
		return
	}
	if !mode.IsDir() {
		return
	}

	// FIXME: handle renames
	if ev.IsCreate() {
		log.Println("add watch", ev.Name)
		// FIXME: what to do with err?
		watcher.Watch(ev.Name)
	}
}
开发者ID:akavel,项目名称:gomon,代码行数:28,代码来源:watcher.go


示例9: recursive

// recursive watches the given dir and recurses into all subdirectories as well
func recursive(watcher *fsnotify.Watcher, dir string) error {
	info, err := os.Stat(dir)

	if err == nil && !info.Mode().IsDir() {
		err = errors.New("Watching a file is not supported. Expected a directory")
	}

	// Watch the specified dir
	if err == nil {
		err = watcher.Watch(dir)
	}

	var list []os.FileInfo

	// Grab list of subdirs
	if err == nil {
		list, err = ioutil.ReadDir(dir)
	}

	// Call recursive for each dir in list
	if err == nil {
		for _, file := range list {
			recursive(watcher, filepath.Join(dir, file.Name()))
		}
	}

	return err
}
开发者ID:codeblanche,项目名称:golibs,代码行数:29,代码来源:watch.go


示例10: rerun

func rerun(buildpath string, args []string) (err error) {
	log.Printf("setting up %s %v", buildpath, args)

	pkg, err := build.Import(buildpath, "", 0)
	if err != nil {
		return
	}

	if pkg.Name != "main" {
		err = errors.New(fmt.Sprintf("expected package %q, got %q", "main", pkg.Name))
		return
	}

	_, binName := path.Split(buildpath)
	binPath := filepath.Join(pkg.BinDir, binName)

	runch := run(binName, binPath, args)

	var errorOutput string
	_, errorOutput, ierr := install(buildpath, errorOutput)
	if ierr == nil {
		runch <- true
	}

	var watcher *fsnotify.Watcher
	watcher, err = getWatcher(buildpath)
	if err != nil {
		return
	}

	for {
		we, _ := <-watcher.Event
		var installed bool
		installed, errorOutput, _ = install(buildpath, errorOutput)
		if installed {
			log.Print(we.Name)
			runch <- true
			watcher.Close()
			/* empty the buffer */
			go func(events chan *fsnotify.FileEvent) {
				for _ = range events {

				}
			}(watcher.Event)
			go func(errors chan error) {
				for _ = range errors {

				}
			}(watcher.Error)
			/* rescan */
			log.Println("rescanning")
			watcher, err = getWatcher(buildpath)
			if err != nil {
				return
			}
		}
	}
	return
}
开发者ID:vuleetu,项目名称:rerun,代码行数:59,代码来源:rerun.go


示例11: watchAllDirs

// watchAllDirs will watch a directory and then walk through all subdirs
// of that directory and watch them too
func watchAllDirs(watcher *fsnotify.Watcher, root string) (err error) {
	walkFn := func(path string, info os.FileInfo, err error) error {
		if info.IsDir() {
			watcher.Watch(path)
		}
		return nil
	}
	return filepath.Walk(root, walkFn)
}
开发者ID:ChrisBuchholz,项目名称:war,代码行数:11,代码来源:watch.go


示例12: CloseWatcher

func (t *InotifyTracker) CloseWatcher(w *fsnotify.Watcher) (err error) {
	t.mux.Lock()
	defer t.mux.Unlock()
	if _, ok := t.watchers[w]; ok {
		err = w.Close()
		delete(t.watchers, w)
	}
	return
}
开发者ID:GeertJohan,项目名称:tail,代码行数:9,代码来源:inotify_tracker.go


示例13: watchAll

func watchAll(watcher *fsnotify.Watcher) filepath.WalkFunc {
	return func(fn string, fi os.FileInfo, err error) error {
		if err != nil {
			return nil
		}

		watcher.Watch(fn)
		return nil
	}
}
开发者ID:hooblei,项目名称:gostatic,代码行数:10,代码来源:watch.go


示例14: main

func main() {

	var (
		err            error
		watcher        *fsnotify.Watcher
		done           chan bool
		includePattern string
		include        *regexp.Regexp
		watchedFile    string
	)

	flag.StringVar(&watchedFile, "watch", "none", `Directory to watch for modification events`)
	flag.StringVar(&includePattern, "include", "", `Filename pattern to include (regex)`)
	flag.Parse()

	include = nil
	if includePattern != "" {
		include = regexp.MustCompile(includePattern)
	}

	watcher, err = fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}

	done = make(chan bool)

	go func(include *regexp.Regexp, hdlr modifyHandler) {
		for {
			select {
			case ev := <-watcher.Event:
				// log.Println("event:", ev)
				if include == nil || include.MatchString(ev.Name) {
					if ev.IsModify() || ev.IsCreate() {
						log.Printf("Calling handler\n")
						hdlr(ev.Name, "make")
					}
				}
			case err := <-watcher.Error:
				log.Println("error:", err)
			}
		}
	}(include, triggerCmd)

	err = watcher.Watch(watchedFile)
	if err != nil {
		fmt.Printf("%s: %v\n", watchedFile, err)
		os.Exit(1)
	}

	<-done

	watcher.Close()
}
开发者ID:marthjod,项目名称:scripts,代码行数:54,代码来源:watchmakr.go


示例15: monitorConf

func monitorConf(file string, wa *fsnotify.Watcher) {
	for {
		err := wa.Watch(file)
		if err != nil {
			time.Sleep(2 * time.Second)
			continue
		}
		log.Error("Set Watch [%s] ok!", file)
		break
	}
}
开发者ID:vashstorm,项目名称:hahaproxy,代码行数:11,代码来源:hahaproxy.go


示例16: walker

func walker(watcher *fsnotify.Watcher) filepath.WalkFunc {
	return func(path string, f os.FileInfo, err error) error {
		if err != nil || !f.IsDir() {
			// TODO: Is there some other thing we should be doing to handle errors? When watching large
			// directories that have lots of programs modifying them (esp. if they're making tempfiles along the
			// way), we often get errors.
			return nil
		}
		if err := watcher.Watch(path); err != nil {
			// TODO: handle this somehow?
			infoPrintf(-1, "Error while watching new path %s: %s\n", path, err)
		}
		return nil
	}
}
开发者ID:rliebling,项目名称:reflex,代码行数:15,代码来源:workers.go


示例17: watchEvents

func (db *DB) watchEvents(watcher *fsnotify.Watcher) {
	for {
		select {
		case ev := <-watcher.Event:
			if ev.Name == db.file && (ev.IsCreate() || ev.IsModify()) {
				db.openFile()
			}
		case <-watcher.Error:
		case <-db.notifyQuit:
			watcher.Close()
			return
		}
		time.Sleep(time.Second) // Suppress high-rate events.
	}
}
开发者ID:joyang1,项目名称:freegeoip,代码行数:15,代码来源:db.go


示例18: watchRecursive

func watchRecursive(dir string, watcher *fsnotify.Watcher) error {
	err := watcher.Watch(dir)
	if err != nil {
		return err
	}
	dirFiles, err := ioutil.ReadDir(dir)
	for _, file := range dirFiles {
		if file.IsDir() {
			err = watchRecursive(path.Join(dir, file.Name()), watcher)
			if err != nil {
				return err
			}
		}
	}
	return nil
}
开发者ID:ungerik,项目名称:gosync,代码行数:16,代码来源:gosync.go


示例19: addToWatcher

func addToWatcher(watcher *fsnotify.Watcher, importpath string, watching map[string]bool) {
	pkg, err := build.Import(importpath, "", 0)
	if err != nil {
		return
	}
	if pkg.Goroot {
		return
	}
	watcher.Watch(pkg.Dir)
	watching[importpath] = true
	for _, imp := range pkg.Imports {
		if !watching[imp] {
			addToWatcher(watcher, imp, watching)
		}
	}
}
开发者ID:sinni800,项目名称:rerun,代码行数:16,代码来源:rerun.go


示例20: walkHandler

// adds a watcher.Watch to every directory
func walkHandler(watcher *fsnotify.Watcher) filepath.WalkFunc {
	return func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		// add a watch if path is a directory
		if info.IsDir() {
			errAddWatch := watcher.Watch(path)
			if errAddWatch != nil {
				// handle
				return errAddWatch
			}
			log.Println("Watching dir:", path)
		}
		return nil
	}
}
开发者ID:peterpavloman,项目名称:OSP_Raspbox,代码行数:19,代码来源:csync_daemon.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang gopass.GetPasswd函数代码示例发布时间:2022-05-28
下一篇:
Golang fsnotify.FileEvent类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap