Пример #1
0
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
0
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
0
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
0
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
0
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
0
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
0
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
0
func (s *SpecInfoGatherer) removeWatcherOn(watcher *fsnotify.Watcher, path string) {
	logger.APILog.Info("Removing watcher on : %s", path)
	watcher.Remove(path)
}
Пример #9
0
func (specInfoGatherer *specInfoGatherer) removeWatcherOn(watcher *fsnotify.Watcher, path string) {
	logger.ApiLog.Error("Removing watcher on : %s", path)
	watcher.Remove(path)
}