Skip to content
/ gowatch Public

Turn file system events into text stream for processing

Notifications You must be signed in to change notification settings

hayeah/gowatch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Turn file system events into JSON stream.

(Does not support recursive directory watching.)

Install

go get github.com/hayeah/gowatch

Usage

gowatch dir ...

It would ouput a JSON stream that you can pipe into another program for filtering & processing. Supposed we are watching a directory called test, you get output like:

> ./gowatch test
{"event":"chmod","path":"test/foobar"}
{"event":"remove","path":"test/foobar"}
{"event":"create","path":"test/foobar"}
{"event":"chmod","path":"test/foobar"}

Slice and dice as you please.

Example

It is convenient to combine gowatch with a JSON processing tool like jq.

Suppose we'd like to execute a command whenenver a new file is added, we can build a pipeline:

> ./gowatch test | unbuffer -p jq -M 'select(.event == "create")' | gogo 'echo process new file {{.path}}'
2014/11/29 11:35:04 watching: test
2014/11/29 11:35:06 run cmd: echo process new file test/foo
process new file test/foo
2014/11/29 11:35:06 run cmd: echo process new file test/bar
process new file test/bar
  • unbuffer - This disables buffering so jq sends output down the pipe as soon as possible.
    • -p is used in a pipeline to read input from upstream.
  • jq - It selects only the create events.
    • -M disables output color. This prevents JSON parser downstream from choking.
  • gogo - A utility to run processes for each object in a JSON stream. See: hayeah/gogo

File System Events Supported

See: fsnotify.Op

const (
    Create Op  = 1 << iota
    Write
    Remove
    Rename
    Chmod
)

About

Turn file system events into text stream for processing

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages