~samwhited/blogsync

blogsync/watch.go -rw-r--r-- 1.1 KiB
5083f77dSam Whited .builds: verify DCO 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2019 The Blog Sync Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package main

import (
	"log"
	"os"
	"path/filepath"

	"github.com/fsnotify/fsnotify"
)

func newWatcher(content string, debug *log.Logger) (watcher *fsnotify.Watcher, err error) {
	watcher, err = fsnotify.NewWatcher()
	if err != nil {
		return nil, err
	}
	// Handle errors by cleaning up so that the caller can follow Go idioms of
	// checking errors before handling the value (eg. in case an error happens
	// while adding files to the already existing watcher).
	defer func() {
		if err != nil {
			if err := watcher.Close(); err != nil {
				debug.Printf("error closing unused %s watcher: %v", content, err)
			}
		}
	}()

	err = filepath.Walk(content, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			debug.Printf("error watching file %s, changes will not trigger a rebuilt: %v", path, err)
			return nil
		}

		if !info.IsDir() {
			// Watch entire directory trees for changes, not individual files.
			return nil
		}

		return watcher.Add(path)
	})

	return watcher, err
}