當前位置: 首頁>>代碼示例>>Golang>>正文


Golang fsnotify.Watcher類代碼示例

本文整理匯總了Golang中github.com/howeyc/fsnotify.Watcher的典型用法代碼示例。如果您正苦於以下問題:Golang Watcher類的具體用法?Golang Watcher怎麽用?Golang Watcher使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Watcher類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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


注:本文中的github.com/howeyc/fsnotify.Watcher類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。