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


Golang fsnotify.NewWatcher函數代碼示例

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


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

示例1: watch

func (s *shardedSearcher) watch() error {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return err
	}
	if err := watcher.Add(s.dir); err != nil {
		return err
	}

	go func() {
		for {
			select {
			case <-watcher.Events:
				s.scan()
			case err := <-watcher.Errors:
				if err != nil {
					log.Println("watcher error:", err)
				}
			case <-s.quit:
				watcher.Close()
				return
			}
		}
	}()
	return nil
}
開發者ID:tadeuszwojcik,項目名稱:zoekt,代碼行數:26,代碼來源:shards.go

示例2: New

// New creates a new Watcher and begins watching events
func New() (*Watcher, error) {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return nil, err
	}

	w := &Watcher{
		watcher: watcher,
		watches: make(map[string][]chan<- Event),
		stop:    make(chan struct{}, 1),
	}
	go w.run()
	return w, nil
}
開發者ID:qrtz,項目名稱:livedev,代碼行數:15,代碼來源:watcher.go

示例3: WatchDirectoryChanges

// WatchDirectoryChanges watches a directory and fires the callback with the changed name, receives a logger just to print with red letters any errors, no need for second callback.
func WatchDirectoryChanges(rootPath string, evt func(filename string), logger *logger.Logger) {
	isWindows := runtime.GOOS == "windows"
	watcher, werr := fsnotify.NewWatcher()
	if werr != nil {
		logger.Dangerf(werr.Error())
		return
	}

	go func() {
		var lastChange = time.Now()
		var i = 0
		for {
			select {
			case event := <-watcher.Events:
				if event.Op&fsnotify.Write == fsnotify.Write {
					//this is received two times, the last time is the real changed file, so
					i++
					if i%2 == 0 || !isWindows { // this 'hack' works for windows but I dont know if works for linux too, we can wait for issue reports here.
						if time.Now().After(lastChange.Add(time.Duration(1) * time.Second)) {
							lastChange = time.Now()
							evt(event.Name)
						}
					}

				}
			case err := <-watcher.Errors:
				logger.Dangerf(err.Error())
			}
		}
	}()

	werr = watcher.Add(rootPath)
	if werr != nil {
		logger.Dangerf(werr.Error())

	}

}
開發者ID:toksea,項目名稱:iris,代碼行數:39,代碼來源:file.go

示例4: watcherSetup

func (e *CodeEditor) watcherSetup() {
	var err error
	e.watcher, err = fsnotify.NewWatcher()
	if err != nil {
		log.Printf("Error creating new fsnotify watcher: %s", err)
	}
}
開發者ID:nelsam,項目名稱:vidar,代碼行數:7,代碼來源:editor.go

示例5: ExampleNewWatcher

func ExampleNewWatcher() {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	done := make(chan bool)
	go func() {
		for {
			select {
			case event := <-watcher.Events:
				log.Println("event:", event)
				if event.Op&fsnotify.Write == fsnotify.Write {
					log.Println("modified file:", event.Name)
				}
			case err := <-watcher.Errors:
				log.Println("error:", err)
			}
		}
	}()

	err = watcher.Add("/tmp/foo")
	if err != nil {
		log.Fatal(err)
	}
	<-done
}
開發者ID:vsayer,項目名稱:fsnotify,代碼行數:28,代碼來源:example_test.go

示例6: NewWatcher

func NewWatcher(file string) {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	done := make(chan bool)
	go func() {
		for {
			select {
			case event := <-watcher.Events:
				if event.Op&fsnotify.Write == fsnotify.Write {
					log.Println("modified file:", event.Name)
				} else if event.Op&fsnotify.Rename == fsnotify.Rename {
					log.Println("rename file:", event.Name)
				} else {
					log.Println("event:", event)
				}
			case err := <-watcher.Errors:
				log.Println("error:", err)
			}
		}
	}()

	err = watcher.Add(file)
	if err != nil {
		log.Fatal(err)
	}
	<-done
}
開發者ID:soarpenguin,項目名稱:go-scripts,代碼行數:31,代碼來源:fsnotify.go

示例7: NewEventWatcher

