observer

所属分类:GO语言编程
开发工具:GO
文件大小:0KB
下载次数:0
上传日期:2018-05-22 12:33:47
上 传 者sh-1993
说明:  Golang事件发射器_and_侦听器,具有内置文件观察程序包。
(Golang event emitter _and_ listener with builtin file watcher package.)

文件列表:
.travis.yml (241, 2018-05-22)
Gopkg.toml (655, 2018-05-22)
LICENSE (11357, 2018-05-22)
Makefile (646, 2018-05-22)
examples/ (0, 2018-05-22)
examples/emit-buffered.go (688, 2018-05-22)
examples/emit.go (602, 2018-05-22)
examples/file-watch-pattern.go (536, 2018-05-22)
examples/file-watch.go (611, 2018-05-22)
main.go (3189, 2018-05-22)
observer/ (0, 2018-05-22)
observer/observer.go (7693, 2018-05-22)
observer/observer_test.go (3512, 2018-05-22)
observer/set/ (0, 2018-05-22)
observer/set/set.go (2330, 2018-05-22)
observer/set/set_test.go (1831, 2018-05-22)
observer/watch_event.go (1727, 2018-05-22)

# Observer Go event emitter and listener with builtin file watcher package. [![Go Report Card](https://goreportcard.com/badge/github.com/yaacov/observer)](https://goreportcard.com/report/github.com/yaacov/observer) [![Build Status](https://travis-ci.org/yaacov/observer.svg?branch=master)](https://travis-ci.org/yaacov/observer) [![GoDoc](https://godoc.org/github.com/yaacov/observer?status.svg)](https://godoc.org/github.com/yaacov/observer) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) #### Code snippets ``` go // Create a new observer o := observer.Observer{} o.Open() // Register a listener method. o.AddListener(func(e interface{}) { log.Printf("Received: %v.\n", e) }) // Emit an event. o.Emit("Hello") // Watch and emit events on file modifications. o.Watch([]string{"*.html", "css/*.scss"}) ``` ## Description This observer implements event emitter and listener pattern in go, the observer register a list of listener functions and implement an event emitter, once an event is emitted, all listener functions will be called. This observer also abstracts watching for file changes, users can register a list for files to watch, once a file is watched, events will be emitted automatically on each file modification. Common use cases are watching for changing in config files, and wating for code changes. This observer is using golang [channels](https://gobyexample.com/channels) for emiting events and [fsnotify](https://github.com/fsnotify/fsnotify) for watching file changes. #### Examples: - [Emit string events](#emit-string-events) - [Watch files, emit file cahnge events](#watch-files-emit-file-change-events) ## Develop ``` $ go get -u github.com/golang/dep/cmd/dep $ make vendor $ make $ ./observe ``` ## Install ``` go get -u github.com/yaacov/observer ``` Install using `go get` will install the package and the CLI tool _observer_ ## CLI _observer_ is a cli tool for watching files and executing shell commands on file modification, it is used to call an action on file change, examples of use can be restart and app when config file changes, recompile code when code updates and send image to server when image change. [inotify-tools](https://github.com/rvoicilas/inotify-tools/wiki), are a set of linux tools that monitor files for changes, they may be better choice for file monitoring on more complex cases. #### Get help: ``` sh observer -h ``` #### Call a server api when config file chage: ``` sh observer -r "curl -X POST http://127.0.0.1:8000/api/v1/-/restart" -w "/root/.aws/config" ``` ## API See [examples](#examples-1) for usage examples. | Method | Description | |--------------------------------|-----------------------------------| | Open() | Open the observer channels | | Close() | Close the observer channels | | AddListener(callback Listener) | Add a listener function to run on event | | Emit(event interface{}) | Emit event | | Watch(files []string) | Watch for file changes, and emit a file change events | | SetBufferDuration(d time.Duration) | Set the event buffer damping duration | | Type | | Description | |--------------------------------|-----------------------------------|-------------| | WatchEvent | struct{ Name string, Op uint32 } | Event type emitted by file watcher | | Listener | func(interface{}) | Function type for listeners | | Observer | struct{ Verbose bool } | The observer object | ## Watching files for modifications Watching files can be done using exact file name, or shell pattern matching. #### Watching for exact file names: ``` go Watch([]string{"./aws/config", "./aws/credentials"}) ``` #### Watching files using shell pattern matching: ``` go Watch([]string{"./kube/*.yml"}) ``` #### Note: We can not expand tilde to home directory, `~/.config` will not work as expected. If needed users can use golang's [os/user/](https://golang.org/pkg/os/user/) package. ## Examples ### Emit string events Example of event listener and emitter. [emit.go](/examples/emit.go) ``` go // Open an observer and start running o := observer.Observer{} o.Open() defer o.Close() // Add a listener that logs events o.AddListener(func(e interface{}) { log.Printf("Received: %s.\n", e.(string)) }) // This event will be picked by the listener o.Emit("Hello") ``` ### Watch files, emit file change events Example of file watching and listener. [file-watch.go](/examples/file-watch.go) ``` go // Open an observer and start watching for files by file name o := observer.Observer{} o.Watch([]string{"../LICENSE"}) defer o.Close() // Add a listener that logs events o.AddListener(func(e interface{}) { watchEvent := e.(observer.WatchEvent) log.Printf("File modified: %s.\n", e.Name) }) ``` [file-watch-pattern.go](/examples/file-watch-pattern.go) ``` go // Open an observer and start watching for file matching shell pattern o := observer.Observer{} o.Watch([]string{"*.html", "css/*.scss"}) defer o.Close() // Add a listener that logs events o.AddListener(func(e interface{}) { log.Printf("File modified: %v.\n", e) }) ``` ### Group events by time, event groups will be sent once as an array of events. [emit-buffered.go](/examples/emit-buffered.go) ``` go o.Open() o.SetBufferDuration(1 * time.Second) o.AddListener(func(e interface{}) { output = e.([]interface{}) // => ["done", "done", "done", "done"] }) // All this events will be grouped together, and sent only once to the listener. o.Emit("done") o.Emit("done") o.Emit("done") o.Emit("done") ```

近期下载者

相关文件


收藏者