本文整理匯總了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
}
示例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
}
示例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())
}
}
示例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)
}
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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()
}
示例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{})
}
示例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 {}
}
示例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
}
示例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))
}
}
示例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
}