2018-06-22 07:01:36 -05:00
|
|
|
package gaper
|
2018-06-16 19:22:21 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-06-18 21:47:20 -05:00
|
|
|
"fmt"
|
2018-06-16 19:22:21 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-06-22 19:44:26 -05:00
|
|
|
"strings"
|
2018-06-16 19:22:21 -05:00
|
|
|
"time"
|
2018-06-18 21:47:20 -05:00
|
|
|
|
|
|
|
zglob "github.com/mattn/go-zglob"
|
2018-06-16 19:22:21 -05:00
|
|
|
)
|
|
|
|
|
2018-06-20 21:04:16 -05:00
|
|
|
// Watcher is a interface for the watch process
|
2018-06-16 19:22:21 -05:00
|
|
|
type Watcher struct {
|
2018-06-18 21:21:43 -05:00
|
|
|
PollInterval int
|
2018-06-22 19:44:26 -05:00
|
|
|
WatchItems map[string]bool
|
|
|
|
IgnoreItems map[string]bool
|
2018-06-16 19:22:21 -05:00
|
|
|
AllowedExtensions map[string]bool
|
|
|
|
Events chan string
|
|
|
|
Errors chan error
|
|
|
|
}
|
|
|
|
|
2018-06-20 21:04:16 -05:00
|
|
|
// NewWatcher creates a new watcher
|
2018-06-18 21:47:20 -05:00
|
|
|
func NewWatcher(pollInterval int, watchItems []string, ignoreItems []string, extensions []string) (*Watcher, error) {
|
2018-06-20 20:40:09 -05:00
|
|
|
if pollInterval == 0 {
|
|
|
|
pollInterval = DefaultPoolInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(extensions) == 0 {
|
|
|
|
extensions = DefaultExtensions
|
|
|
|
}
|
|
|
|
|
2018-06-16 19:22:21 -05:00
|
|
|
allowedExts := make(map[string]bool)
|
|
|
|
for _, ext := range extensions {
|
|
|
|
allowedExts["."+ext] = true
|
|
|
|
}
|
|
|
|
|
2018-06-22 19:44:26 -05:00
|
|
|
watchPaths, err := resolvePaths(watchItems, allowedExts)
|
2018-06-18 21:47:20 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-06-22 19:44:26 -05:00
|
|
|
ignorePaths, err := resolvePaths(ignoreItems, allowedExts)
|
2018-06-18 21:47:20 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-06-22 19:44:26 -05:00
|
|
|
logger.Debugf("Resolved watch paths: %v", watchPaths)
|
|
|
|
logger.Debugf("Resolved ignore paths: %v", ignorePaths)
|
2018-06-16 19:22:21 -05:00
|
|
|
return &Watcher{
|
|
|
|
Events: make(chan string),
|
|
|
|
Errors: make(chan error),
|
2018-06-18 21:21:43 -05:00
|
|
|
PollInterval: pollInterval,
|
2018-06-22 19:44:26 -05:00
|
|
|
WatchItems: watchPaths,
|
|
|
|
IgnoreItems: ignorePaths,
|
2018-06-16 19:22:21 -05:00
|
|
|
AllowedExtensions: allowedExts,
|
2018-06-18 21:47:20 -05:00
|
|
|
}, nil
|
2018-06-16 19:22:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var startTime = time.Now()
|
|
|
|
var errDetectedChange = errors.New("done")
|
|
|
|
|
2018-06-20 21:04:16 -05:00
|
|
|
// Watch starts watching for file changes
|
2018-06-18 21:47:20 -05:00
|
|
|
func (w *Watcher) Watch() {
|
2018-06-16 19:22:21 -05:00
|
|
|
for {
|
2018-06-22 19:44:26 -05:00
|
|
|
for watchPath := range w.WatchItems {
|
|
|
|
fileChanged, err := w.scanChange(watchPath)
|
2018-06-18 21:22:18 -05:00
|
|
|
if err != nil {
|
|
|
|
w.Errors <- err
|
|
|
|
return
|
2018-06-16 19:22:21 -05:00
|
|
|
}
|
|
|
|
|
2018-06-18 21:22:18 -05:00
|
|
|
if fileChanged != "" {
|
|
|
|
w.Events <- fileChanged
|
|
|
|
startTime = time.Now()
|
2018-06-16 19:22:21 -05:00
|
|
|
}
|
2018-06-18 21:22:18 -05:00
|
|
|
}
|
2018-06-16 19:22:21 -05:00
|
|
|
|
2018-06-18 21:21:43 -05:00
|
|
|
time.Sleep(time.Duration(w.PollInterval) * time.Millisecond)
|
2018-06-18 21:22:18 -05:00
|
|
|
}
|
|
|
|
}
|
2018-06-16 19:22:21 -05:00
|
|
|
|
2018-06-18 21:22:18 -05:00
|
|
|
func (w *Watcher) scanChange(watchPath string) (string, error) {
|
|
|
|
logger.Debug("Watching ", watchPath)
|
|
|
|
|
|
|
|
var fileChanged string
|
|
|
|
|
|
|
|
err := filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error {
|
2018-06-22 19:44:26 -05:00
|
|
|
// always ignore hidden files and directories
|
2018-06-24 18:49:28 -05:00
|
|
|
if dir := filepath.Base(path); dir[0] == '.' && dir != "." {
|
|
|
|
return skipFile(info)
|
2018-06-18 21:22:18 -05:00
|
|
|
}
|
|
|
|
|
2018-06-22 19:44:26 -05:00
|
|
|
if _, ignored := w.IgnoreItems[path]; ignored {
|
2018-06-24 18:49:28 -05:00
|
|
|
return skipFile(info)
|
2018-06-18 21:22:18 -05:00
|
|
|
}
|
2018-06-16 19:22:21 -05:00
|
|
|
|
2018-06-18 21:22:18 -05:00
|
|
|
ext := filepath.Ext(path)
|
|
|
|
if _, ok := w.AllowedExtensions[ext]; ok && info.ModTime().After(startTime) {
|
|
|
|
fileChanged = path
|
|
|
|
return errDetectedChange
|
2018-06-16 19:22:21 -05:00
|
|
|
}
|
|
|
|
|
2018-06-18 21:22:18 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil && err != errDetectedChange {
|
|
|
|
return "", err
|
2018-06-16 19:22:21 -05:00
|
|
|
}
|
2018-06-18 21:22:18 -05:00
|
|
|
|
|
|
|
return fileChanged, nil
|
2018-06-16 19:22:21 -05:00
|
|
|
}
|
2018-06-18 21:47:20 -05:00
|
|
|
|
2018-06-22 19:44:26 -05:00
|
|
|
func resolvePaths(paths []string, extensions map[string]bool) (map[string]bool, error) {
|
|
|
|
result := map[string]bool{}
|
2018-06-18 21:47:20 -05:00
|
|
|
|
|
|
|
for _, path := range paths {
|
2018-06-22 19:44:26 -05:00
|
|
|
matches := []string{path}
|
|
|
|
|
|
|
|
isGlob := strings.Contains(path, "*")
|
|
|
|
if isGlob {
|
|
|
|
var err error
|
|
|
|
matches, err = zglob.Glob(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't resolve glob path \"%s\": %v", path, err)
|
|
|
|
}
|
2018-06-18 21:47:20 -05:00
|
|
|
}
|
|
|
|
|
2018-06-22 19:44:26 -05:00
|
|
|
for _, match := range matches {
|
|
|
|
// don't care for extension filter right now for non glob paths
|
|
|
|
// since they could be a directory
|
|
|
|
if isGlob {
|
|
|
|
if _, ok := extensions[filepath.Ext(path)]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := result[match]; !ok {
|
|
|
|
result[match] = true
|
|
|
|
}
|
|
|
|
}
|
2018-06-18 21:47:20 -05:00
|
|
|
}
|
|
|
|
|
2018-06-22 19:44:26 -05:00
|
|
|
removeOverlappedPaths(result)
|
|
|
|
|
2018-06-18 21:47:20 -05:00
|
|
|
return result, nil
|
|
|
|
}
|
2018-06-22 19:44:26 -05:00
|
|
|
|
|
|
|
// remove overlapped paths so it makes the scan for changes later faster and simpler
|
|
|
|
func removeOverlappedPaths(mapPaths map[string]bool) {
|
|
|
|
for p1 := range mapPaths {
|
2018-06-24 18:48:34 -05:00
|
|
|
// skip to next item if this path has already been checked
|
|
|
|
if v, ok := mapPaths[p1]; ok && !v {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-06-22 19:44:26 -05:00
|
|
|
for p2 := range mapPaths {
|
|
|
|
if p1 == p2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(p2, p1) {
|
|
|
|
mapPaths[p2] = false
|
|
|
|
} else if strings.HasPrefix(p1, p2) {
|
|
|
|
mapPaths[p1] = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup path list
|
|
|
|
for p := range mapPaths {
|
|
|
|
if !mapPaths[p] {
|
|
|
|
delete(mapPaths, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 18:49:28 -05:00
|
|
|
|
|
|
|
func skipFile(info os.FileInfo) error {
|
|
|
|
if info.IsDir() {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|