本文整理汇总了Golang中gopkg/in/fsnotify/v1.NewWatcher函数的典型用法代码示例。如果您正苦于以下问题:Golang NewWatcher函数的具体用法?Golang NewWatcher怎么用?Golang NewWatcher使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewWatcher函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
flag.Parse()
path = os.Getenv("PWD")
if len(flag.Args()) > 1 {
path = flag.Arg(1)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
updates := make(chan Update)
go WatcherEventLoop(watcher, updates, done)
log.Println("Watching directory", path)
err = filepath.Walk(path, AddWatch(watcher))
if err != nil {
log.Fatal(err)
}
listeners := make(chan Listener)
go UpdateListeners(updates, listeners)
http.HandleFunc("/", RootFunc)
http.HandleFunc("/md/", PageFunc)
http.HandleFunc("/github.css", CSSFunc(githubCss))
http.Handle("/ws/", websocket.Handler(HandleListener(listeners)))
http.ListenAndServe(fmt.Sprintf("%s:%s", *host, *port), nil)
}
示例2: AddPair
func (s *Syncer) AddPair(left, right string, config *PairConfig) error {
lUri, err := uri.Parse(left)
if err != nil {
return err
}
rUri, err := uri.Parse(right)
if err != nil {
return err
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
p := &Pair{
Left: lUri,
Right: rUri,
watcher: watcher,
progress: make(chan int64),
handlers: make(map[fsnotify.Op][]OpHandler),
syncer: s,
Config: config,
}
s.Pairs = append(s.Pairs, p)
go func(pair *Pair) {
s.addPair <- pair
fmt.Println("add pair", pair.Left.Uri())
}(p)
return nil
}
示例3: main
func main() {
flag.Parse()
w, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("Error making watcher: %v", err)
}
dir, err := os.Getwd()
if err != nil {
log.Fatalf("Error getting wd: %v", err)
}
for ; !exists(path.Join(dir, ".git")); dir = path.Dir(dir) {
if dir == "/" {
log.Fatalf("Could not find git root")
}
}
filepath.Walk(dir, makeWalker(w))
wg.Add(1)
populateOnce(dir)
ch := make(chan struct{}, 100) // Buffered to ensure we can multiple-increment wg for pending writes
go doPopulate(ch, dir)
go watchFS(ch, w)
fmt.Printf("Listening on port %d\n", *port)
http.HandleFunc("/", serve)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
}
示例4: watchForFileChanges
func (s *SpecInfoGatherer) watchForFileChanges() {
s.waitGroup.Add(1)
watcher, err := fsnotify.NewWatcher()
if err != nil {
logger.APILog.Error("Error creating fileWatcher: %s", err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
s.handleEvent(event, watcher)
case err := <-watcher.Errors:
logger.APILog.Error("Error event while watching specs", err)
}
}
}()
allDirsToWatch := make([]string, 0)
specDir := filepath.Join(config.ProjectRoot, common.SpecsDirectoryName)
allDirsToWatch = append(allDirsToWatch, specDir)
allDirsToWatch = append(allDirsToWatch, util.FindAllNestedDirs(specDir)...)
for _, dir := range allDirsToWatch {
s.addDirToFileWatcher(watcher, dir)
}
s.waitGroup.Done()
<-done
}
示例5: NewWatcher
// NewWatcher create watcher
func NewWatcher(dirs []string) (*Watcher, error) {
// w
w, err := fs.NewWatcher()
if err != nil {
return nil, err
}
// listen
log.Println("[umon]", "Watcher: watch dirs", dirs)
for _, dir := range dirs {
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info == nil {
return err
}
if !info.IsDir() {
return nil
}
w.Add(path)
return nil
})
}
// ok
return &Watcher{
w: w,
exit: make(ChanExit, 1),
}, nil
}
示例6: NewNotifier
func NewNotifier(config *Config) (*Notifier, error) {
w, err := fsnotify.NewWatcher()
if err != nil {
return &Notifier{}, err
}
return &Notifier{c: config, w: w}, nil
}
示例7: TestWatchWrite
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")
}
})
}
示例8: main
func main() {
if *hotReload {
// set up file watcher
log.Printf("setting up file watcher for %s\n", *hooksFilePath)
var err error
watcher, err = fsnotify.NewWatcher()
if err != nil {
log.Fatal("error creating file watcher instance", err)
}
defer watcher.Close()
go watchForFileChange()
err = watcher.Add(*hooksFilePath)
if err != nil {
log.Fatal("error adding hooks file to the watcher", err)
}
}
l := negroni.NewLogger()
l.Logger = log.New(os.Stdout, "[webhook] ", log.Ldate|log.Ltime)
negroniRecovery := &negroni.Recovery{
Logger: l.Logger,
PrintStack: true,
StackAll: false,
StackSize: 1024 * 8,
}
n := negroni.New(negroniRecovery, l)
router := mux.NewRouter()
var hooksURL string
if *hooksURLPrefix == "" {
hooksURL = "/{id}"
} else {
hooksURL = "/" + *hooksURLPrefix + "/{id}"
}
router.HandleFunc(hooksURL, hookHandler)
n.UseHandler(router)
if *secure {
log.Printf("starting secure (https) webhook on %s:%d", *ip, *port)
log.Fatal(http.ListenAndServeTLS(fmt.Sprintf("%s:%d", *ip, *port), *cert, *key, n))
} else {
log.Printf("starting insecure (http) webhook on %s:%d", *ip, *port)
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", *ip, *port), n))
}
}
示例9: watchFile
// watchFile assigns a new fsnotify watcher to the file if possible.
// It it watches for any signals that lead to file change, and responds accordingly.
func (t *Tail) watchFile(newFile bool) error {
if t.watcher != nil {
t.watcher.Close()
}
var err error
t.watcher, err = fsnotify.NewWatcher()
if err != nil {
return err
}
err = t.watcher.Add(t.fname)
if err != nil {
return err
}
go func() {
// Start reading at the beginning of the file if new
if newFile {
t.readLines()
}
for {
closed := false
select {
case evt, ok := <-t.watcher.Events:
// Exit if the channel is closed
if !ok {
closed = true
break
}
if evt.Op&(fsnotify.Create|fsnotify.Rename|fsnotify.Remove) != 0 {
if err = t.openAndWatch(); err != nil {
log.Fatalln("[FATA] open and watch failed: ", err)
}
}
if evt.Op&fsnotify.Write == fsnotify.Write {
t.readLines()
}
case err, ok := <-t.watcher.Errors:
// Exit if the channel is closed
if !ok {
closed = true
break
}
if err != nil {
log.Println("[WARN] watcher error: ", err)
}
}
if closed {
break
}
}
}()
return nil
}
示例10: startWatch
func startWatch(watches *services.ServiceWatch, cfg services.OperationConfig) (*fsnotify.Watcher, error) {
fmt.Printf("Watching %v paths for service %v\n", len(watches.IncludedPaths), watches.Service.GetName())
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, errors.WithStack(err)
}
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
fmt.Printf("File edited: %v\n", event.Name)
var wasExcluded bool
for _, excluded := range watches.ExcludedPaths {
if strings.HasPrefix(event.Name, excluded) {
fmt.Println("File is under excluded path:", excluded)
wasExcluded = true
break
}
}
if wasExcluded {
continue
}
fmt.Printf("Rebuilding %v\n", watches.Service.GetName())
err = rebuildService(watches.Service, cfg)
if err != nil {
fmt.Printf("Could not rebuild %v: %v\n", watches.Service.GetName(), err)
}
}
case err := <-watcher.Errors:
if err != nil {
log.Println("error:", err)
}
}
}
}()
for _, dir := range watches.IncludedPaths {
err = watcher.Add(dir)
if err != nil {
watcher.Close()
return nil, errors.WithStack(err)
}
}
return watcher, nil
}
示例11: watchFFDB
func watchFFDB(fxLocation string, lastLogData []byte, errorLog *os.File) {
fxPlaces := fxLocation + "places.sqlite"
fxPlacesDB, err := OpenAndCheckSQLiteDB(fxPlaces)
if err != nil {
SomethingBroke(errorLog, err, "Did not connect to Firefox database")
} else {
Notify(errorLog, "Database "+fxPlaces+" opened.")
}
dbWatcher, err := fsnotify.NewWatcher()
if err != nil {
SomethingBroke(errorLog, err, "White Walkers got to the new Watcher before he could be assigned.")
} else {
Notify(errorLog, fmt.Sprintf("Watcher assigned to %s", fxPlaces))
}
defer dbWatcher.Close()
dbWatcher.Add(fxPlaces)
var lastLog Visit
//This whole section exists for persistence; after logging is initiated, only writes are made, no reads.
//By keeping a record of the last entry, we don't end up with duplicate data being sent after restart.
err2 := json.Unmarshal([]byte(lastLogData), &lastLog)
Notify(errorLog, "Initiating; unmarshaled last log")
if err2 != nil {
SomethingBroke(errorLog, err2)
}
var (
lastDate int
lastURL string
)
lastDate = lastLog.Timestamp
lastURL = lastLog.URL
for {
select {
case event := <-dbWatcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
lastDate, lastURL = catchDBChanges(fxPlacesDB, lastDate, lastURL, errorLog)
}
case err := <-dbWatcher.Errors:
SomethingBroke(errorLog, err, "dbWatcher encountered an error and gave up in disgust")
}
}
}
示例12: NewWatcher
func NewWatcher() (*Watcher, error) {
var w Watcher
var err error
w.watcher, err = fsnotify.NewWatcher()
if err != nil {
return nil, err
}
w.Events = make(chan fsnotify.Event)
w.Errors = make(chan error)
w.quit = make(chan bool)
go w.watch()
return &w, err
}
示例13: MustRegisterWatcher
// MustRegisterWatcher creates a new Watcher and starts listening
// given folders
func MustRegisterWatcher(params *Params) *Watcher {
w := &Watcher{
update: make(chan bool),
rootdir: params.Get("watch"),
}
var err error
w.watcher, err = fsnotify.NewWatcher()
if err != nil {
log.Fatalf("Could not register watcher: %s", err)
}
// add watched paths
w.watchFolders()
return w
}
示例14: FollowFile
func FollowFile(path string, cfg *tf.TfcatConfig) {
if !FileExists(path) {
fmt.Fprintf(os.Stderr, "input file '%s' does not exist.\n", path)
os.Exit(1)
}
watcher, err := fsnotify.NewWatcher()
panicOn(err)
defer watcher.Close()
f, err := os.Open(path)
panicOn(err)
// move to end for tailing
_, err = f.Seek(0, 2)
panicOn(err)
err = watcher.Add(path)
panicOn(err)
fr := tf.NewFrameReader(f, 1024*1024)
var frame tf.Frame
i := int64(1)
nextFrame:
for {
_, _, err, _ = fr.NextFrame(&frame)
if err != nil {
if err == io.EOF {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
continue nextFrame
}
}
}
fmt.Fprintf(os.Stderr, "tfcat error from fr.NextFrame(): '%v'\n", err)
os.Exit(1)
}
frame.DisplayFrame(os.Stdout, i, cfg.PrettyPrint, cfg.SkipPayload, cfg.Rreadable)
i++
}
}
示例15: NewTilesetIndex
// NewTilesetIndex creates a new tileset index, but does not read the tile tilesets from disk
func NewTilesetIndex(mbtilesDir string) (tsi *TilesetIndex, err error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return
}
watcher.Add(mbtilesDir)
cache, err := InitTileCache(filepath.Join(mbtilesDir, CacheName))
if err != nil {
return
}
tsi = &TilesetIndex{
Path: mbtilesDir,
Tilesets: make(map[string]*mbtiles.Tileset),
Events: make(chan TsEvent),
watcher: watcher,
cache: cache,
}
return
}