Compare commits

...

108 Commits
v1.7 ... main

Author SHA1 Message Date
ehlxr c221fbc347 Merge branch 'master' into dev 2020-11-16 17:43:20 +08:00
ehlxr ecf0676d67 fix bug in should-finish instruction 2020-11-16 16:58:56 +08:00
ehlxr b4ad1e0e96 fix bug in should-finish instruction 2020-11-16 16:55:15 +08:00
ehlxr 195740fa3d support absolute dir 2020-11-11 15:19:32 +08:00
ehlxr 4f3ad86984 support absolute dir 2020-11-11 15:17:48 +08:00
ehlxr 4b3bf5e77c add profile config 2020-11-11 15:01:36 +08:00
ehlxr bf4f156714 add ignore suffix of the listener file 2020-11-10 17:33:45 +08:00
ehlxr 6a59036070 add ignore suffix of the listener file 2020-11-10 17:24:14 +08:00
ehlxr 8dc1ebe6d9 add ignore suffix of the listener file 2020-11-10 17:13:49 +08:00
dengsgo da54305188 增加 1.16 2020-10-17 10:29:33 +08:00
dengsgo 9e651d3023 优化 代码 2020-10-17 10:24:37 +08:00
dengsgo 605914d1e1 增加 1.16 2020-08-23 16:22:08 +08:00
dengsgo 86cf40564b 优化 文件扫描性能 2020-08-23 16:17:18 +08:00
dengsgo 819297118c 更新 changelog 2020-07-19 18:10:34 +08:00
dengsgo a76d4cfe6c 增加 对信号的处理 2020-07-19 18:06:24 +08:00
dengsgo 14ba79c2b3 更新 README 2020-06-27 15:15:29 +08:00
dengsgo c5f0649ab5 增加 pid文件进程感知 2020-06-27 15:11:55 +08:00
dengsgo 7c6f8f910b 增加 snap config 2020-04-22 15:40:09 +08:00
dengsgo d07616bde9 rename 2020-03-16 11:19:13 +08:00
dengsgo 4f81692954 typo deamon->daemon 2020-03-16 11:16:14 +08:00
Deng.Liu 588af5143f
Update README.md 2020-03-15 21:10:11 +08:00
Deng.Liu 2d8924b7fe
Update go.yml 2020-03-15 21:02:32 +08:00
Deng.Liu 08fb7aabf8
Update go.yml 2020-03-15 20:56:56 +08:00
Deng.Liu 9f0a51ce0e
Update go.yml 2020-03-15 20:55:31 +08:00
Deng.Liu 8a3a2d570b
Update go.yml 2020-03-15 20:53:42 +08:00
Deng.Liu e5405f4fa9
Update go.yml 2020-03-15 20:46:58 +08:00
Deng.Liu 9d9cce0f31
Create go.yml 2020-03-15 20:43:56 +08:00
dengsgo c0364a62bc 发行 v1.15 2020-03-08 13:26:38 +08:00
dengsgo a9758653ba 修改 changelog 2020-03-08 13:24:49 +08:00
dengsgo b1c5f7da39 修改 readme mod 2020-03-05 15:37:36 +08:00
dengsgo 29fc62de90 更改 mod管理依赖 2020-03-05 15:31:47 +08:00
dengsgo 2d409fbce0 修改 log 2020-03-04 13:41:02 +08:00
dengsgo 550dc01dfb 修改 readme 2020-03-04 11:18:21 +08:00
dengsgo d0492bad8c 更新 readme 2020-03-03 15:01:08 +08:00
dengsgo 55625d51bc 增加 支持 2020-03-03 14:57:35 +08:00
dengsgo 8ddc124f89 增加 支持 2020-03-03 14:54:12 +08:00
dengsgo 8b7042411c 修改 rekeased 2020-03-03 11:39:15 +08:00
dengsgo b72caedcbb 重构 指令 2020-03-03 11:37:20 +08:00
dengsgo f989590d14 修改 文案 2020-03-03 11:12:08 +08:00
dengsgo f2afa015bd 修改 版本 2020-01-05 14:47:46 +08:00
dengsgo dc7162b57d 修改 配置 2020-01-05 14:44:56 +08:00
dengsgo 8828bcaed6 修复 错误 2020-01-02 18:32:16 +08:00
dengsgo 7d2e8dd34d 修复 panic 2020-01-02 18:22:16 +08:00
dengsgo bb80977213 更新 changelog 2020-01-02 17:42:02 +08:00
dengsgo 1b4206a6b3 增加 ignore*指令支持 2020-01-02 17:38:52 +08:00
dengsgo 3351429515 重构 log 2020-01-02 17:25:02 +08:00
dengsgo 00e34193aa 更新 changelog 2020-01-02 16:58:01 +08:00
dengsgo 4e0b9f54f6 增加 指令支持 2020-01-02 16:38:26 +08:00
dengsgo 9defc968ba 优化 taskman 2019-12-27 18:09:56 +08:00
dengsgo 66eee100f2 忽略 pid 2019-12-27 16:46:36 +08:00
dengsgo 9e4642d0c2 更新 logo出现的时机 2019-12-27 16:43:49 +08:00
dengsgo 44bb0dd65f 增加 未知参数提醒 2019-12-27 16:31:00 +08:00
dengsgo eeaad58d1e 更新 .travis 2019-12-24 11:50:21 +08:00
dengsgo 44051b9184 更新 .travis 2019-12-24 11:47:21 +08:00
dengsgo a2daa98d08 更新 .travis 2019-12-24 11:35:51 +08:00
dengsgo 97bdcb644c 忽略 pid监听 2019-12-24 11:27:38 +08:00
dengsgo a0137807f3 update echo PATH 2019-12-23 16:26:20 +08:00
dengsgo b0a0e296d2 update echo PATH 2019-12-23 16:23:28 +08:00
dengsgo e63d200382 更新 .travis 2019-12-23 16:18:03 +08:00
dengsgo cb86b0dbe6 增加 守护进程 2019-12-23 16:13:31 +08:00
dengsgo bead88c5e2 更新 travis script 2019-12-17 19:28:46 +08:00
dengsgo b8c709ac4c 发行 v1.12 2019-12-17 16:42:56 +08:00
dengsgo 4d5e13daf5 更新 v1.12 2019-12-17 16:38:14 +08:00
dengsgo 46df7a206a 增加 init提示 2019-12-17 16:10:33 +08:00
dengsgo 04c46435d4 更新 readme 2019-12-17 15:37:20 +08:00
dengsgo aa23628137 更新 readme 2019-12-11 18:02:00 +08:00
Deng.Liu 86ceb19acd
Merge pull request #11 from jason-gao/master
增加 Makefile
2019-12-11 16:36:03 +08:00
Deng.Liu a3f5af69ae
Update .travis.yml 2019-12-11 16:29:22 +08:00
Deng.Liu 4f044b8ebf
Update Makefile 2019-12-11 16:25:02 +08:00
Deng.Liu b5f068dd55
Update .travis.yml 2019-12-11 16:19:19 +08:00
Deng.Liu f912ccb753
Delete build.sh 2019-12-11 16:13:47 +08:00
Deng.Liu 7dd5b44ab6
更新 Makefile
add -u flag
2019-12-11 16:13:25 +08:00
jason-gao 679ddba2d8 update travis 2019-12-10 14:15:33 +08:00
jason-gao e4d9b74869 update 2019-12-09 18:06:05 +08:00
jason-gao c61fac453d update travis 2019-12-09 17:40:25 +08:00
jason-gao f4859b37d8 add Makefile 2019-12-09 16:47:40 +08:00
jason-gao 06fb9f6a97 Merge remote-tracking branch 'upstream/master' 2019-12-09 16:26:40 +08:00
dengsgo 3e47cd28ba 更新 错误 2019-12-06 16:26:41 +08:00
dengsgo 753ff72f85 增加 动态监听 2019-12-06 11:51:28 +08:00
dengsgo aa929ae838 增加 自定义事件支持 2019-12-04 11:50:11 +08:00
dengsgo a0af500678 修复 includeDirs不包含项目目录时仍会监听该目录的问题 2019-12-04 11:02:22 +08:00
dengsgo 3964324f43 修改 控制台输出 2019-12-04 10:31:49 +08:00
dengsgo 3bc323154f 修改 版本 2019-12-04 10:31:17 +08:00
Deng.Liu 08d421ebcb add gitpod.yml 2019-10-09 10:23:40 +00:00
Deng.Liu 134f6e45f5
Update .travis.yml 2019-08-12 13:56:11 +08:00
Deng.Liu 8552d33dcd
Create .travis.yml 2019-08-12 13:46:52 +08:00
Deng.Liu 58a3b27da9
Update README.md 2019-08-12 13:45:48 +08:00
jason-gao 7022e8bb04 add build shell 2019-06-09 17:29:45 +08:00
dengsgo a69cbeea2d 发行 1.10版本 2019-06-04 11:25:19 +08:00
dengsgo c9aedb2d34 修改 参数 2019-06-04 11:19:24 +08:00
dengsgo d2be0654e2 优化 log 2019-06-04 11:12:41 +08:00
dengsgo b6be235c6e 调整 位置 2019-06-04 10:58:42 +08:00
dengsgo 30312d4b9c 删除 log 2019-06-04 10:56:27 +08:00
dengsgo 281a4f4d64 修复 gin框架一直wait的问题 2019-06-04 10:52:58 +08:00
dengsgo 8181a8f0c5 优化 错误信息 2019-05-15 11:38:03 +08:00
Deng.Liu 9e73a0901c
修改 readme 2019-04-13 18:48:22 +08:00
dengsgo 789caba260 发行 1.9版本 2019-04-04 14:48:11 +08:00
dengsgo 86923ffdab 增加 build脚本 2019-04-04 14:44:43 +08:00
dengsgo 72807c78a9 增加 changelog 2019-04-04 14:30:54 +08:00
dengsgo 1f4d27491b 增加 readme国际化 2019-04-04 14:30:35 +08:00
dengsgo 758bd1e409 优化 path(break change) 2019-04-04 10:46:52 +08:00
dengsgo 5c31d76fac 优化 逻辑 2019-04-03 18:04:07 +08:00
dengsgo bee37e29e6 增加 监听文件夹 2019-04-02 18:37:46 +08:00
dengsgo 5f43051ae8 增加 监听文件夹数 2019-04-01 16:44:16 +08:00
dengsgo 9004aceb91 删除 冗余代码 2019-04-01 16:20:20 +08:00
dengsgo 8d5c11d20d 优化 逻辑,时间复杂度 2019-04-01 16:18:01 +08:00
dengsgo 2eb994561d 优化 types in 2019-04-01 15:32:27 +08:00
dengsgo c8d8cfff72 发行 1.8版本 2019-02-27 10:38:17 +08:00
22 changed files with 770 additions and 358 deletions

