Skip to content

location-txl/file_watch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dir_watch

一个用于监听目录变更的命令行工具,适合被其他软件作为子进程启动,并通过读取 stdout 的 JSON Lines 来消费文件事件流。

安装 / 构建

go build -o dir_watch .

开发:提交前自动格式化(git hook)

本仓库提供可版本化的 pre-commit hook:在 git commit 前对已暂存的 Go 文件执行格式化(使用 gofmt),并自动重新暂存,避免污染未暂存改动。

安装方式(仓库内执行一次即可):

./scripts/install-githooks.sh

等价于:

git config core.hooksPath .githooks
chmod +x .githooks/pre-commit

用法

./dir_watch -debounce 200ms /path/to/dir

输出写到 stdout(每行一个 JSON),错误写到 stderr。

输出格式(JSONL)

每一行是一个事件对象:

{"v":1,"ts":"2026-01-12T00:00:00Z","op":"write","path":"/abs/path/file.txt","root":"/abs/path","is_dir":false}

字段说明:

  • v:协议版本(当前固定为 1
  • ts:时间戳(RFC3339Nano,UTC)
  • op:事件类型(create / write / remove / rename / chmod
  • path:事件路径(绝对路径)
  • root:匹配到的监听根目录(绝对路径,可能为空)
  • is_dir:是否目录(仅在可判断时输出)

去抖/合并行为

本工具会按 -debounce 指定的窗口进行合并:

  • 同一窗口内同一路径会被合并成一条(按优先级选择更“强”的事件)
  • 优先级:remove > rename > create > write > chmod

递归与符号链接

  • 默认递归监听(-recursive=true
  • 默认跟随符号链接目录做递归遍历(-follow-symlinks=true),并自动做循环检测以避免无限递归

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published