Compare commits
50 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
b8c709ac4c | ||
|
4d5e13daf5 | ||
|
46df7a206a | ||
|
04c46435d4 | ||
|
aa23628137 | ||
|
86ceb19acd | ||
|
a3f5af69ae | ||
|
4f044b8ebf | ||
|
b5f068dd55 | ||
|
f912ccb753 | ||
|
7dd5b44ab6 | ||
|
679ddba2d8 | ||
|
e4d9b74869 | ||
|
c61fac453d | ||
|
f4859b37d8 | ||
|
06fb9f6a97 | ||
|
3e47cd28ba | ||
|
753ff72f85 | ||
|
aa929ae838 | ||
|
a0af500678 | ||
|
3964324f43 | ||
|
3bc323154f | ||
|
08d421ebcb | ||
|
134f6e45f5 | ||
|
8552d33dcd | ||
|
58a3b27da9 | ||
|
7022e8bb04 | ||
|
a69cbeea2d | ||
|
c9aedb2d34 | ||
|
d2be0654e2 | ||
|
b6be235c6e | ||
|
30312d4b9c | ||
|
281a4f4d64 | ||
|
8181a8f0c5 | ||
|
9e73a0901c | ||
|
789caba260 | ||
|
86923ffdab | ||
|
72807c78a9 | ||
|
1f4d27491b | ||
|
758bd1e409 | ||
|
5c31d76fac | ||
|
bee37e29e6 | ||
|
5f43051ae8 | ||
|
9004aceb91 | ||
|
8d5c11d20d | ||
|
2eb994561d | ||
|
c8d8cfff72 | ||
|
0cd4beac62 | ||
|
4e9eecd0cc | ||
|
042038a60e |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -5,4 +5,4 @@ filegirl.yaml
|
||||
fileboy-darwin-amd64.bin
|
||||
fileboy-linux-amd64.bin
|
||||
fileboy-windows-amd64.exe
|
||||
|
||||
./bin/*
|
||||
|
12
.gitpod.Dockerfile
vendored
Normal file
12
.gitpod.Dockerfile
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
FROM gitpod/workspace-full
|
||||
|
||||
USER root
|
||||
|
||||
# Install custom tools, runtime, etc. using apt-get
|
||||
# For example, the command below would install "bastet" - a command line tetris clone:
|
||||
#
|
||||
# RUN apt-get update \
|
||||
# && apt-get install -y bastet \
|
||||
# && apt-get clean && rm -rf /var/cache/apt/* && rm -rf /var/lib/apt/lists/* && rm -rf /tmp/*
|
||||
#
|
||||
# More information: https://www.gitpod.io/docs/42_config_docker/
|
7
.gitpod.yml
Normal file
7
.gitpod.yml
Normal file
@@ -0,0 +1,7 @@
|
||||
image:
|
||||
file: .gitpod.Dockerfile
|
||||
|
||||
# List the start up tasks. You can start them in parallel in multiple terminals. See https://www.gitpod.io/docs/44_config_start_tasks/
|
||||
tasks:
|
||||
- init: go get -d -v ./...
|
||||
command: go build
|
21
.travis.yml
Normal file
21
.travis.yml
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.13.x
|
||||
- master
|
||||
|
||||
|
||||
dist: bionic # ubuntu 18.04
|
||||
|
||||
|
||||
script:
|
||||
- make init
|
||||
- make build-linux
|
||||
- ls bin
|
||||
- ./bin/fileboy-linux-amd64.bin version
|
||||
- ./bin/fileboy-linux-amd64.bin help
|
||||
- ./bin/fileboy-linux-amd64.bin init
|
||||
- cat filegirl.yaml
|
||||
- ./bin/fileboy-linux-amd64.bin exec
|
||||
|
50
CHANGELOG.md
50
CHANGELOG.md
@@ -1,3 +1,53 @@
|
||||
### Release v1.12
|
||||
|
||||
2019.12.18
|
||||
|
||||
- 增加 自定义监控事件(write/rename/remove/create/chmod)
|
||||
- 增加 {{event}} 事件名占位符 / event 网络通知字段
|
||||
- 增加 文件(夹)变更动态添加/删除监听(beta)
|
||||
- 优化 init 命令,如果已有`filegirl.yaml`现在提示错误,不会自动覆盖
|
||||
- 优化 日志输出缓冲,可以通过 >> 将fileboy自身输出日志重定向到文件
|
||||
- 修复 始终默认监听主目录的问题
|
||||
- 升级 底层依赖
|
||||
- PR Makefile 的支持 @jason-gao
|
||||
|
||||
|
||||
### Release v1.10
|
||||
|
||||
2019.06.04
|
||||
|
||||
- 优化 log
|
||||
- 优化 代码逻辑
|
||||
- 修复 gin框架hotReload
|
||||
|
||||
|
||||
### Release v1.9
|
||||
|
||||
2019.04.03
|
||||
|
||||
- 优化 文件夹监听效率,减少大量深层文件夹遍历的时间
|
||||
- 优化 代码逻辑
|
||||
- 增加 readme 英文说明
|
||||
- 修复 偶现监听项目主目录无效的问题
|
||||
|
||||
|
||||
### Release v1.8
|
||||
|
||||
2019.02.27
|
||||
|
||||
- 使用 go1.12 编译
|
||||
|
||||
|
||||
|
||||
### Release v1.7
|
||||
|
||||
2019.01.24
|
||||
|
||||
- 修复 time 内存
|
||||
- 修复 某些情况下cmd异常导致进程挂掉的问题
|
||||
|
||||
|
||||
|
||||
### Release v1.6
|
||||
|
||||
2019.01.19
|
||||
|
41
Makefile
Normal file
41
Makefile
Normal file
@@ -0,0 +1,41 @@
|
||||
# notice
|
||||
# Make is very picky about spaces vs. tabs.
|
||||
# Command lines absolutely must be indented with a single tab, and not spaces.
|
||||
# You may need to adjust your editor to generate tab characters.
|
||||
# http://blog.chinaunix.net/uid/28458801/sid-171170-list-1.html
|
||||
|
||||
# use:
|
||||
# make build-mac 编译
|
||||
# make start-mac 启动
|
||||
|
||||
# make build-start-mac 编译+启动
|
||||
init:
|
||||
go get -u gopkg.in/yaml.v2
|
||||
go get -u gopkg.in/fsnotify/fsnotify.v1
|
||||
|
||||
build-mac: ;@echo "编译-mac版";
|
||||
CGO_ENABLED=0 GOARCH=amd64 GOOS=darwin go build -ldflags "-s -w" -o ./bin/fileboy-darwin-amd64.bin
|
||||
|
||||
build-linux: ;@echo "编译-linux版";
|
||||
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags "-s -w" -o ./bin/fileboy-linux-amd64.bin
|
||||
|
||||
build-win: ;@echo "编译-windows版";
|
||||
CGO_ENABLED=0 GOARCH=amd64 GOOS=windows go build -ldflags "-s -w" -o ./bin/fileboy-windows-amd64.exe
|
||||
|
||||
build-all: build-mac build-linux build-win
|
||||
|
||||
start-mac: ;@echo "启动服务";
|
||||
./bin/fileboy-darwin-amd64.bin init
|
||||
./bin/fileboy-darwin-amd64.bin
|
||||
|
||||
start-linux: ;@echo "启动服务";
|
||||
./bin/fileboy-linux-amd64.bin init
|
||||
./bin/fileboy-linux-amd64.bin
|
||||
|
||||
start-win: ;@echo "启动服务";
|
||||
./bin/fileboy-windows-amd64.exe init
|
||||
./bin/fileboy-windows-amd64.exe
|
||||
|
||||
build-start-mac: build-mac start-mac
|
||||
|
||||
.PHONY: build-mac build-linux build-win build-all start-linux travis-linux start-mac start-win build-start-mac
|
61
README.md
61
README.md
@@ -1,7 +1,10 @@
|
||||
## 项目说明
|
||||
|
||||
fileboy,文件变更监听通知系统,使用 Go 编写。
|
||||
[](https://travis-ci.org/dengsgo/fileboy) [](https://goreportcard.com/report/github.com/dengsgo/fileboy)
|
||||
|
||||
[简体中文](README.md) | [ENGLISH](README_EN.md)
|
||||
|
||||
fileboy,文件变更监听通知系统,使用 Go 编写。
|
||||
适用于 Hot Reload (典型的如开发go项目,无需每次手动执行 go build;又比如前端 node 打包) 或者 系统监控的场景。
|
||||
|
||||
## 特性
|
||||
@@ -9,6 +12,7 @@ fileboy,文件变更监听通知系统,使用 Go 编写。
|
||||
- 极简的用法和配置
|
||||
- 支持多平台,Windows/Linux/MacOS
|
||||
- 支持自定义文件监听范围,监听指定文件夹/不监听指定文件夹/指定后缀文件
|
||||
- 支持自定义监控事件(write/rename/remove/create/chmod)
|
||||
- 支持设置多条命令
|
||||
- 命令支持变量占位符
|
||||
- 支持冗余任务丢弃,自定义冗余任务范围
|
||||
@@ -17,26 +21,24 @@ fileboy,文件变更监听通知系统,使用 Go 编写。
|
||||
|
||||
## 编译环境
|
||||
|
||||
go version >=1.10,推荐 1.11
|
||||
go version 1.13
|
||||
|
||||
## 更新日志
|
||||
|
||||
[CHANGELOG](CHANGELOG.md)
|
||||
|
||||
|
||||
## 运行
|
||||
|
||||
### 下载二进制文件
|
||||
|
||||
Github: [正式版 v1.6](https://github.com/dengsgo/fileboy/releases)
|
||||
Gitee: [正式版 v1.6](https://gitee.com/dengsgo/fileboy/releases)
|
||||
Github: [download v1.12](https://github.com/dengsgo/fileboy/releases)
|
||||
Gitee: [dowmload v1.12](https://gitee.com/dengsgo/fileboy/releases)
|
||||
|
||||
下载已经编译好的对应平台二进制文件,重命名为`fileboy`, 加入系统 Path 中即可。
|
||||
|
||||
### 源码编译
|
||||
|
||||
clone 该项目,进入主目录,运行命令:
|
||||
|
||||
clone 该项目,进入主目录,运行命令:
|
||||
```shell
|
||||
## 安装依赖
|
||||
go get -u gopkg.in/fsnotify/fsnotify.v1
|
||||
@@ -49,19 +51,14 @@ go build
|
||||
|
||||
## 使用
|
||||
|
||||
fileboy 的正常运行依赖于 `filegirl.yaml` 配置,所以首次在项目中使用需要初始化 `filegirl.yaml`。
|
||||
|
||||
fileboy 的正常运行依赖于 `filegirl.yaml` 配置文件,因此首次在项目中使用需要初始化 `filegirl.yaml`。
|
||||
- 进入你想要 hot reload 的项目主目录下;
|
||||
|
||||
- 运行 `fileboy init`,会在该目录下生成 `filegirl.yaml`文件;
|
||||
|
||||
- 查看 `filegirl.yaml`,修改为适合自己项目的配置项;
|
||||
|
||||
- 运行 `fileboy`即可.
|
||||
|
||||
如果你定义了 `command -> exec`命令,想事先确认是否能正常执行,可以运行 `fileboy exec`命令,系统会尝试运行你的自定义命令。
|
||||
|
||||
你可以使用 `fileboy help`查看使用帮助。
|
||||
|
||||
如果你定义了 `command -> exec`命令,想事先确认是否能正常执行,可以运行 `fileboy exec`命令,系统会尝试运行你的自定义命令。
|
||||
你可以使用 `fileboy help`查看使用帮助。
|
||||
|
||||
## filegirl.yaml 配置文件说明
|
||||
|
||||
@@ -96,6 +93,20 @@ monitor:
|
||||
types:
|
||||
- .go
|
||||
|
||||
# 监听的事件类型,发生此类事件才执行 command 中的命令
|
||||
# 没有该配置默认监听所有事件
|
||||
# write 写入文件事件
|
||||
# rename 重命名文件事件
|
||||
# remove 移除文件事件
|
||||
# create 创建文件事件
|
||||
# chmod 更新文件权限事件(类unix)
|
||||
events:
|
||||
- write
|
||||
- rename
|
||||
- remove
|
||||
- create
|
||||
- chmod
|
||||
|
||||
# 命令
|
||||
command:
|
||||
# 监听的文件有更改会执行的命令
|
||||
@@ -105,6 +116,7 @@ command:
|
||||
# 支持变量占位符,运行命令时会替换成实际值:
|
||||
# {{file}} 文件名(如 a.txt 、test/test2/a.go)
|
||||
# {{ext}} 文件后缀(如 .go)
|
||||
# {{event}} 事件(上面的events, 如 write)
|
||||
# {{changed}} 文件更新的本地时间戳(纳秒,如 1537326690523046400)
|
||||
# 变量占位符使用示例:cp {{file}} /root/sync -rf 、 myCommand --{{ext}} {{changed}}
|
||||
exec:
|
||||
@@ -125,8 +137,8 @@ notifier:
|
||||
# 请求超时 15 秒
|
||||
# POST 格式:
|
||||
# Content-Type: application/json;charset=UTF-8
|
||||
# User-Agent: FileBoy Net Notifier v1.6
|
||||
# Body: {"project_folder":"/watcher-dirs","file":"test.go","changed":1546421173070433800,"ext":".go"}
|
||||
# User-Agent: FileBoy Net Notifier v1.12
|
||||
# Body: {"project_folder":"/project/path","file":"main.go","changed":1576567861913824940,"ext":".go","event":"write"}
|
||||
# 例: http://example.com/notifier/fileboy-listener
|
||||
# 不启用通知,请留空 ""
|
||||
callUrl: ""
|
||||
@@ -139,12 +151,11 @@ notifier:
|
||||
- [x] 支持监听指定文件夹
|
||||
- [x] 支持不监听指定文件夹
|
||||
- [x] 支持监听指定后缀文件
|
||||
- [x] 支持自定事件监听
|
||||
- [x] 支持 http 通知
|
||||
- [x] 支持冗余任务丢弃
|
||||
- [ ] 支持 http 合并任务的通知
|
||||
|
||||
|
||||
|
||||
## QA
|
||||
|
||||
#### 很多框架都自带了 hot reload 的功能,为什么还要单独写个 fileboy 呢?
|
||||
@@ -154,7 +165,6 @@ notifier:
|
||||
#### fileboy 可以应用在那些具体的场景?
|
||||
|
||||
在开发中,我们很需要一款可以帮助我们自动打包编译的工具,那 fileboy 就非常适合这样的场景。比如 go 项目的热编译,让我们可以边修改代码边运行得到反馈。又比如 PHP Swoole 框架,由于常驻进程的原因,无法更改代码立即reload,使用 fileboy 就可以辅助做到传统 PHP 开发的体验。
|
||||
|
||||
对于一些需要监控文件日志或者配置变动的场景, fileboy 同样适合。你可以事先编写好相应的通知报警脚本,然后定义`filegirl.yaml`中的`command`命令,交由 fileboy 自动运行监控报警。
|
||||
|
||||
#### 通知器在什么时候会发送 http 请求 ?
|
||||
@@ -165,20 +175,13 @@ notifier:
|
||||
|
||||
由于 idea 系列软件特殊的文件保存策略,他会自动创建一些临时文件,并且在需要时多次重写文件,所以有时反映在文件上就是有多次的更改,所以会出现这种情况。1.5之后的版本增加了 `delayMillSecond` 参数,可以解决这个问题。
|
||||
|
||||
#### filegirl.yaml 里面的 command 不支持复杂的命令吗?
|
||||
|
||||
对于“很复杂的命令”这种说法很难去定义,比如 `echo "hello world"`并不复杂,但是对于 fileboy 来讲,目前无法解析这种命令。
|
||||
#### filegirl.yaml 里面的 command 如何配置复杂命令?
|
||||
|
||||
fileboy 目前支持 `命令 + 参数`这种形式的 command,而且 参数中不能有""符号或者有空格。如:
|
||||
|
||||
`go build`:支持;
|
||||
|
||||
`go env`:支持;
|
||||
|
||||
`php swoole start --daemon`:支持
|
||||
|
||||
`cat a.txt | grep "q" | wc -l`:不支持
|
||||
|
||||
对于不支持的命令,可以把它写到一个文件里,然后在 command 中执行这个文件来解决。
|
||||
|
||||
#### 为什么起名为 fileboy,又把配置名叫做 filegirl ?
|
||||
|
150
README_EN.md
Normal file
150
README_EN.md
Normal file
@@ -0,0 +1,150 @@
|
||||
## FILEBOY
|
||||
|
||||
[](https://travis-ci.org/dengsgo/fileboy) [](https://goreportcard.com/report/github.com/dengsgo/fileboy)
|
||||
|
||||
[简体中文](README.md) | [ENGLISH](README_EN.md)
|
||||
|
||||
Fileboy, File Change Monitoring Notification System, written with Go.
|
||||
For Hot Reload scenarios (typically for developing go projects without having to perform go build manually every time; for example, front-end node packaging) or system monitoring.
|
||||
|
||||
## FEATURES
|
||||
|
||||
- Minimalist usage and configuration
|
||||
- Support multiple platforms, Windows/Linux/MacOS
|
||||
- Supports custom file listening scope, listening for specified folders/not listening for specified folders/specified suffix files
|
||||
- Support for custom monitoring events (write / rename / remove / create / chmod)
|
||||
- Support for setting up multiple commands
|
||||
- Command support variable placeholders
|
||||
- Supporting redundant task discarding and customizing redundant task scope
|
||||
- Supporting HTTP notifications
|
||||
- more...
|
||||
|
||||
## COMPILE
|
||||
|
||||
go version 1.13
|
||||
|
||||
## CHANGELOG
|
||||
|
||||
[CHANGELOG](CHANGELOG.md)
|
||||
|
||||
|
||||
## RUN
|
||||
|
||||
### BINARIES
|
||||
|
||||
Github: [download v1.12](https://github.com/dengsgo/fileboy/releases)
|
||||
Gitee: [dowmload v1.12](https://gitee.com/dengsgo/fileboy/releases)
|
||||
|
||||
Download the compiled binary file of the corresponding platform, rename it `fileboy`, and add it to the system Path.
|
||||
|
||||
### SOURCE
|
||||
|
||||
Clone project, enter the project directory, run the command:
|
||||
```shell
|
||||
## installation dependency
|
||||
go get-u gopkg.in/fsnotify/fsnotify.v1
|
||||
go get-u gopkg.in/yaml.v2
|
||||
## compile
|
||||
go build
|
||||
## run
|
||||
./fileboy
|
||||
```
|
||||
## USAGE
|
||||
|
||||
The normal operation of fileboy depends on the `filegirl.yaml` configuration, so for the first time in a project, `filegirl.yaml` needs to be initialized.
|
||||
- Enter the project home directory where you want hot reload;
|
||||
- Running `fileboy init` will generate `filegirl. yaml` files in this directory.
|
||||
- View `filegirl. yaml` and modify it to a configuration item suitable for your project.
|
||||
- Run `fileboy`.
|
||||
|
||||
If you define the `command-> exec` command, you can run the `fileboy exec` command to confirm whether it can be executed properly in advance. The system will try to run your custom command.
|
||||
You can use `fileboy help` to see help info.
|
||||
|
||||
## filegirl.yaml
|
||||
|
||||
```yaml
|
||||
core:
|
||||
# config version code
|
||||
version: 1
|
||||
|
||||
# monitor section
|
||||
monitor:
|
||||
# directories to monitor
|
||||
# test1 listen for the test1 directory in the project directory
|
||||
# test1/test2 listen for the test1/test2 directory in the project directory
|
||||
# test1,* listen for the test1 directory in the project directory and all its subdirectories (recursion)
|
||||
# .,* listen for the project directory and all its subdirectories (recursion)
|
||||
includeDirs:
|
||||
- .,*
|
||||
|
||||
# Unmonitored directories
|
||||
# .idea ignore listening to .idea directory and all its subdirectories
|
||||
exceptDirs:
|
||||
- .idea
|
||||
- .git
|
||||
- .vscode
|
||||
- node_modules
|
||||
- vendor
|
||||
|
||||
# the suffix of the listener file, which changes the file to execute commands
|
||||
# .go file changes suffixed with .go execute commands
|
||||
# .* all file changes execute commands in the command
|
||||
types:
|
||||
- .go
|
||||
|
||||
# the type of event to listen to. Only when such an event occurs can the command in command be executed
|
||||
# without this configuration, all events will be monitored by default
|
||||
# write write file event
|
||||
# rename rename file event
|
||||
# remove remove remove file event
|
||||
# create create file event
|
||||
# chmod update file permission event (UNIX like)
|
||||
events:
|
||||
- write
|
||||
- rename
|
||||
- remove
|
||||
- create
|
||||
- chmod
|
||||
|
||||
command:
|
||||
# the files monitored have commands that change to be executed
|
||||
# there can be multiple commands that will be executed in turn
|
||||
# in case of interactive commands, allow external access to input
|
||||
# variable placeholders are supported, and the actual values are replaced when the command is run:
|
||||
# {{file}} (e.g: a.txt 、test/test2/a.go)
|
||||
# {{ext}} (e.g: .go)
|
||||
# {{event}} event(e.g: write)
|
||||
# {{changed}} local timestamp for file updated(nanosecond,e.g 1537326690523046400)
|
||||
# variable placeholders e.g:cp {{file}} /root/sync -rf 、 myCommand --{{ext}} {{changed}}
|
||||
exec:
|
||||
- go version
|
||||
- go env
|
||||
|
||||
# the command will not execute until XX milliseconds after the file changes
|
||||
# a change event (A) cancels execution if there is a new file change event (B) within the defined delay time (t).
|
||||
# B and subsequent events are analogized in turn until event Z does not produce new events within t, and Z executes.
|
||||
# reasonable setting of delay time will effectively reduce redundancy and duplicate task execution.
|
||||
# If this feature is not required, set to 0
|
||||
delayMillSecond: 2000
|
||||
|
||||
notifier:
|
||||
# file changes send requests to the URL (POST JSON text data)
|
||||
# the timing of triggering the request is consistent with executing the command command
|
||||
# timeout 15 second
|
||||
# POST :
|
||||
# Content-Type: application/json;charset=UTF-8
|
||||
# User-Agent: FileBoy Net Notifier v1.12
|
||||
# Body: {"project_folder":"/project/path","file":"main.go","changed":1576567861913824940,"ext":".go","event":"write"}
|
||||
# e.g: http://example.com/notifier/fileboy-listener
|
||||
# no notice is enabled. Please leave it blank. ""
|
||||
callUrl: ""
|
||||
```
|
||||
|
||||
### CONTRIBUTOR
|
||||
|
||||
[@dengsgo](https://www.yoytang.com) <dengsgo@gmail.com>
|
||||
|
||||
[@itwesley](https://github.com/itwesley) <wcshen1126@gmail.com>
|
||||
|
||||
[@jason-gao](https://github.com/jason-gao) <3048789891@qq.com>
|
||||
|
206
fileboy.go
206
fileboy.go
@@ -9,6 +9,7 @@ import (
|
||||
"math/rand"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -23,31 +24,53 @@ const (
|
||||
var (
|
||||
projectFolder = "."
|
||||
|
||||
filegirlYamlName = "filegirl.yaml"
|
||||
|
||||
cfg *FileGirl
|
||||
|
||||
watcher *fsnotify.Watcher
|
||||
|
||||
taskMan *TaskMan
|
||||
|
||||
ioeventMapStr = map[fsnotify.Op]string{
|
||||
fsnotify.Write: "write",
|
||||
fsnotify.Rename: "rename",
|
||||
fsnotify.Remove: "remove",
|
||||
fsnotify.Create: "create",
|
||||
fsnotify.Chmod: "chmod",
|
||||
}
|
||||
)
|
||||
|
||||
type changedFile struct {
|
||||
Name string
|
||||
Changed int64
|
||||
Ext string
|
||||
Event string
|
||||
}
|
||||
|
||||
func parseConfig() {
|
||||
cfg = new(FileGirl)
|
||||
fc, err := ioutil.ReadFile(projectFolder + "/filegirl.yaml")
|
||||
fc, err := ioutil.ReadFile(getFileGirlPath())
|
||||
if err != nil {
|
||||
log.Panicln(PreError, "read filegirl.yaml file err: ", err)
|
||||
log.Println(PreError, "The filegirl.yaml file in", projectFolder, "is not exist! ", err)
|
||||
fmt.Print(firstRunHelp)
|
||||
logAndExit("Fileboy unable to run.")
|
||||
}
|
||||
err = yaml.Unmarshal(fc, cfg)
|
||||
if err != nil {
|
||||
log.Panicln(PreError, "parsed filegirl.yaml failed: ", err)
|
||||
logAndExit(PreError, "Parsed filegirl.yaml failed: ", err)
|
||||
}
|
||||
if cfg.Core.Version > Version {
|
||||
log.Panicln(PreError, "current fileboy support max version : ", Version)
|
||||
logAndExit(PreError, "Current fileboy support max version : ", Version)
|
||||
}
|
||||
// init map
|
||||
cfg.Monitor.TypesMap = map[string]bool{}
|
||||
cfg.Monitor.IncludeDirsMap = map[string]bool{}
|
||||
cfg.Monitor.ExceptDirsMap = map[string]bool{}
|
||||
cfg.Monitor.IncludeDirsRec = map[string]bool{}
|
||||
// convert to map
|
||||
for _, v := range cfg.Monitor.Types {
|
||||
cfg.Monitor.TypesMap[v] = true
|
||||
}
|
||||
log.Println(cfg)
|
||||
}
|
||||
@@ -55,88 +78,92 @@ func parseConfig() {
|
||||
func eventDispatcher(event fsnotify.Event) {
|
||||
ext := path.Ext(event.Name)
|
||||
if len(cfg.Monitor.Types) > 0 &&
|
||||
cfg.Monitor.Types[0] != ".*" &&
|
||||
!inStringArray(ext, cfg.Monitor.Types) {
|
||||
//log.Println(ext, cfg.Monitor.Types, inStringArray(ext, cfg.Monitor.Types))
|
||||
!keyInMonitorTypesMap(".*", cfg) &&
|
||||
!keyInMonitorTypesMap(ext, cfg) {
|
||||
return
|
||||
}
|
||||
switch event.Op {
|
||||
case
|
||||
fsnotify.Write,
|
||||
fsnotify.Rename:
|
||||
log.Println("EVENT", event.Op.String(), ":", event.Name)
|
||||
taskMan.Put(&changedFile{
|
||||
Name: relativePath(projectFolder, event.Name),
|
||||
Changed: time.Now().UnixNano(),
|
||||
Ext: ext,
|
||||
})
|
||||
case fsnotify.Remove:
|
||||
case fsnotify.Create:
|
||||
|
||||
op := ioeventMapStr[event.Op]
|
||||
if len(cfg.Monitor.Events) != 0 && !inStrArray(op, cfg.Monitor.Events) {
|
||||
return
|
||||
}
|
||||
log.Println("EVENT", event.Op.String(), ":", event.Name)
|
||||
taskMan.Put(&changedFile{
|
||||
Name: relativePath(projectFolder, event.Name),
|
||||
Changed: time.Now().UnixNano(),
|
||||
Ext: ext,
|
||||
Event: op,
|
||||
})
|
||||
}
|
||||
|
||||
func addWatcher() {
|
||||
log.Println("collecting directory information...")
|
||||
dirs := make([]string, 0)
|
||||
for i := 0; i < len(cfg.Monitor.IncludeDirs); i++ {
|
||||
darr := dirParse2Array(cfg.Monitor.IncludeDirs[i])
|
||||
dirsMap := map[string]bool{}
|
||||
for _, dir := range cfg.Monitor.IncludeDirs {
|
||||
darr := dirParse2Array(dir)
|
||||
if len(darr) < 1 || len(darr) > 2 {
|
||||
log.Fatalln(PreError, "filegirl section monitor dirs is error. ", cfg.Monitor.IncludeDirs[i])
|
||||
logAndExit(PreError, "filegirl section monitor dirs is error. ", dir)
|
||||
}
|
||||
if strings.HasPrefix(darr[0], "/") {
|
||||
log.Fatalln(PreError, "dirs must be relative paths ! err path:", cfg.Monitor.IncludeDirs[i])
|
||||
logAndExit(PreError, "dirs must be relative paths ! err path:", dir)
|
||||
}
|
||||
if darr[0] == "." {
|
||||
if len(darr) == 2 && darr[1] == "*" {
|
||||
dirs = make([]string, 0)
|
||||
dirs = append(dirs, ".")
|
||||
// The highest priority
|
||||
dirsMap = map[string]bool{
|
||||
projectFolder: true,
|
||||
}
|
||||
listFile(projectFolder, func(d string) {
|
||||
dirs = arrayUniqueAdd(dirs, d)
|
||||
dirsMap[d] = true
|
||||
})
|
||||
cfg.Monitor.IncludeDirsRec[projectFolder] = true
|
||||
break
|
||||
} else {
|
||||
dirs = arrayUniqueAdd(dirs, projectFolder)
|
||||
dirsMap[projectFolder] = true
|
||||
}
|
||||
break
|
||||
} else {
|
||||
md := projectFolder + "/" + darr[0]
|
||||
dirsMap[md] = true
|
||||
if len(darr) == 2 && darr[1] == "*" {
|
||||
dirs = arrayUniqueAdd(dirs, md)
|
||||
listFile(md, func(d string) {
|
||||
dirs = arrayUniqueAdd(dirs, d)
|
||||
dirsMap[d] = true
|
||||
})
|
||||
} else {
|
||||
dirs = arrayUniqueAdd(dirs, md)
|
||||
cfg.Monitor.IncludeDirsRec[md] = true
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
for i := 0; i < len(cfg.Monitor.ExceptDirs); i++ {
|
||||
p := projectFolder + "/" + cfg.Monitor.ExceptDirs[i]
|
||||
dirs = arrayRemoveElement(dirs, p)
|
||||
for _, dir := range cfg.Monitor.ExceptDirs {
|
||||
if dir == "." {
|
||||
logAndExit(PreError, "exceptDirs must is not project root path ! err path:", dir)
|
||||
}
|
||||
p := projectFolder + "/" + dir
|
||||
delete(dirsMap, p)
|
||||
listFile(p, func(d string) {
|
||||
dirs = arrayRemoveElement(dirs, d)
|
||||
delete(dirsMap, d)
|
||||
})
|
||||
}
|
||||
for _, dir := range dirs {
|
||||
for dir := range dirsMap {
|
||||
log.Println("watcher add -> ", dir)
|
||||
err := watcher.Add(dir)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
logAndExit(PreError, err)
|
||||
}
|
||||
}
|
||||
log.Println("total monitored dirs: " + strconv.Itoa(len(dirsMap)))
|
||||
log.Println("fileboy is ready.")
|
||||
cfg.Monitor.DirsMap = dirsMap
|
||||
}
|
||||
|
||||
func initWatcher() {
|
||||
parseConfig()
|
||||
var err error
|
||||
if watcher != nil {
|
||||
_ = watcher.Close()
|
||||
}
|
||||
watcher, err = fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
logAndExit(err)
|
||||
}
|
||||
defer watcher.Close()
|
||||
|
||||
done := make(chan bool)
|
||||
taskMan = newTaskMan(cfg.Command.DelayMillSecond, cfg.Notifier.CallUrl)
|
||||
go func() {
|
||||
for {
|
||||
@@ -145,6 +172,9 @@ func initWatcher() {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
// directory structure changes, dynamically add, delete and monitor according to rules
|
||||
// TODO // this method cannot be triggered when the parent folder of the change folder is not monitored
|
||||
go watchChangeHandler(event)
|
||||
eventDispatcher(event)
|
||||
case err, ok := <-watcher.Errors:
|
||||
if !ok {
|
||||
@@ -155,31 +185,82 @@ func initWatcher() {
|
||||
}
|
||||
}()
|
||||
addWatcher()
|
||||
<-done
|
||||
}
|
||||
|
||||
func watchChangeHandler(event fsnotify.Event) {
|
||||
if event.Op != fsnotify.Create && event.Op != fsnotify.Rename {
|
||||
return
|
||||
}
|
||||
_, err := ioutil.ReadDir(event.Name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
do := false
|
||||
for rec := range cfg.Monitor.IncludeDirsRec {
|
||||
if !strings.HasPrefix(event.Name, rec) {
|
||||
continue
|
||||
}
|
||||
// check exceptDirs
|
||||
has := false
|
||||
for _, v := range cfg.Monitor.ExceptDirs {
|
||||
if strings.HasPrefix(event.Name, projectFolder+"/"+v) {
|
||||
has = true
|
||||
}
|
||||
}
|
||||
if has {
|
||||
continue
|
||||
}
|
||||
|
||||
_ = watcher.Remove(event.Name)
|
||||
err := watcher.Add(event.Name)
|
||||
if err == nil {
|
||||
do = true
|
||||
log.Println("watcher add -> ", event.Name)
|
||||
} else {
|
||||
log.Println(PreWarn, "watcher add faild:", event.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
if do {
|
||||
return
|
||||
}
|
||||
|
||||
// check map
|
||||
if _, ok := cfg.Monitor.DirsMap[event.Name]; ok {
|
||||
_ = watcher.Remove(event.Name)
|
||||
err := watcher.Add(event.Name)
|
||||
if err == nil {
|
||||
log.Println("watcher add -> ", event.Name)
|
||||
} else {
|
||||
log.Println(PreWarn, "watcher add faild:", event.Name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func parseArgs() {
|
||||
l := len(os.Args)
|
||||
if l == 1 {
|
||||
_, err := ioutil.ReadFile(projectFolder + "/filegirl.yaml")
|
||||
if err != nil {
|
||||
log.Println(PreError, "the filegirl.yaml file does not exist! ", err)
|
||||
fmt.Print(firstRunHelp)
|
||||
return
|
||||
}
|
||||
switch len(os.Args) {
|
||||
case 1:
|
||||
parseConfig()
|
||||
done := make(chan bool)
|
||||
initWatcher()
|
||||
defer watcher.Close()
|
||||
<-done
|
||||
return
|
||||
}
|
||||
if l == 2 {
|
||||
case 2:
|
||||
c := os.Args[1]
|
||||
switch c {
|
||||
case "init":
|
||||
err := ioutil.WriteFile(projectFolder+"/filegirl.yaml", []byte(exampleFileGirl), 0644)
|
||||
_, err := ioutil.ReadFile(getFileGirlPath())
|
||||
if err == nil {
|
||||
log.Println(PreError, "Profile filegirl.yaml already exists.")
|
||||
logAndExit("If you want to regenerate filegirl.yaml, delete it first")
|
||||
}
|
||||
err = ioutil.WriteFile(getFileGirlPath(), []byte(exampleFileGirl), 0644)
|
||||
if err != nil {
|
||||
log.Println(PreError, "error create filegirl.yaml config! ", err)
|
||||
log.Println(PreError, "Profile filegirl.yaml create failed! ", err)
|
||||
return
|
||||
}
|
||||
log.Println("create filegirl.yaml ok")
|
||||
log.Println("Profile filegirl.yaml created ok")
|
||||
return
|
||||
case "exec":
|
||||
parseConfig()
|
||||
@@ -191,9 +272,15 @@ func parseArgs() {
|
||||
fmt.Print(helpStr)
|
||||
}
|
||||
return
|
||||
default:
|
||||
logAndExit("Unknown parameters, use `fileboy help` show help info.")
|
||||
}
|
||||
}
|
||||
|
||||
func getFileGirlPath() string {
|
||||
return projectFolder + "/" + filegirlYamlName
|
||||
}
|
||||
|
||||
func show() {
|
||||
fmt.Print(logo)
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
@@ -205,11 +292,12 @@ func show() {
|
||||
func main() {
|
||||
log.SetPrefix("[FileBoy]: ")
|
||||
log.SetFlags(2)
|
||||
log.SetOutput(os.Stdout)
|
||||
show()
|
||||
var err error
|
||||
projectFolder, err = os.Getwd()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
logAndExit(err)
|
||||
}
|
||||
parseArgs()
|
||||
}
|
||||
|
@@ -8,6 +8,14 @@ type FileGirl struct {
|
||||
Types []string `yaml:"types"`
|
||||
IncludeDirs []string `yaml:"includeDirs"`
|
||||
ExceptDirs []string `yaml:"exceptDirs"`
|
||||
Events []string `yaml:"events"`
|
||||
// convert to
|
||||
TypesMap map[string]bool `yaml:"-"`
|
||||
IncludeDirsMap map[string]bool `yaml:"-"`
|
||||
ExceptDirsMap map[string]bool `yaml:"-"`
|
||||
DirsMap map[string]bool `yaml:"-"`
|
||||
|
||||
IncludeDirsRec map[string]bool `yaml:"-"`
|
||||
}
|
||||
Command struct {
|
||||
Exec []string `yaml:"exec"`
|
||||
|
12
mac-build-all
Executable file
12
mac-build-all
Executable file
@@ -0,0 +1,12 @@
|
||||
export CGO_ENABLED=0
|
||||
export GOARCH=amd64
|
||||
export GOOS=darwin
|
||||
go build -ldflags "-s -w" -o fileboy-darwin-amd64.bin
|
||||
echo darwin ok
|
||||
export GOOS=linux
|
||||
go build -ldflags "-s -w" -o fileboy-linux-amd64.bin
|
||||
echo linux ok
|
||||
export GOOS=windows
|
||||
go build -ldflags "-s -w" -o fileboy-windows-amd64.exe
|
||||
echo windows ok
|
||||
|
7
mergeUpstream.sh
Executable file
7
mergeUpstream.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 合并上游-fork来源
|
||||
git remote add upstream https://github.com/dengsgo/fileboy.git
|
||||
git fetch upstream
|
||||
git checkout master
|
||||
git merge upstream/master
|
@@ -14,6 +14,7 @@ type postParams struct {
|
||||
File string `json:"file"`
|
||||
Changed int64 `json:"changed"`
|
||||
Ext string `json:"ext"`
|
||||
Event string `json:"event"`
|
||||
}
|
||||
|
||||
type NetNotifier struct {
|
||||
@@ -42,6 +43,7 @@ func (n *NetNotifier) Put(cf *changedFile) {
|
||||
File: cf.Name,
|
||||
Changed: cf.Changed,
|
||||
Ext: cf.Ext,
|
||||
Event: cf.Event,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -60,7 +62,7 @@ func (n *NetNotifier) dispatch(params *postParams) {
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
|
||||
req.Header.Set("User-Agent", "FileBoy Net Notifier v1.6")
|
||||
req.Header.Set("User-Agent", "FileBoy Net Notifier v1.12")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Println(PreError, "notifier call failed. err:", err)
|
||||
|
27
raw.go
27
raw.go
@@ -32,6 +32,20 @@ monitor:
|
||||
types:
|
||||
- .go
|
||||
|
||||
# 监听的事件类型,发生此类事件才执行 command 中的命令
|
||||
# 没有该配置默认监听所有事件
|
||||
# write 写入文件事件
|
||||
# rename 重命名文件事件
|
||||
# remove 移除文件事件
|
||||
# create 创建文件事件
|
||||
# chmod 更新文件权限事件(类unix)
|
||||
events:
|
||||
- write
|
||||
- rename
|
||||
- remove
|
||||
- create
|
||||
- chmod
|
||||
|
||||
# 命令
|
||||
command:
|
||||
# 监听的文件有更改会执行的命令
|
||||
@@ -41,6 +55,7 @@ command:
|
||||
# 支持变量占位符,运行命令时会替换成实际值:
|
||||
# {{file}} 文件名(如 a.txt 、test/test2/a.go)
|
||||
# {{ext}} 文件后缀(如 .go)
|
||||
# {{event}} 事件(上面的events, 如 write)
|
||||
# {{changed}} 文件更新的本地时间戳(纳秒,如 1537326690523046400)
|
||||
# 变量占位符使用示例:cp {{file}} /root/sync -rf 、 myCommand --{{ext}} {{changed}}
|
||||
exec:
|
||||
@@ -48,7 +63,7 @@ command:
|
||||
- go env
|
||||
|
||||
# 文件变更后命令在xx毫秒后才会执行,单位为毫秒
|
||||
# 一个变更事件(A)如果在定义的延迟时间(t)内,又有新的文件变更事件(B),那么A会取消执行。
|
||||
# 一个变更事件(A)如果在定义的延迟时间(t)内, 又有新的文件变更事件(B), 那么A会取消执行。
|
||||
# B及以后的事件均依次类推,直到事件Z在t内没有新事件产生,Z 会执行
|
||||
# 合理设置延迟时间,将有效减少冗余和重复任务的执行
|
||||
# 如果不需要该特性,设置为 0
|
||||
@@ -61,8 +76,8 @@ notifier:
|
||||
# 请求超时 15 秒
|
||||
# POST 格式:
|
||||
# Content-Type: application/json;charset=UTF-8
|
||||
# User-Agent: FileBoy Net Notifier v1.6
|
||||
# Body: {"project_folder":"/watcher-dirs","file":"test.go","changed":1546421173070433800,"ext":".go"}
|
||||
# User-Agent: FileBoy Net Notifier v1.12
|
||||
# Body: {"project_folder":"/project/path","file":"main.go","changed":1576567861913824940,"ext":".go","event":"write"}
|
||||
# 例: http://example.com/notifier/fileboy-listener
|
||||
# 不启用通知,请留空 ""
|
||||
callUrl: ""
|
||||
@@ -104,13 +119,13 @@ var logo = `
|
||||
_____ _ | | _____ ____) ) | | | |___| |
|
||||
| ___) | | | | | ___) | __ (| | | |\_____/
|
||||
| | _| |_| |_____| |_____| |__) ) |___| | ___
|
||||
|_| (_____)_______)_______)______/ \_____/ (___) V1.6
|
||||
|_| (_____)_______)_______)______/ \_____/ (___) V1.12
|
||||
`
|
||||
var statement = `Dengsgo [dengsgo@gmail.com] Open Source with MIT License`
|
||||
|
||||
var versionDesc = `
|
||||
Version fileboy: v1.6 filegirl: v` + strconv.Itoa(Version) + `
|
||||
Released 2019.1.19
|
||||
Version fileboy: v1.12 filegirl: v` + strconv.Itoa(Version) + `
|
||||
Released 2019.12.18
|
||||
Licence MIT
|
||||
Author dengsgo [dengsgo@gmail.com]
|
||||
Website https://github.com/dengsgo/fileboy
|
||||
|
@@ -36,7 +36,7 @@ func (t *TaskMan) Put(cf *changedFile) {
|
||||
defer t.putLock.Unlock()
|
||||
t.lastTaskId = cf.Changed
|
||||
go func() {
|
||||
<-time.Tick(time.Millisecond * time.Duration(t.delay))
|
||||
<-time.After(time.Millisecond * time.Duration(t.delay))
|
||||
if t.lastTaskId > cf.Changed {
|
||||
return
|
||||
}
|
||||
@@ -45,17 +45,17 @@ func (t *TaskMan) Put(cf *changedFile) {
|
||||
}
|
||||
|
||||
func (t *TaskMan) preRun(cf *changedFile) {
|
||||
if t.cmd != nil && t.cmd.Process != nil && !t.cmd.ProcessState.Exited() {
|
||||
if t.cmd != nil && t.cmd.Process != nil {
|
||||
log.Println("stop old process ")
|
||||
if err := t.cmd.Process.Kill(); err != nil {
|
||||
log.Println(PreWarn, "stopped err, reason:", err)
|
||||
}
|
||||
}
|
||||
go t.run(cf)
|
||||
go t.notifier.Put(cf)
|
||||
}
|
||||
|
||||
func (t *TaskMan) run(cf *changedFile) {
|
||||
go t.notifier.Put(cf)
|
||||
t.runLock.Lock()
|
||||
defer t.runLock.Unlock()
|
||||
for i := 0; i < len(cfg.Command.Exec); i++ {
|
||||
@@ -90,7 +90,7 @@ func (t *TaskMan) run(cf *changedFile) {
|
||||
log.Println(PreWarn, "cmd wait err ", err)
|
||||
break
|
||||
}
|
||||
if t.cmd.Process != nil && !t.cmd.ProcessState.Exited() {
|
||||
if t.cmd.Process != nil {
|
||||
if err = t.cmd.Process.Kill(); err != nil {
|
||||
log.Println(PreWarn, "cmd cannot kill, reason:", err)
|
||||
}
|
||||
|
57
util.go
57
util.go
@@ -2,17 +2,15 @@ package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func inStringArray(value string, arr []string) bool {
|
||||
for _, v := range arr {
|
||||
if value == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
func keyInMonitorTypesMap(k string, cfg *FileGirl) bool {
|
||||
_, ok := cfg.Monitor.TypesMap[k]
|
||||
return ok
|
||||
}
|
||||
|
||||
func cmdParse2Array(s string, cf *changedFile) []string {
|
||||
@@ -27,12 +25,15 @@ func cmdParse2Array(s string, cf *changedFile) []string {
|
||||
}
|
||||
|
||||
func strParseRealStr(s string, cf *changedFile) string {
|
||||
return strings.Replace(
|
||||
strings.Replace(
|
||||
strings.Replace(s, "{{file}}", cf.Name, -1),
|
||||
"{{ext}}", cf.Ext, -1,
|
||||
return strings.ReplaceAll(
|
||||
strings.ReplaceAll(
|
||||
strings.ReplaceAll(
|
||||
strings.ReplaceAll(s, "{{file}}", cf.Name),
|
||||
"{{ext}}", cf.Ext,
|
||||
),
|
||||
"{{changed}}", strconv.FormatInt(cf.Changed, 10),
|
||||
),
|
||||
"{{changed}}", strconv.FormatInt(cf.Changed, 10), -1,
|
||||
"{{event}}", cf.Event,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -58,30 +59,24 @@ func listFile(folder string, fun func(string)) {
|
||||
}
|
||||
}
|
||||
|
||||
func arrayUniqueAdd(a []string, add string) []string {
|
||||
if inStringArray(add, a) {
|
||||
return a
|
||||
func relativePath(folder, p string) string {
|
||||
s := strings.ReplaceAll(strings.TrimPrefix(p, folder), "\\", "/")
|
||||
if strings.HasPrefix(s, "/") && len(s) > 1 {
|
||||
s = s[1:]
|
||||
}
|
||||
return append(a, add)
|
||||
return s
|
||||
}
|
||||
|
||||
func arrayRemoveElement(a []string, r string) []string {
|
||||
i := -1
|
||||
for k, v := range a {
|
||||
if v == r {
|
||||
i = k
|
||||
break
|
||||
func inStrArray(s string, arr []string) bool {
|
||||
for _, v := range arr {
|
||||
if s == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if i == -1 {
|
||||
return a
|
||||
}
|
||||
if len(a) == 1 && i == 0 {
|
||||
return []string{}
|
||||
}
|
||||
return append(a[:i], a[i+1:]...)
|
||||
return false
|
||||
}
|
||||
|
||||
func relativePath(folder, p string) string {
|
||||
return strings.TrimPrefix(p, folder)
|
||||
func logAndExit(v ...interface{}) {
|
||||
log.Println(v...)
|
||||
os.Exit(15)
|
||||
}
|
||||
|
Reference in New Issue
Block a user