// NewEventWatcher returns an fs-event based file watcher
func NewEventWatcher() (FileWatcher, error) {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return nil, err
	}
	return &fsNotifyWatcher{watcher}, nil
}
開發者ID:jfrazelle,項目名稱:docker,代碼行數:8,代碼來源:filenotify.go

示例8: NewSession

func NewSession(options *Options) *Session {
	if options.GOROOT == "" {
		options.GOROOT = build.Default.GOROOT
	}
	if options.GOPATH == "" {
		options.GOPATH = build.Default.GOPATH
	}
	options.Verbose = options.Verbose || options.Watch

	s := &Session{
		options:  options,
		Archives: make(map[string]*compiler.Archive),
	}
	s.Types = make(map[string]*types.Package)
	if options.Watch {
		if out, err := exec.Command("ulimit", "-n").Output(); err == nil {
			if n, err := strconv.Atoi(strings.TrimSpace(string(out))); err == nil && n < 1024 {
				fmt.Printf("Warning: The maximum number of open file descriptors is very low (%d). Change it with 'ulimit -n 8192'.\n", n)
			}
		}

		var err error
		s.Watcher, err = fsnotify.NewWatcher()
		if err != nil {
			panic(err)
		}
	}
	return s
}
開發者ID:moul,項目名稱:advanced-ssh-config,代碼行數:29,代碼來源:build.go

示例9: startFileWatcher

func startFileWatcher(file string) *fsnotify.Watcher {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		for {
			select {
			case event := <-watcher.Events:
				log.Println("evt:", event)
				if event.Op&fsnotify.Write == fsnotify.Write {
					log.Println("modified file:", event.Name)
				}
			case err := <-watcher.Errors:
				log.Println("error:", err)
			}
		}
	}()
	err = watcher.Add(file)
	if err != nil {
		log.Fatal(err)
	}

	return watcher
}
開發者ID:Opiskull,項目名稱:go-lazy-remote,代碼行數:26,代碼來源:filewatcher.go

示例10: registerVM

func registerVM(vm *modules.JsVm) otto.Value {
	watcher, err := fsnotify.NewWatcher()
	//defer watcher.Close()
	if err != nil {
		log.Println(pluginName + " " + err.Error())
	}
	obj, _ := vm.Object("({})")

	obj.Set("watchDir", func(c otto.FunctionCall) otto.Value {
		path, _ := c.Argument(0).ToString()
		err := watcher.Add(path)
		return modules.ToResult(vm, true, err)
	})

	obj.Set("start", func(c otto.FunctionCall) otto.Value {
		log.Println(pluginName + " start")
		go func() {
			for {
				select {
				case ev := <-watcher.Events:
					log.Println("event:", ev.String())
				case err := <-watcher.Errors:
					log.Println("error:", err)
				}
			}
		}()
		return otto.TrueValue()
	})
	return obj.Value()
}
開發者ID:BlackEspresso,項目名稱:ServeJS,代碼行數:30,代碼來源:filewatch.go

示例11: SetRoot

func (p *ProjectTree) SetRoot(path string) {
	p.layout.RemoveAll()
	p.SetTOC(nil)
	p.tocCtl = nil

	if p.watcher != nil {
		p.watcher.Close()
	}
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Printf("Error creating project tree watcher: %s", err)
	}
	p.watcher = watcher
	p.startWatch(path)

	p.dirs = newDirectory(p, path)
	scrollable := p.theme.CreateScrollLayout()
	// Disable horiz scrolling until we can figure out an accurate
	// way to calculate our width.
	scrollable.SetScrollAxis(false, true)
	scrollable.SetChild(p.dirs)
	p.layout.AddChild(scrollable)
	p.layout.SetChildWeight(p.dirs, 1)

	// Expand the top level
	p.dirs.button.Click(gxui.MouseEvent{})
}
開發者ID:nelsam,項目名稱:vidar,代碼行數:27,代碼來源:project_tree.go

示例12: Start

func (jitome *Jitome) Start() error {
	// check init task
	// register watchers
	for name, target := range jitome.config.Targets {
		if name == "init" {
			continue
		}

		target.Name = name
		target.events = make(chan *Event, 30)
		target.jitome = jitome

		log.Print("evaluating target '" + FgCB(name) + "'.")

		// register watched directories
		for i, watchConfig := range target.Watch {
			watchConfig.InitPatterns()
			w, err := fsnotify.NewWatcher()
			if err != nil {
				return err
			}

			err = watch(watchConfig.Base, watchConfig.ignoreRegs, w)
			if err != nil {
				return err
			}

			watcher, err := NewWatcher(jitome, target, watchConfig, w, i)
			if err != nil {
				return err
			}

			jitome.watchers = append(jitome.watchers, watcher)
			go watcher.Wait()
		}
		go target.Wait()
	}
	defer jitome.Close()

	go func() {
		log.Print("watching files...")
		for {
			event := <-jitome.events
			runTarget(event)
		}
	}()

	go func() {
		err := jitome.restartCommand()
		if err != nil {
			log.Print(FgRB(fmt.Sprintf("[warning] %v", err)))
		}
	}()

	// wait infinitely.
	select {}
}
開發者ID:kohkimakimoto,項目名稱:jitome,代碼行數:57,代碼來源:jitome.go

示例13: newDispatcher

func newDispatcher() (*dispatcher, error) {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return nil, err
	}
	d := &dispatcher{watcher: watcher, closer: make(chan struct{}), routes: map[string]chan struct{}{}}
	go d.listen()
	return d, nil
}
開發者ID:johnsiilver,項目名稱:golib,代碼行數:9,代碼來源:local.go

示例14: main

func main() {
	flag.Parse()
	file, err := ioutil.ReadFile(*configFile)
	if err != nil {
		log.Fatal("unable to read config file, exiting...")
	}
	if err := json.Unmarshal(file, &parsedConfig); err != nil {
		log.Fatal("unable to marshal config file, exiting...")
	}

	// fire up filesystem watcher
	mywatcher, err = fsnotify.NewWatcher()
	if err != nil {
		log.Fatal("error creating fswatcher: ", err)
	}

	if err := createDirs(parsedConfig); err != nil {
		log.Println(err)
		log.Fatalf("error creating directory structure, exiting")
	}

	go func() {
		for {
			select {
			case event := <-mywatcher.Events:
				if (event.Op&fsnotify.Write == fsnotify.Write) || (event.Op&fsnotify.Create == fsnotify.Create) || (event.Op&fsnotify.Remove == fsnotify.Remove) {
					log.Println("modified file:", event.Name)
					mutex.Lock()

					log.Println("got lock, updating package list...")
					distroArch := destructPath(event.Name)
					if filepath.Base(event.Name) != "Packages.gz" {
						if err := createPackagesGz(parsedConfig, distroArch[0], distroArch[1]); err != nil {
							log.Println("error creating package: %s", err)
						}
					}
					mutex.Unlock()
				}
			case err := <-mywatcher.Errors:
				log.Println("error:", err)
			}
		}
	}()

	http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(parsedConfig.RootRepoPath))))
	http.Handle("/upload", uploadHandler(parsedConfig))
	http.Handle("/delete", deleteHandler(parsedConfig))

	if parsedConfig.EnableSSL {
		log.Println("running with SSL enabled")
		log.Fatal(http.ListenAndServeTLS(":"+parsedConfig.ListenPort, parsedConfig.SSLCert, parsedConfig.SSLKey, nil))
	} else {
		log.Println("running without SSL enabled")
		log.Fatal(http.ListenAndServe(":"+parsedConfig.ListenPort, nil))
	}
}
開發者ID:esell,項目名稱:deb-simple,代碼行數:56,代碼來源:main.go

示例15: NewWatcher

// NewWatcher create the new watcher
func NewWatcher() (*FileWatcher, error) {
	tmpWatcher, err := fsnotify.NewWatcher()
	if err != nil {
		return nil, err
	}
	w := &FileWatcher{
		watcher: tmpWatcher,
	}
	return w, nil
}
開發者ID:Simbory,項目名稱:wemvc,代碼行數:11,代碼來源:fileWatcher.go


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