1
0
mirror of https://github.com/taigrr/wtf synced 2026-04-02 02:28:55 -07:00

Update dependencies to latest versions

This commit is contained in:
Chris Cummer
2019-01-11 16:44:42 -08:00
parent ea27f40164
commit 48cb7ba773
358 changed files with 29553 additions and 8982 deletions

View File

@@ -15,7 +15,8 @@ Events contain the `os.FileInfo` of the file or directory that the event is base
[Watcher Command](#command)
# Update
Event.Path for Rename and Move events is now returned in the format of `fromPath -> toPath`
- Added new file filter hooks (Including a built in regexp filtering hook) [Dec 12, 2018]
- Event.Path for Rename and Move events is now returned in the format of `fromPath -> toPath`
#### Chmod event is not supported under windows.
@@ -68,6 +69,11 @@ func main() {
// Only notify rename and move events.
w.FilterOps(watcher.Rename, watcher.Move)
// Only files that match the regular expression during file listings
// will be watched.
r := regexp.MustCompile("^abc$")
w.AddFilterHook(watcher.RegexFilterHook(r, false))
go func() {
for {
select {
@@ -128,6 +134,8 @@ Usage of watcher:
command to run when an event occurs
-dotfiles
watch dot files (default true)
-ignore string
comma separated list of paths to ignore
-interval string
watcher poll interval (default "100ms")
-keepalive

12
vendor/github.com/radovskyb/watcher/ishidden.go generated vendored Normal file
View File

@@ -0,0 +1,12 @@
// +build !windows
package watcher
import (
"path/filepath"
"strings"
)
func isHiddenFile(path string) (bool, error) {
return strings.HasPrefix(filepath.Base(path), "."), nil
}

View File

@@ -0,0 +1,21 @@
// +build windows
package watcher
import (
"syscall"
)
func isHiddenFile(path string) (bool, error) {
pointer, err := syscall.UTF16PtrFromString(path)
if err != nil {
return false, err
}
attributes, err := syscall.GetFileAttributes(pointer)
if err != nil {
return false, err
}
return attributes&syscall.FILE_ATTRIBUTE_HIDDEN != 0, nil
}

View File

@@ -4,6 +4,6 @@ package watcher
import "os"
func SameFile(fi1, fi2 os.FileInfo) bool {
func sameFile(fi1, fi2 os.FileInfo) bool {
return os.SameFile(fi1, fi2)
}

View File

@@ -4,7 +4,7 @@ package watcher
import "os"
func SameFile(fi1, fi2 os.FileInfo) bool {
func sameFile(fi1, fi2 os.FileInfo) bool {
return fi1.ModTime() == fi2.ModTime() &&
fi1.Size() == fi2.Size() &&
fi1.Mode() == fi2.Mode() &&

View File

@@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
@@ -24,6 +25,10 @@ var (
// ErrWatchedFileDeleted is an error that occurs when a file or folder that was
// being watched has been deleted.
ErrWatchedFileDeleted = errors.New("error: watched file or folder deleted")
// ErrSkip is less of an error, but more of a way for path hooks to skip a file or
// directory.
ErrSkip = errors.New("error: skipping file")
)
// An Op is a type that is used to describe what type
@@ -69,16 +74,43 @@ type Event struct {
// String returns a string depending on what type of event occurred and the
// file name associated with the event.
func (e Event) String() string {
if e.FileInfo != nil {
pathType := "FILE"
if e.IsDir() {
pathType = "DIRECTORY"
}
return fmt.Sprintf("%s %q %s [%s]", pathType, e.Name(), e.Op, e.Path)
if e.FileInfo == nil {
return "???"
}
return "???"
pathType := "FILE"
if e.IsDir() {
pathType = "DIRECTORY"
}
return fmt.Sprintf("%s %q %s [%s]", pathType, e.Name(), e.Op, e.Path)
}
// FilterFileHookFunc is a function that is called to filter files during listings.
// If a file is ok to be listed, nil is returned otherwise ErrSkip is returned.
type FilterFileHookFunc func(info os.FileInfo, fullPath string) error
// RegexFilterHook is a function that accepts or rejects a file
// for listing based on whether it's filename or full path matches
// a regular expression.
func RegexFilterHook(r *regexp.Regexp, useFullPath bool) FilterFileHookFunc {
return func(info os.FileInfo, fullPath string) error {
str := info.Name()
if useFullPath {
str = fullPath
}
// Match
if r.MatchString(str) {
return nil
}
// No match.
return ErrSkip
}
}
// Watcher describes a process that watches files for changes.
type Watcher struct {
Event chan Event
Error chan error
@@ -88,6 +120,7 @@ type Watcher struct {
// mu protects the following.
mu *sync.Mutex
ffh []FilterFileHookFunc
running bool
names map[string]bool // bool for recursive or not.
files map[string]os.FileInfo // map of files.
@@ -125,6 +158,13 @@ func (w *Watcher) SetMaxEvents(delta int) {
w.mu.Unlock()
}
// AddFilterHook
func (w *Watcher) AddFilterHook(f FilterFileHookFunc) {
w.mu.Lock()
w.ffh = append(w.ffh, f)
w.mu.Unlock()
}
// IgnoreHiddenFiles sets the watcher to ignore any file or directory
// that starts with a dot.
func (w *Watcher) IgnoreHiddenFiles(ignore bool) {
@@ -157,7 +197,13 @@ func (w *Watcher) Add(name string) (err error) {
// If name is on the ignored list or if hidden files are
// ignored and name is a hidden file or directory, simply return.
_, ignored := w.ignored[name]
if ignored || (w.ignoreHidden && strings.HasPrefix(name, ".")) {
isHidden, err := isHiddenFile(name)
if err != nil {
return err
}
if ignored || (w.ignoreHidden && isHidden) {
return nil
}
@@ -200,18 +246,36 @@ func (w *Watcher) list(name string) (map[string]os.FileInfo, error) {
// Add all of the files in the directory to the file list as long
// as they aren't on the ignored list or are hidden files if ignoreHidden
// is set to true.
outer:
for _, fInfo := range fInfoList {
path := filepath.Join(name, fInfo.Name())
_, ignored := w.ignored[path]
if ignored || (w.ignoreHidden && strings.HasPrefix(fInfo.Name(), ".")) {
isHidden, err := isHiddenFile(path)
if err != nil {
return nil, err
}
if ignored || (w.ignoreHidden && isHidden) {
continue
}
for _, f := range w.ffh {
err := f(fInfo, path)
if err == ErrSkip {
continue outer
}
if err != nil {
return nil, err
}
}
fileList[path] = fInfo
}
return fileList, nil
}
// Add adds either a single file or directory recursively to the file list.
// AddRecursive adds either a single file or directory recursively to the file list.
func (w *Watcher) AddRecursive(name string) (err error) {
w.mu.Lock()
defer w.mu.Unlock()
@@ -242,10 +306,27 @@ func (w *Watcher) listRecursive(name string) (map[string]os.FileInfo, error) {
if err != nil {
return err
}
for _, f := range w.ffh {
err := f(info, path)
if err == ErrSkip {
return nil
}
if err != nil {
return err
}
}
// If path is ignored and it's a directory, skip the directory. If it's
// ignored and it's a single file, skip the file.
_, ignored := w.ignored[path]
if ignored || (w.ignoreHidden && strings.HasPrefix(info.Name(), ".")) {
isHidden, err := isHiddenFile(path)
if err != nil {
return err
}
if ignored || (w.ignoreHidden && isHidden) {
if info.IsDir() {
return filepath.SkipDir
}
@@ -292,7 +373,7 @@ func (w *Watcher) Remove(name string) (err error) {
return nil
}
// Remove removes either a single file or a directory recursively from
// RemoveRecursive removes either a single file or a directory recursively from
// the file's list.
func (w *Watcher) RemoveRecursive(name string) (err error) {
w.mu.Lock()
@@ -346,11 +427,17 @@ func (w *Watcher) Ignore(paths ...string) (err error) {
return nil
}
// WatchedFiles returns a map of files added to a Watcher.
func (w *Watcher) WatchedFiles() map[string]os.FileInfo {
w.mu.Lock()
defer w.mu.Unlock()
return w.files
files := make(map[string]os.FileInfo)
for k, v := range w.files {
files[k] = v
}
return files
}
// fileInfo is an implementation of os.FileInfo that can be used
@@ -560,7 +647,7 @@ func (w *Watcher) pollEvents(files map[string]os.FileInfo, evt chan Event,
// Check for renames and moves.
for path1, info1 := range removes {
for path2, info2 := range creates {
if SameFile(info1, info2) {
if sameFile(info1, info2) {
e := Event{
Op: Move,
Path: fmt.Sprintf("%s -> %s", path1, path2),
@@ -606,6 +693,7 @@ func (w *Watcher) Wait() {
w.wg.Wait()
}
// Close stops a Watcher and unlocks its mutex, then sends a close signal.
func (w *Watcher) Close() {
w.mu.Lock()
if !w.running {