1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
Sergio Rubio d2a3e504cf Vendor dependencies using golang/dep
Output from 'dep status':

PROJECT                               CONSTRAINT     VERSION        REVISION  LATEST   PKGS USED
cloud.google.com/go                   v0.23.0        v0.23.0        0fd7230   v0.23.0  1
github.com/briandowns/openweathermap  ^0.11.0        0.11           1b87579   0.11     1
github.com/gdamore/encoding           branch master  branch master  b23993c   b23993c  1
github.com/gdamore/tcell              ^1.0.0         v1.0.0         061d51a   v1.0.0   2
github.com/go-test/deep               ^1.0.1         v1.0.1         6592d9c   v1.0.1   1
github.com/golang/protobuf            v1.1.0         v1.1.0         b4deda0   v1.1.0   1
github.com/google/go-github           branch master  branch master  2ae5df7   2ae5df7  1
github.com/google/go-querystring      branch master  branch master  53e6ce1   53e6ce1  1
github.com/jessevdk/go-flags          ^1.4.0         v1.4.0         c6ca198   v1.4.0   1
github.com/lucasb-eyer/go-colorful    v1.0           v1.0           345fbb3   v1.0     1
github.com/mattn/go-runewidth         v0.0.2         v0.0.2         9e777a8   v0.0.2   1
github.com/olebedev/config            branch master  branch master  9a10d05   9a10d05  1
github.com/radovskyb/watcher          ^1.0.2         v1.0.2         6145e14   v1.0.2   1
github.com/rivo/tview                 branch master  branch master  71ecf1f   71ecf1f  1
github.com/yfronto/newrelic           branch master  branch master  f7fa0c6   f7fa0c6  1
golang.org/x/net                      branch master  branch master  1e49130   1e49130  2
golang.org/x/oauth2                   branch master  branch master  1e0a3fa   1e0a3fa  5
golang.org/x/text                     v0.3.0         v0.3.0         f21a4df   v0.3.0   5
google.golang.org/api                 branch master  branch master  00e3bb8   00e3bb8  4
google.golang.org/appengine           v1.0.0         v1.0.0         150dc57   v1.0.0   10
gopkg.in/yaml.v2                      ^2.2.1         v2.2.1         5420a8b   v2.2.1   1

See https://golang.github.io/dep/docs/daily-dep.html
2018-06-06 18:29:46 +02:00

4.5 KiB

watcher

Build Status

watcher is a Go package for watching for files or directory changes (recursively or non recursively) without using filesystem events, which allows it to work cross platform consistently.

watcher watches for changes and notifies over channels either anytime an event or an error has occurred.

Events contain the os.FileInfo of the file or directory that the event is based on and the type of event and file or directory path.

Installation
Features
Example
Contributing
Watcher Command

Update

Event.Path for Rename and Move events is now returned in the format of fromPath -> toPath

Chmod event is not supported under windows.

Installation

go get -u github.com/radovskyb/watcher/...

Features

  • Customizable polling interval.
  • Filter Events.
  • Watch folders recursively or non-recursively.
  • Choose to ignore hidden files.
  • Choose to ignore specified files and folders.
  • Notifies the os.FileInfo of the file that the event is based on. e.g Name, ModTime, IsDir, etc.
  • Notifies the full path of the file that the event is based on or the old and new paths if the event was a Rename or Move event.
  • Limit amount of events that can be received per watching cycle.
  • List the files being watched.
  • Trigger custom events.

Todo

  • Write more tests.
  • Write benchmarks.

Example

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/radovskyb/watcher"
)

func main() {
	w := watcher.New()

	// SetMaxEvents to 1 to allow at most 1 event's to be received
	// on the Event channel per watching cycle.
	//
	// If SetMaxEvents is not set, the default is to send all events.
	w.SetMaxEvents(1)

	// Only notify rename and move events.
	w.FilterOps(watcher.Rename, watcher.Move)

	go func() {
		for {
			select {
			case event := <-w.Event:	
				fmt.Println(event) // Print the event's info.
			case err := <-w.Error:
				log.Fatalln(err)
			case <-w.Closed:
				return
			}
		}
	}()

	// Watch this folder for changes.
	if err := w.Add("."); err != nil {
		log.Fatalln(err)
	}

	// Watch test_folder recursively for changes.
	if err := w.AddRecursive("../test_folder"); err != nil {
		log.Fatalln(err)
	}

	// Print a list of all of the files and folders currently
	// being watched and their paths.
	for path, f := range w.WatchedFiles() {
		fmt.Printf("%s: %s\n", path, f.Name())
	}

	fmt.Println()

	// Trigger 2 events after watcher started.
	go func() {
		w.Wait()
		w.TriggerEvent(watcher.Create, nil)
		w.TriggerEvent(watcher.Remove, nil)
	}()

	// Start the watching process - it'll check for changes every 100ms.
	if err := w.Start(time.Millisecond * 100); err != nil {
		log.Fatalln(err)
	}
}

Contributing

If you would ike to contribute, simply submit a pull request.

Command

watcher comes with a simple command which is installed when using the go get command from above.

Usage

Usage of watcher:
  -cmd string
    	command to run when an event occurs
  -dotfiles
    	watch dot files (default true)
  -interval string
    	watcher poll interval (default "100ms")
  -keepalive
    	keep alive when a cmd returns code != 0
  -list
    	list watched files on start
  -pipe
    	pipe event's info to command's stdin
  -recursive
    	watch folders recursively (default true)
  -startcmd
    	run the command when watcher starts

All of the flags are optional and watcher can also be called by itself:

watcher

(watches the current directory recursively for changes and notifies any events that occur.)

A more elaborate example using the watcher command:

watcher -dotfiles=false -recursive=false -cmd="./myscript" main.go ../

In this example, watcher will ignore dot files and folders and won't watch any of the specified folders recursively. It will also run the script ./myscript anytime an event occurs while watching main.go or any files or folders in the previous directory (../).

Using the pipe and cmd flags together will send the event's info to the command's stdin when changes are detected.

First create a file called script.py with the following contents:

import sys

for line in sys.stdin:
	print (line + " - python")

Next, start watcher with the pipe and cmd flags enabled:

watcher -cmd="python script.py" -pipe=true

Now when changes are detected, the event's info will be output from the running python script.