45
.github/workflows/go.yml vendored Normal file
View File

@ -0,0 +1,45 @@
name: Go
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Get dependencies
run: |
export GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direct
go get
- name: Build
run: go build
- name: Use
run: |
export PATH=/home/runner/work/fileboy/fileboy:$PATH
fileboy version
fileboy help
fileboy init
cat filegirl.yaml
fileboy exec
fileboy daemon
ls -al .fileboy.pid
ps aux | grep fileboy
fileboy stop

3
.gitignore vendored
View File

@ -5,4 +5,5 @@ filegirl.yaml
fileboy-darwin-amd64.bin
fileboy-linux-amd64.bin
fileboy-windows-amd64.exe
./bin/*
.fileboy.pid

12
.gitpod.Dockerfile vendored Normal file
View 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
View 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

27
.travis.yml Normal file
View File

@ -0,0 +1,27 @@
language: go
go:
- 1.13.x
- master
dist: bionic # ubuntu 18.04
script:
- make init
- make build-all
- ls -al bin
- echo $PATH
- cp bin/fileboy-linux-amd64.bin /home/travis/bin/fileboy
- fileboy version
- fileboy help
- fileboy init
- cat filegirl.yaml
- fileboy exec
- fileboy daemon
- ls -al .fileboy.pid
- ps aux | grep fileboy
- fileboy stop

View File

@ -1,3 +1,86 @@
### Release v1.16
2020.08.23
- 优化 文件扫描性能
2020.07.19
- 增加 pid 文件处理
- 增加 信息处理
2020.03.16
- typo deamon->daemon
### Release v1.15
2020.03.08
- 优化 指令模式
- 使用 mod 管理依赖
- go version >= 1.13
- 优化 一些细节
2020.01.02
- 增加 指令配置项 `instruction`, 可以通过预定义的指令来控制 command 的行为
- 增加 `should-finish` 指令
- 增加 `exec-when-start` 指令
- 增加 `ignore-warn` 指令
- 增加 `ignore-info` 指令
- 增加 `ignore-stdout` 指令
- 增加 `ignore-exec-error` 指令
2019.12.28
- 增加 `deamon`命令,支持以守护进程的方式运行在后台 **Unix only**
- 增加 `stop`命令,用来停止 deamon 进程 **Unix only**
- 优化 exec stdout
### 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

40
Makefile Normal file
View File

@ -0,0 +1,40 @@
# 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
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

198
README.md
View File

@ -1,199 +1,3 @@
## 项目说明
fileboy文件变更监听通知系统使用 Go 编写。
适用于 Hot Reload 典型的如开发go项目无需每次手动执行 go build又比如前端 node 打包) 或者 系统监控的场景。
## 特性
- 极简的用法和配置
- 支持多平台Windows/Linux/MacOS
- 支持自定义文件监听范围,监听指定文件夹/不监听指定文件夹/指定后缀文件
- 支持设置多条命令
- 命令支持变量占位符
- 支持冗余任务丢弃,自定义冗余任务范围
- 支持 http 通知
- 更多...
## 编译环境
go version >=1.10,推荐 1.11
## 更新日志
[CHANGELOG](CHANGELOG.md)
## 运行
### 下载二进制文件
Github: [正式版 v1.7](https://github.com/dengsgo/fileboy/releases)
Gitee: [正式版 v1.7](https://gitee.com/dengsgo/fileboy/releases)
下载已经编译好的对应平台二进制文件,重命名为`fileboy`, 加入系统 Path 中即可。
### 源码编译
clone 该项目,进入主目录,运行命令:
```shell
## 安装依赖
go get -u gopkg.in/fsnotify/fsnotify.v1
go get -u gopkg.in/yaml.v2
## 编译
go build
## 运行
./fileboy
```
## 使用
fileboy 的正常运行依赖于 `filegirl.yaml` 配置,所以首次在项目中使用需要初始化 `filegirl.yaml`
- 进入你想要 hot reload 的项目主目录下;
- 运行 `fileboy init`,会在该目录下生成 `filegirl.yaml`文件;
- 查看 `filegirl.yaml`,修改为适合自己项目的配置项;
- 运行 `fileboy`即可.
如果你定义了 `command -> exec`命令,想事先确认是否能正常执行,可以运行 `fileboy exec`命令,系统会尝试运行你的自定义命令。
你可以使用 `fileboy help`查看使用帮助。
## filegirl.yaml 配置文件说明
```yaml
# 主配置
core:
# 配置版本号
version: 1
# 监控配置
monitor:
# 要监听的目录
# test1 监听当前目录下 test1 目录
# test1/test2 监听当前目录下 test1/test2 目录
# test1,* 监听当前目录下 test1 目录及其所有子目录(递归)
# .,* 监听当前目录及其所有子目录(递归)
includeDirs:
- .,*
# 不监听的目录
# .idea 忽略.idea目录及其所有子目录的监听
exceptDirs:
- .idea
- .git
- .vscode
- node_modules
- vendor
# 监听文件的格式,此类文件更改会执行 command 中的命令
# .go 后缀为 .go 的文件更改,会执行 command 中的命令
# .* 所有的文件更改都会执行 command 中的命令
types:
- .go
# 命令
command:
# 监听的文件有更改会执行的命令
# 可以有多条命令,会依次执行
# 如有多条命令,每条命令都会等待上一条命令执行完毕后才会执行
# 如遇交互式命令,允许外部获取输入
# 支持变量占位符,运行命令时会替换成实际值:
# {{file}} 文件名(如 a.txt 、test/test2/a.go)
# {{ext}} 文件后缀(如 .go)
# {{changed}} 文件更新的本地时间戳(纳秒,如 1537326690523046400)
# 变量占位符使用示例cp {{file}} /root/sync -rf 、 myCommand --{{ext}} {{changed}}
exec:
- go version
- go env
# 文件变更后命令在xx毫秒后才会执行单位为毫秒
# 一个变更事件(A)如果在定义的延迟时间(t)内,又有新的文件变更事件(B)那么A会取消执行。
# B及以后的事件均依次类推直到事件Z在t内没有新事件产生Z 会执行
# 合理设置延迟时间,将有效减少冗余和重复任务的执行
# 如果不需要该特性,设置为 0
delayMillSecond: 2000
# 通知器
notifier:
# 文件更改会向该 url 发送请求POST 一段 json 文本数据)
# 触发请求的时机和执行 command 命令是一致的
# 请求超时 15 秒
# POST 格式:
# Content-Type: application/json;charset=UTF-8
# User-Agent: FileBoy Net Notifier v1.7
# Body: {"project_folder":"/watcher-dirs","file":"test.go","changed":1546421173070433800,"ext":".go"}
# 例: http://example.com/notifier/fileboy-listener
# 不启用通知,请留空 ""
callUrl: ""
```
### TODO
- [x] 命令支持变量占位符
- [x] 支持多命令
- [x] 支持监听指定文件夹
- [x] 支持不监听指定文件夹
- [x] 支持监听指定后缀文件
- [x] 支持 http 通知
- [x] 支持冗余任务丢弃
- [ ] 支持 http 合并任务的通知
## QA
#### 很多框架都自带了 hot reload 的功能,为什么还要单独写个 fileboy 呢?
这个是一款通用的 hot reload 的软件,理论上适用于任何需要 hot reload 的场景,并不局限于语言层面上。只要灵活的配置 `filegirl.yaml`文件就行了。
#### fileboy 可以应用在那些具体的场景?
在开发中,我们很需要一款可以帮助我们自动打包编译的工具,那 fileboy 就非常适合这样的场景。比如 go 项目的热编译,让我们可以边修改代码边运行得到反馈。又比如 PHP Swoole 框架由于常驻进程的原因无法更改代码立即reload使用 fileboy 就可以辅助做到传统 PHP 开发的体验。
对于一些需要监控文件日志或者配置变动的场景, fileboy 同样适合。你可以事先编写好相应的通知报警脚本,然后定义`filegirl.yaml`中的`command`命令,交由 fileboy 自动运行监控报警。
#### 通知器在什么时候会发送 http 请求 ?
通知器发送 http 通知的前提是在配置文件中设置了 `callUrl` 参数(不为空即为已设置)。触发请求的时机和执行 command 命令是一致的,`command -> delayMillSecond` 参数对于触发器同样有效。请求超时默认15秒.
#### idea 下更改文件,为什么会执行两次或者多次 command ?
由于 idea 系列软件特殊的文件保存策略他会自动创建一些临时文件并且在需要时多次重写文件所以有时反映在文件上就是有多次的更改所以会出现这种情况。1.5之后的版本增加了 `delayMillSecond` 参数,可以解决这个问题。
#### filegirl.yaml 里面的 command 不支持复杂的命令吗?
对于“很复杂的命令”这种说法很难去定义,比如 `echo "hello world"`并不复杂,但是对于 fileboy 来讲,目前无法解析这种命令。
fileboy 目前支持 `命令 + 参数`这种形式的 command而且 参数中不能有""符号或者有空格。如:
`go build`:支持;
`go env`:支持;
`php swoole start --daemon`:支持
`cat a.txt | grep "q" | wc -l`:不支持
对于不支持的命令,可以把它写到一个文件里,然后在 command 中执行这个文件来解决。
#### 为什么起名为 fileboy又把配置名叫做 filegirl
因为爱情~~ (◡ᴗ◡✿)
### 贡献者
> 排名不分先后
[@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>
基于 [fileboy](https://github.com/dengsgo/fileboy) 魔改版

56
daemon.go Normal file
View File

@ -0,0 +1,56 @@
package main
import (
"io/ioutil"
"os"
"os/exec"
"runtime"
"strconv"
)
func getPidFile() string {
return projectFolder + "/.fileboy.pid"
}
func runAsDaemon() (int, error) {
if runtime.GOOS == "windows" {
logAndExit("daemons mode cannot run on windows.")
}
err := stopDaemon()
if err != nil {
logAndExit(err)
}
_, err = exec.LookPath("fileboy")
if err != nil {
logAndExit("cannot found `fileboy` command in the PATH")
}
daemon := exec.Command("fileboy")
daemon.Dir = projectFolder
daemon.Env = os.Environ()
daemon.Stdout = os.Stdout
err = daemon.Start()
if err != nil {
logAndExit(err)
}
pid := daemon.Process.Pid
if pid != 0 {
ioutil.WriteFile(getPidFile(), []byte(strconv.Itoa(pid)), 0644)
}
return pid, nil
}
func stopDaemon() error {
bs, err := ioutil.ReadFile(getPidFile())
if err != nil {
return nil
}
_ = exec.Command("kill", string(bs)).Run()
os.Remove(getPidFile())
return nil
}
func stopSelf() {
pid := os.Getpid()
os.Remove(getPidFile())
_ = exec.Command("kill", strconv.Itoa(pid)).Run()
}

View File

@ -2,141 +2,188 @@ package main
import (
"fmt"
"gopkg.in/fsnotify/fsnotify.v1"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"math/rand"
"os"
"os/signal"
"path"
"strconv"
"strings"
"syscall"
"time"
"gopkg.in/fsnotify/fsnotify.v1"
"gopkg.in/yaml.v2"
)
const (
Version = 1
PreError = "ERROR:"
PreWarn = "Warn:"
InstExecWhenStart = "exec-when-start"
InstShouldFinish = "should-finish"
InstIgnoreWarn = "ignore-warn"
InstIgnoreInfo = "ignore-info"
InstIgnoreStdout = "ignore-stdout"
InstIgnoreExecError = "ignore-exec-error"
)
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() {
func parseConfig(filegirlYamlPath string) {
cfg = new(FileGirl)
fc, err := ioutil.ReadFile(projectFolder + "/filegirl.yaml")
fc, err := ioutil.ReadFile(filegirlYamlPath)
if err != nil {
log.Panicln(PreError, "read filegirl.yaml file err: ", err)
logError("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("parsed filegirl.yaml failed: ", err)
}
if cfg.Core.Version > Version {
log.Panicln(PreError, "current fileboy support max version : ", Version)
logAndExit("current fileboy support max version : ", Version)
}
log.Println(cfg)
// init map
cfg.Monitor.TypesMap = map[string]bool{}
cfg.Monitor.ExceptTypesMap = map[string]bool{}
cfg.Monitor.IncludeDirsMap = map[string]bool{}
cfg.Monitor.ExceptDirsMap = map[string]bool{}
cfg.Monitor.IncludeDirsRec = map[string]bool{}
cfg.InstructionMap = map[string]bool{}
// convert to map
for _, v := range cfg.Monitor.Types {
cfg.Monitor.TypesMap[v] = true
}
for _, v := range cfg.Instruction {
cfg.InstructionMap[v] = true
}
for _, v := range cfg.Monitor.ExceptTypes {
cfg.Monitor.ExceptTypesMap[v] = true
}
log.Printf("%+v", cfg)
}
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))
if event.Name == getPidFile() {
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:
ext := path.Ext(event.Name)
if len(cfg.Monitor.Types) > 0 &&
!keyInMonitorTypesMap(".*", cfg.Monitor.TypesMap) &&
!keyInMonitorTypesMap(ext, cfg.Monitor.TypesMap) {
return
}
if len(cfg.Monitor.ExceptTypes) > 0 &&
keyInMonitorTypesMap(ext, cfg.Monitor.ExceptTypesMap) {
return
}
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])
logInfo("collecting directory information...")
dirsMap := map[string]bool{}
for _, dir := range cfg.Monitor.ExceptDirs {
if dir == "." {
logAndExit("exceptDirs must is not project root path ! err path:", dir)
}
}
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])
}
if strings.HasPrefix(darr[0], "/") {
log.Fatalln(PreError, "dirs must be relative paths ! err path:", cfg.Monitor.IncludeDirs[i])
logAndExit("filegirl section monitor dirs is error. ", dir)
}
//if strings.HasPrefix(darr[0], "/") {
// logAndExit("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]
//md := projectFolder + "/" + darr[0]
md := 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)
listFile(p, func(d string) {
dirs = arrayRemoveElement(dirs, d)
})
}
for _, dir := range dirs {
log.Println("watcher add -> ", dir)
for dir := range dirsMap {
logInfo("watcher add -> ", dir)
err := watcher.Add(dir)
if err != nil {
log.Fatalln(err)
logAndExit(err)
}
}
log.Println("fileboy is ready.")
logInfo("total monitored dirs: " + strconv.Itoa(len(dirsMap)))
logInfo("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,55 +192,165 @@ 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 {
return
}
log.Println(PreError, err)
logError(err)
}
}
}()
addWatcher()
<-done
}
func watchChangeHandler(event fsnotify.Event) {
// stop the fileboy daemon process when the .fileboy.pid file is changed
if event.Name == getPidFile() &&
(event.Op == fsnotify.Remove ||
event.Op == fsnotify.Write ||
event.Op == fsnotify.Rename) {
logUInfo("exit daemon process")
stopSelf()
return
}
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
if hitDirs(event.Name, &cfg.Monitor.ExceptDirs) {
continue
}
_ = watcher.Remove(event.Name)
err := watcher.Add(event.Name)
if err == nil {
do = true
logInfo("watcher add -> ", event.Name)
} else {
logWarn("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 {
logInfo("watcher add -> ", event.Name)
} else {
logWarn("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 {
case len(os.Args) == 1:
show()
parseConfig(getFileGirlPath())
done := make(chan bool)
initWatcher()
defer watcher.Close()
if keyInInstruction(InstExecWhenStart) {
taskMan.run(new(changedFile))
}
<-done
return
}
if l == 2 {
case len(os.Args) > 1:
c := os.Args[1]
switch c {
case "init":
err := ioutil.WriteFile(projectFolder+"/filegirl.yaml", []byte(exampleFileGirl), 0644)
case "deamon", "daemon":
pid, err := runAsDaemon()
if err != nil {
log.Println(PreError, "error create filegirl.yaml config! ", err)
logAndExit(err)
}
logUInfo("PID:", pid)
logUInfo("fileboy is ready. the main process will run as a daemons")
return
case "stop":
err := stopDaemon()
if err != nil {
logAndExit(err)
}
logUInfo("fileboy daemon is stoped.")
return
case "init":
_, err := ioutil.ReadFile(getFileGirlPath())
if err == nil {
logError("profile filegirl.yaml already exists.")
logAndExit("delete it first when you want to regenerate filegirl.yaml")
}
err = ioutil.WriteFile(getFileGirlPath(), []byte(exampleFileGirl), 0644)
if err != nil {
logError("profile filegirl.yaml create failed! ", err)
return
}
log.Println("create filegirl.yaml ok")
logUInfo("profile filegirl.yaml created ok")
return
case "exec":
parseConfig()
parseConfig(getFileGirlPath())
newTaskMan(0, cfg.Notifier.CallUrl).run(new(changedFile))
return
case "profile":
filegirlYamlPath := os.Args[2]
show()
parseConfig(filegirlYamlPath)
done := make(chan bool)
initWatcher()
defer watcher.Close()
if keyInInstruction(InstExecWhenStart) {
taskMan.run(new(changedFile))
}
<-done
return
case "version", "v", "-v", "--version":
fmt.Println(versionDesc)
default:
case "help", "--help", "--h", "-h":
fmt.Print(helpStr)
default:
logAndExit("unknown parameter, use 'fileboy help' to view available commands")
}
return
default:
logAndExit("unknown parameters, use `fileboy help` show help info.")
}
}
func signalHandler() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
if taskMan != nil && taskMan.cmd != nil && taskMan.cmd.Process != nil {
if err := taskMan.cmd.Process.Kill(); err != nil {
logError("stopping the process failed: PID:", taskMan.cmd.ProcessState.Pid(), ":", err)
}
}
os.Exit(0)
}()
}
func getFileGirlPath() string {
return projectFolder + "/" + filegirlYamlName
}
func show() {
fmt.Print(logo)
rand.Seed(time.Now().UnixNano())
@ -205,11 +362,13 @@ func show() {
func main() {
log.SetPrefix("[FileBoy]: ")
log.SetFlags(2)
show()
log.SetOutput(os.Stdout)
// show()
var err error
projectFolder, err = os.Getwd()
if err != nil {
log.Fatalln(err)
logAndExit(err)
}
signalHandler()
parseArgs()
}

View File

@ -6,8 +6,18 @@ type FileGirl struct {
}
Monitor struct {
Types []string `yaml:"types"`
ExceptTypes []string `yaml:"exceptTypes"`
IncludeDirs []string `yaml:"includeDirs"`
ExceptDirs []string `yaml:"exceptDirs"`
Events []string `yaml:"events"`
// convert to
TypesMap map[string]bool `yaml:"-"`
ExceptTypesMap 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"`
@ -16,4 +26,8 @@ type FileGirl struct {
Notifier struct {
CallUrl string `yaml:"callUrl"`
}
Instruction []string `yaml:"instruction"`
// convert to
InstructionMap map[string]bool `yaml:"-"`
}

10
go.mod Normal file
View File

@ -0,0 +1,10 @@
module fileboy
go 1.13
require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 // indirect
gopkg.in/fsnotify/fsnotify.v1 v1.4.7
gopkg.in/yaml.v2 v2.3.0
)

11
go.sum Normal file
View File

@ -0,0 +1,11 @@
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 h1:9UQO31fZ+0aKQOFldThf7BKPMJTiBfWycGh/u3UoO88=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify/fsnotify.v1 v1.4.7 h1:XNNYLJHt73EyYiCZi6+xjupS9CpvmiDgjPTAjrBlQbo=
gopkg.in/fsnotify/fsnotify.v1 v1.4.7/go.mod h1:Fyux9zXlo4rWoMSIzpn9fDAYjalPqJ/K1qJ27s+7ltE=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

12
mac-build-all Executable file
View 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
View 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

View File

@ -3,7 +3,6 @@ package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"strings"
"time"
@ -14,6 +13,7 @@ type postParams struct {
File string `json:"file"`
Changed int64 `json:"changed"`
Ext string `json:"ext"`
Event string `json:"event"`
}
type NetNotifier struct {
@ -34,7 +34,7 @@ func newNetNotifier(callUrl string) *NetNotifier {
func (n *NetNotifier) Put(cf *changedFile) {
if !n.CanPost {
log.Println(PreWarn, "notifier call url ignore. ", n.CallUrl)
logWarn("notifier call url ignore. ", n.CallUrl)
return
}
n.dispatch(&postParams{
@ -42,13 +42,14 @@ func (n *NetNotifier) Put(cf *changedFile) {
File: cf.Name,
Changed: cf.Changed,
Ext: cf.Ext,
Event: cf.Event,
})
}
func (n *NetNotifier) dispatch(params *postParams) {
b, err := json.Marshal(params)
if err != nil {
log.Println(PreError, "json.Marshal n.params. ", err)
logError("json.Marshal n.params. ", err)
return
}
client := &http.Client{
@ -56,14 +57,14 @@ func (n *NetNotifier) dispatch(params *postParams) {
}
req, err := http.NewRequest("POST", n.CallUrl, bytes.NewBuffer(b))
if err != nil {
log.Println(PreError, "http.NewRequest. ", err)
logError("http.NewRequest. ", err)
return
}
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
req.Header.Set("User-Agent", "FileBoy Net Notifier v1.7")
req.Header.Set("User-Agent", "FileBoy Net Notifier v1.16")
resp, err := client.Do(req)
if err != nil {
log.Println(PreError, "notifier call failed. err:", err)
logError("notifier call failed. err:", err)
return
}
defer func() {
@ -74,5 +75,5 @@ func (n *NetNotifier) dispatch(params *postParams) {
if resp.StatusCode >= 300 {
// todo retry???
}
log.Println("notifier done .")
logInfo("notifier done .")
}

54
raw.go
View File

@ -32,6 +32,25 @@ monitor:
types:
- .go
# command
# .DS_Store .DS_Store command
exceptTypes:
- .DS_Store
# command
#
# write
# rename
# remove
# create
# chmod (unix)
events:
- write
- rename
- remove
- create
- chmod
#
command:
#
@ -41,6 +60,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 +68,7 @@ command:
- go env
# xx
# (A)(t)(B)A
# (A)(t), (B), A
# BZtZ
#
# 0
@ -61,11 +81,27 @@ notifier:
# 15
# POST :
# Content-Type: application/json;charset=UTF-8
# User-Agent: FileBoy Net Notifier v1.7
# Body: {"project_folder":"/watcher-dirs","file":"test.go","changed":1546421173070433800,"ext":".go"}
# User-Agent: FileBoy Net Notifier v1.16
# Body: {"project_folder":"/project/path","file":"main.go","changed":1576567861913824940,"ext":".go","event":"write"}
# : http://example.com/notifier/fileboy-listener
# ""
callUrl: ""
#
instruction:
# command
#
# exec-when-start fileboy 'exec'
# should-finish 'exec' (C)(L)退 L 退 kill L exit code
# L 退(N) C N
# ignore-stdout 'exec' stdout
# ignore-warn fileboy warn
# ignore-info fileboy info
# ignore-exec-error 'exec' 退
#- should-finish
#- exec-when-start
- ignore-warn
`
var firstRunHelp = ` fileboy ?
@ -81,6 +117,12 @@ Usage of fileboy:
fileboy, filegirl.yaml
exec
command
daemon
filegirl.yaml
stop
profile
version
`
@ -104,13 +146,13 @@ var logo = `
_____ _ | | _____ ____) ) | | | |___| |
| ___) | | | | | ___) | __ (| | | |\_____/
| | _| |_| |_____| |_____| |__) ) |___| | ___
|_| (_____)_______)_______)______/ \_____/ (___) V1.7
|_| (_____)_______)_______)______/ \_____/ (___) V1.16
`
var statement = `Dengsgo [dengsgo@gmail.com] Open Source with MIT License`
var versionDesc = `
Version fileboy: v1.7 filegirl: v` + strconv.Itoa(Version) + `
Released 2019.1.24
Version fileboy: v1.16 filegirl: v` + strconv.Itoa(Version) + `
Released 2020.10.17
Licence MIT
Author dengsgo [dengsgo@gmail.com]
Website https://github.com/dengsgo/fileboy

BIN
resources/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

BIN
resources/jetbrains.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 KiB

8
snapcraft.yaml Normal file
View File

@ -0,0 +1,8 @@
name: fileboy
vendor: dengsgo, https://github.com/dengsgo, <dengsgo@yoytang.com>
summary: File Change Monitoring Notification Tools.
description: File Change Monitoring Notification Tools. Please Visit https://github.com/dengsgo/fileboy
version: 1.15
icon: resources/icon.png
base: core18
grade: stable

View File

@ -1,10 +1,6 @@
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/exec"
"sync"
@ -18,18 +14,40 @@ type TaskMan struct {
notifier *NetNotifier
putLock sync.Mutex
runLock sync.Mutex
waitChan chan bool
waitQueue []*changedFile
}
func newTaskMan(delay int, callUrl string) *TaskMan {
return &TaskMan{
delay: delay,
notifier: newNetNotifier(callUrl),
t := &TaskMan{
delay: delay,
notifier: newNetNotifier(callUrl),
waitChan: make(chan bool, 1),
waitQueue: []*changedFile{},
}
if keyInInstruction(InstShouldFinish) {
go func() {
for {
<-t.waitChan
if len(t.waitQueue) >= 1 {
cf := t.waitQueue[len(t.waitQueue)-1]
if len(t.waitQueue) > 1 {
logInfo("redundant tasks dropped:", len(t.waitQueue)-1)
}
t.waitQueue = []*changedFile{}
go t.preRun(cf)
}
}
}()
}
return t
}
func (t *TaskMan) Put(cf *changedFile) {
if t.delay < 1 {
t.preRun(cf)
t.dispatcher(cf)
return
}
t.putLock.Lock()
@ -40,62 +58,78 @@ func (t *TaskMan) Put(cf *changedFile) {
if t.lastTaskId > cf.Changed {
return
}
t.preRun(cf)
t.dispatcher(cf)
}()
}
func (t *TaskMan) dispatcher(cf *changedFile) {
if keyInInstruction(InstShouldFinish) {
t.waitQueue = append(t.waitQueue, cf)
if t.cmd == nil {
t.waitChan <- true
return
}
logInfo("waitting for the last task to finish")
logInfo("waiting tasks:", len(t.waitQueue))
} else {
t.preRun(cf)
}
}
func (t *TaskMan) preRun(cf *changedFile) {
if t.cmd != nil && t.cmd.Process != nil && t.cmd.ProcessState != nil && !t.cmd.ProcessState.Exited() {
log.Println("stop old process ")
if t.cmd != nil && t.cmd.Process != nil {
if err := t.cmd.Process.Kill(); err != nil {
log.Println(PreWarn, "stopped err, reason:", err)
logInfo("stop old process ")
logWarn("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++ {
carr := cmdParse2Array(cfg.Command.Exec[i], cf)
log.Println("EXEC", carr)
logInfo("EXEC", carr)
t.cmd = exec.Command(carr[0], carr[1:]...)
//cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: syscall.CREATE_UNICODE_ENVIRONMENT}
t.cmd.Stdin = os.Stdin
//cmd.Stdout = os.Stdout
t.cmd.Stdout = os.Stdout
if keyInInstruction(InstIgnoreStdout) {
t.cmd.Stdout = nil
}
t.cmd.Stderr = os.Stderr
t.cmd.Dir = projectFolder
t.cmd.Env = os.Environ()
stdout, err := t.cmd.StdoutPipe()
err := t.cmd.Start()
if err != nil {
log.Println(PreError, err.Error())
return
}
err = t.cmd.Start()
if err != nil {
log.Println(PreError, "run command", carr, "error. ", err)
}
reader := bufio.NewReader(stdout)
for {
line, err2 := reader.ReadString('\n')
if err2 != nil || io.EOF == err2 {
break
logError("run command", carr, "error. ", err)
if keyInInstruction(InstIgnoreExecError) {
continue
}
fmt.Print(line)
break
}
err = t.cmd.Wait()
if err != nil {
log.Println(PreWarn, "cmd wait err ", err)
logError("command exec failed:", carr, err)
if keyInInstruction(InstIgnoreExecError) {
continue
}
break
}
if t.cmd.Process != nil && t.cmd.ProcessState != nil && !t.cmd.ProcessState.Exited() {
if err = t.cmd.Process.Kill(); err != nil {
log.Println(PreWarn, "cmd cannot kill, reason:", err)
if t.cmd.Process != nil {
err := t.cmd.Process.Kill()
logInfo(t.cmd.ProcessState)
if t.cmd.ProcessState != nil && !t.cmd.ProcessState.Exited() {
logError("command cannot stop!", carr, err)
}
}
}
log.Println("end ")
if keyInInstruction(InstShouldFinish) {
t.cmd = nil
t.waitChan <- true
}
logInfo("EXEC end")
}

101
util.go
View File

@ -2,17 +2,20 @@ 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, maps map[string]bool) bool {
_, ok := maps[k]
return ok
}
func keyInInstruction(k string) bool {
_, ok := cfg.InstructionMap[k]
return ok
}
func cmdParse2Array(s string, cf *changedFile) []string {
@ -27,12 +30,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,
)
}
@ -47,41 +53,74 @@ func dirParse2Array(s string) []string {
return r
}
func hitDirs(d string, dirs *[]string) bool {
d += "/"
for _, v := range *dirs {
if strings.HasPrefix(d, projectFolder+"/"+v+"/") {
return true
}
}
return false
}
func listFile(folder string, fun func(string)) {
files, _ := ioutil.ReadDir(folder)
for _, file := range files {
if file.IsDir() {
d := folder + "/" + file.Name()
if hitDirs(d, &cfg.Monitor.ExceptDirs) {
continue
}
fun(d)
listFile(d, fun)
}
}
}
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 logInfo(v ...interface{}) {
if keyInInstruction(InstIgnoreInfo) {
return
}
logUInfo(v...)
}
func logUInfo(v ...interface{}) {
v = append([]interface{}{"I:"}, v...)
log.Println(v...)
}
func logWarn(v ...interface{}) {
if keyInInstruction(InstIgnoreWarn) {
return
}
v = append([]interface{}{"W:"}, v...)
log.Println(v...)
}
func logError(v ...interface{}) {
v = append([]interface{}{"E:"}, v...)
log.Println(v...)
}
func logAndExit(v ...interface{}) {
v = append([]interface{}{"O:"}, v...)
log.Println(v...)
os.Exit(15)
}