...

Source file src/github.com/redhat-developer/odo/pkg/testingutil/filesystem/watcher.go

Documentation: github.com/redhat-developer/odo/pkg/testingutil/filesystem

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  /*
    18  	This package is a FORK of https://github.com/kubernetes/kubernetes/blob/master/pkg/util/filesystem/watcher.go
    19  	See above license
    20  */
    21  
    22  package filesystem
    23  
    24  import (
    25  	"github.com/fsnotify/fsnotify"
    26  )
    27  
    28  // FSWatcher is a callback-based filesystem watcher abstraction for fsnotify.
    29  type FSWatcher interface {
    30  	// Initializes the watcher with the given watch handlers.
    31  	// Called before all other methods.
    32  	Init(FSEventHandler, FSErrorHandler) error
    33  
    34  	// Starts listening for events and errors.
    35  	// When an event or error occurs, the corresponding handler is called.
    36  	Run()
    37  
    38  	// Add a filesystem path to watch
    39  	AddWatch(path string) error
    40  }
    41  
    42  // FSEventHandler is called when a fsnotify event occurs.
    43  type FSEventHandler func(event fsnotify.Event)
    44  
    45  // FSErrorHandler is called when a fsnotify error occurs.
    46  type FSErrorHandler func(err error)
    47  
    48  type fsnotifyWatcher struct {
    49  	watcher      *fsnotify.Watcher
    50  	eventHandler FSEventHandler
    51  	errorHandler FSErrorHandler
    52  }
    53  
    54  var _ FSWatcher = &fsnotifyWatcher{}
    55  
    56  func (w *fsnotifyWatcher) AddWatch(path string) error {
    57  	return w.watcher.Add(path)
    58  }
    59  
    60  func (w *fsnotifyWatcher) Init(eventHandler FSEventHandler, errorHandler FSErrorHandler) error {
    61  	var err error
    62  	w.watcher, err = fsnotify.NewWatcher()
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	w.eventHandler = eventHandler
    68  	w.errorHandler = errorHandler
    69  	return nil
    70  }
    71  
    72  func (w *fsnotifyWatcher) Run() {
    73  	go func() {
    74  		defer w.watcher.Close()
    75  		for {
    76  			select {
    77  			case event := <-w.watcher.Events:
    78  				if w.eventHandler != nil {
    79  					w.eventHandler(event)
    80  				}
    81  			case err := <-w.watcher.Errors:
    82  				if w.errorHandler != nil {
    83  					w.errorHandler(err)
    84  				}
    85  			}
    86  		}
    87  	}()
    88  }
    89  

View as plain text